Part

PS/2 keyboard

A real PS/2 keyboard on two wires, scan code set 2. Click it while it runs and type on it for real; Escape gives the keyboard back.

See every part
  • 4 pins
  • In 1 project
Drawn live by the editor's own code, at the size you see it.
At a glance

The PS/2 keyboard in brief

What it is, and where it sits on the bench.

A PS/2 keyboard needs only four wires, VCC, GND, CLK and DATA, and both signal lines are open collector with the pull-ups inside the keyboard, so the host uses two plain input pins and no resistors. The keyboard generates the clock and the host reads DATA on its falling edge, so CLK must go to an interrupt pin: 2 or 3 on an Uno, any GPIO on an ESP32. A real keyboard wants 5 volts; Mokxi works from 3.0 volts so a 3.3 volt board can power it directly, the one place it is more generous than the specification. Mokxi models the real eleven-bit frame (a start bit, eight data bits least significant first, odd parity and a stop bit) on a 12.5 kHz clock, scan code set 2 with 0xF0 break codes and 0xE0 prefixes, and the host-to-device half: Reset, Echo, Read ID, Set LEDs and the rest, with the 0xAA self-test reply 600 milliseconds. Click the keyboard and your own typing becomes scan codes; Escape gives it back. The classic mistake is CLK on a pin with no interrupt, which leaves the keyboard silent. Typematic repeat, the lock lamps, Print Screen and Pause are not modeled.

Family
Inputs
Pins
4
Pins

4 pins

Named as the editor names them, so a wire lands where you expect it.

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

How to use a PS/2 keyboard with an Arduino

Four wires: VCC, GND, CLK and DATA. Both signals are open collector with the pull-ups inside the keyboard, so they go to two plain input pins with no resistors. The keyboard makes the clock and the host reads DATA on its falling edge, so CLK must be on an interrupt pin: 2 or 3 on an Uno. The typewriter here is an ESP32-C3 board, where any GPIO will do, with CLK on GPIO 3 and DATA on GPIO 2.

Click the keyboard while it runs and your own typing becomes set 2 scan codes; Escape gives it back. On an Uno the PS2Keyboard library’s begin(DataPin, IRQpin), available() and read() compile as they stand; note the data pin comes first. CLK on a pin with no interrupt is the fault everybody hits: the sketch is right and the keyboard is silent.

Part pin
Board pin
CLK
GPIO 3
DATA
GPIO 2
VCC
3V3
GND
GND

A real PS/2 keyboard wants 5 V; the part works from 3.0 V so the ESP32-C3 can power it from 3V3 without 5 V on its pins.

ESP32-C3: PS/2 typewriterlive0.000 s 0.00x
Click to open it in the editor
This is the simulator itself, running here. Click anything to open it in the editor.
The PS2Keyboard library on an Uno
#include <PS2Keyboard.h>
PS2Keyboard keyboard;

void setup() {
  keyboard.begin(8, 3);  // DATA on 8, CLK on 3
  Serial.begin(9600);
}

void loop() {
  if (keyboard.available()) {
    char c = keyboard.read();
    Serial.print(c);
  }
}
How it is modeled

What is true about the PS/2 keyboard, here

In its help article’s own words: what the simulation gets right, and where it stops.

Modeled

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.

Not modeled

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.

From PS/2 keyboard, in full.

Projects

See the PS/2 keyboard in a project

One built-in project uses it. It opens in the editor, already wired.

Boards

The boards it is shown on

This board has a built-in project with the PS/2 keyboard in it.

Wire up the PS/2 keyboard

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.