Skip to content
If this is your first visit, please click here to learn about shipping and delivery.
If this is your first visit, please touch here to learn about shipping and delivery.
PS/2 mini-DIN keyboard socket breakout board

Add a PS/2 Keyboard to a Microcontroller: Wiring, Scan Codes and Reliable Input

A full keyboard is useful when an electronics project needs labels, numerical settings or commands that would be awkward on a few buttons. A PS/2 keyboard offers a relatively simple interface for a stand-alone terminal, retrocomputer or classroom protocol experiment.

The PS/2 socket breakout from PMD Way provides a convenient connection without cutting a keyboard cable. The board exposes the connector; your controller and software still need to supply compatible power and interpret the keyboard's signals.

Start with a keyboard that supports PS/2

Use a known working PS/2 keyboard for the first test. A USB keyboard with a small passive plug adaptor is suitable only if the keyboard itself supports PS/2 signalling. StarTech's adaptor compatibility guidance makes this distinction explicit.

A passive adaptor rearranges connections. It does not translate USB packets into PS/2 clock and data. If your keyboard is USB-only, use a suitable USB host solution or an active converter designed for the required direction.

Record the keyboard model and layout. Keep that same keyboard throughout initial development so a hardware change does not become confused with a firmware change. Test additional keyboards later as a separate compatibility exercise.

Verify the connector and electrical levels

The conventional six-pin PS/2 connector uses pin 1 for data, pin 3 for ground, pin 4 for supply and pin 5 for clock; pins 2 and 6 are reserved. Digilent's PS/2 reference manual documents the connector separately from its board header. Do not confuse a module header's numbering with the mini-DIN connector's numbering.

Identify whether a drawing shows the mating face or rear of the socket. With power removed, use continuity checks to map the breakout's labelled pads to connector contacts. Cable colours are not a dependable substitute for this check.

PS/2 keyboards normally require 5 V power. Their clock and data lines may have pull-ups to 5 V, which can damage a 3.3 V-only controller. PJRC's PS2Keyboard documentation highlights this issue. Use an appropriate interface that preserves bidirectional open-collector behaviour where the controller is not voltage tolerant.

Check the keyboard's current requirement against the available supply, including its indicators and startup behaviour. Keep a common signal reference in a non-isolated arrangement. Make connections with power off for initial testing rather than assuming the project supports hot plugging.

Understand the clock and data connection

PS/2 uses open-collector signalling: devices pull a line low or release it. They should not fight each other by driving opposite push-pull levels. The interface idles high when the lines are released.

For keyboard-to-host traffic, a frame contains a start bit, eight data bits sent least-significant bit first, odd parity and a stop bit. The keyboard supplies the clock. This is a clocked interface, so connecting the data lead to a UART receive pin does not make it ordinary serial text.

If you use a library, check its required clock pin and interrupt support for your particular controller. PJRC's implementation requires an interrupt-capable clock input. Examples written for another board may use a pin number that has a different function on yours.

During early testing, keep the receiver's work small. Capture incoming data promptly and place it in a buffer. Display formatting, command execution and slow logging belong outside the timing-sensitive capture routine.

Scan codes represent keys, not finished characters

In the commonly used scan-code set 2, pressing a key sends its make code and releasing most keys sends F0 followed by that code. Extended keys can include E0 prefixes, and some keys need special handling. Digilent's manual provides examples of these sequences.

A letter's key code does not independently tell you whether the user intended an uppercase letter. The host must consider Shift, Caps Lock and the selected layout. A character-oriented library may perform this translation for you, while a lower-level library may expose raw events.

Choose the abstraction that fits the project. A text terminal usually benefits from translated characters. A game or control panel may need separate key-down and key-up events, including simultaneous keys. Check the library's actual output contract before designing the application around it.

Do not assume that a mouse connected to the same socket will behave as a keyboard. Similar electrical signalling does not make their data interpretation and initialisation identical.

Bring the interface up in stages

  1. Confirm power: measure the intended supply and check for unexpected current or heating.
  2. Confirm idle signals: verify released clock and data levels are compatible with the controller.
  3. Observe one key: capture a press, hold and release without running application commands.
  4. Test modifiers: compare lower case, Shift combinations and Caps Lock behaviour.
  5. Test navigation: include arrows, Enter, Backspace and Escape.
  6. Exercise the application: type while its display and other tasks are busy.

If raw scan bytes are available, log them in hexadecimal with a clear event boundary. This helps distinguish a lost byte from an incorrect layout table. If the library exposes characters only, use its simplest example first and compare the observed characters with its documented key mappings.

Design repeat and release behaviour intentionally

Holding a key can generate repeated input. That is helpful when editing text but troublesome when a key triggers a one-time operation. Decide separately which actions may repeat, which require a fresh press and which remain active only while held.

For a setting editor, accumulate characters in a bounded text field and apply the value only after validation and confirmation. Reject an invalid number before changing the hardware. Keep Escape or another clear cancellation action available.

For a control interface, maintain explicit key states and clear them when the connection or parser loses synchronisation. A missed release should not leave an application behaving as though a key is held indefinitely. Test this recovery path with benign outputs before connecting the real mechanism.

Troubleshoot the layer that is failing

  • No activity: check keyboard compatibility, power, pin mapping and the clock input configuration.
  • Intermittent characters: inspect signal levels, wiring and tasks that delay input processing.
  • Wrong punctuation: verify keyboard layout and modifier translation.
  • Broken arrow keys: inspect extended-code handling or the library's special-key mapping.
  • Lost input during display updates: review buffering and the time spent with interrupts unavailable.

Finish by testing a cold start, a long typing session and the busiest normal application workload. Save the verified wiring map and keyboard model with the project. A board from the PS/2 breakout collection simplifies the physical connection; a deliberate input model makes that connection useful.

Previous article Battery Power for Arduino Projects: Voltage Range, Runtime and Regulator Heat