Love your old Model M keyboard but don't want to buy a USB adapter? Use this circuit and kernel module and you won't even use up a USB port!


You'll notice in the schematic that the keyboard clock line isn't connected to a GPIO, so how can this work? After a bit of testing I've found you can read a keyboard without using the clock line if you use a serial port running at a custom speed. This works because the keyboard clock period is regular and consistent (at least on the keyboards I've tested). The only other things you need are two resistors as required by the PS/2 spec, and an LED to drop the 5V keyboard data output to something the Zero can handle (kind of an unusual use for an LED, but it works beautifully).

You'll need a mini DIN 6-pin socket to connect to the keyboard. It can be scrounged from an old keyboard adapter, or you can eliminate it altogether by cutting the connector off the keyboard cable and wiring it directly to the interface. Refer to the PS/2 interfacing link at the bottom of this page for pinout information. The LED can be replaced with another color as long as the forward voltage at low current is 1.8-2.1 volts. The resistors are also not critical - any value from 2000 to 10,000 ohms should work, 1/10 watt or larger.

Solder the components directly to the GPIO connector holes like I did in the picture below. All connections are to the outside row of holes on the Zero. The LED's cathode is bent down and soldered in the fifth hole, the anode is soldered to a resistor. The keyboard data line is also soldered to this junction. The keyboard +5V line is soldered in the first hole, and the keyboard ground is soldered in the third hole. The keyboard clock line is soldered to the other resistor. Lastly the free ends of both resistors are bent round and soldered in the second hole on the Zero. Make sure nothing connects where it shouldn't - a bit of paper or tape under everything will prevent this from happening. The picture is a bit fuzzy, but if you refer to the schematic and pinouts for the Pi it should become clear.

IMPORTANT: The keyboard should not be hot-plugged: connect or disconnect only when the Zero is powered off. Make sure you wire it correctly or you may fry your Zero, or the keyboard!

The kernel module needs to know what the clock divisor for the UART should be. To find this, connect the keyboard and power up. Then compile the ps2test program and run it as root. Follow the onscreen instructions to find the needed dividers. You'll have to edit the overlay source then compile it to set these values. The overlay as compiled will work on an IBM Model M keyboard.

Source code is available under the files sub-page. Easy module compiling instructions are available under my piModules page. 

The device tree overlay ps2pi-overlay in the files subpage needs to be copied to /boot/overlays and dtoverlay=ps2pi needs to be added to /boot/config.txt. It will automatically load the kernel module as long as it's in the proper subdirectory in /lib/modules and depmod has been run while you are in that directory.

You also need to disable the serial console. The easiest way is to use raspi-config, and disable Serial under Advanced options.

The keyboard mapping is for a US keyboard. Other keyboards may need changes to the key map table in the overlay.

There is no reverse communication from the Pi to the keyboard so the keyboard lock lights do not function.

These instructions have not been exhaustively tested. If you find any errors, please email me at my gmail address mincepi.

Here are some links to sites that helped me in this project: