PS/2 keyboard

A real PS/2 keyboard on the two wires it has, with eleven-bit frames and scan code set 2. Click it while the simulation runs and type on it for real.

CLK, DATA and the supply: four pins and nothing else. Both signal lines are open collector with the pull-ups inside the keyboard, which is why an Arduino reads one with two plain input pins and no resistors on the breadboard: either end may pull a line down, and neither ever drives it up.

Pins

Pin What it does
VCC Supply.
GND Ground.
CLK The clock, which the keyboard generates. Open collector.
DATA The data line. Open collector.

Properties

None.

While it runs

Click a key on the picture to hold it down, one at a time.

Click the keyboard and it takes your real keyboard: every key you press from then on becomes a set 2 scan code on DATA, and the editor's own shortcuts stop working until you give it back.

Escape gives it back. It is the one key that never reaches the part, so there is always a way out; the Esc key drawn on the picture is there to be clicked when a sketch wants one. Keys your browser keeps for itself (F11, F12, and anything with Ctrl, Cmd or Alt held) do not reach the part either, so click those on the picture too.

Every other key on the picture is captured: the letters and digits, the punctuation, Tab, Enter, Backspace, the two Shifts, Caps Lock, Ctrl, Alt, the Windows and menu keys, the function row, the navigation block, the arrows and the whole numeric keypad. The key drawn lit is the one the simulation says is down.

What the model gets right

The frame is the real one: a start bit, eight data bits least significant first, an odd parity bit and a stop bit, with the keyboard generating the clock at 12.5 kHz (inside the 10 to 16.7 kHz the specification allows), the data settled 15 µs before the clock falls and the clock low for 40 µs, both inside their windows. The host reads DATA on the falling edge.

Scan code set 2, which is what every PS/2 keyboard powers up in: a key pressed sends its make code, a key released sends 0xF0 then the same code, and the keys added after the original 84-key layout (the arrows, the navigation block, the right-hand modifiers, the keypad's / and Enter) carry an 0xE0 prefix in front of both, so releasing the up arrow is E0 F0 75.

The host-to-device half is there too. Hold CLK down for more than 100 µs, pull DATA down and let CLK go, and the keyboard clocks eleven bits in, reading DATA on each rising edge, and pulls DATA down for a twelfth clock to acknowledge. It answers Reset (0xFF), Resend, Set defaults, Enable and Disable scanning, Set typematic rate, Read ID (0xFA 0xAB 0x83), Set scan code set, Echo and Set LEDs, and answers 0xFE to a command it does not know, which is what a keyboard does. It also runs the power-on self test and sends 0xAA six hundred milliseconds later, as the specification says.

Holding the clock down inhibits it: a byte typed while the host has CLK low waits in the keyboard's buffer and goes out when the line comes back, rather than being lost.

What it does not model

Typematic repeat. A real keyboard repeats the last key held down after its typematic delay. This one has no timer for it. The repeat you see is your own computer's, arriving as more presses, which is what it looks like on the wire anyway. 0xF3 is accepted and its rate byte kept, and changes nothing.

The lock lamps. 0xED is accepted and the three bits kept; there are no LEDs on the picture to light.

Print Screen and Pause. Their set 2 sequences are the two multi-byte oddities (E0 12 E0 7C and E1 14 77 E1 F0 14 F0 77). There is no key for them on this part, so nothing here has to pretend to send them.

The supply. A PS/2 keyboard is nominally a 5 V device; the connector carries 5 V ±10 %. This model works from 3.0 V up, so a 3.3 V board can drive one directly the way every ESP32 PS/2 sketch assumes. That is the one place the part is more generous than the specification, and it is deliberate: the alternative is an example circuit that puts 5 V signals on pins that are not 5 V tolerant.

Common mistakes

Putting CLK on a pin with no external interrupt. On an ATmega328P only pins 2 and 3 have one, so a keyboard on any other pin is simply silent. The sketch is right and the wire is wrong. On an ESP32-C3 any GPIO will do.

Reading DATA on the rising edge of the clock. That is the direction the keyboard reads in; a host reads on the falling edge, and a driver that has it backwards gets a byte shifted by one bit and a parity failure on every frame.

See it in action

Typewriter reads this keyboard on an interrupt and prints what you type on a 16x2 LCD. Open it at /templates.