Close
0%
0%

ZX Keyboard

An Arduino based project to convert an old broken ZX Spectrum computer in to a USB keyboard.

Similar projects worth following
This is a project to convert an old ZX Spectrum in to a fully usable USB keyboard that will function with a PC, Raspberry Pi, or an Android device that supports HID devices via a USB host adapter.

More about the history of the hack and a background to the decision making process please see my blog. For the source code and other files please see the GitHub repository. (Links below)

he USB communication is achieved using the VUSB library from Objective Development that is wrapped in the VUSB for Arduino library. The USB hardware is based on a design from the book Practical Arduino and as all the hard work is being done in code is frighteningly simple.

The keyboard is a simple matrix with 5 columns and 8 half rows. Each column is pulled to ground one at a time while the others are left floating. We can then poll the rows (done in two block operations). As we are using the internal pull up resistors inside the Arduino we do not need any external components to do this. It is worth noting that we are using the analogue pins as digital pins but that is fine and how the micro-controller is designed to work. We also need to remove the LED from PIN 13 as this will interfere with the pull-up function.

The keyboard operates in two different modes. When you are typing normally the key presses are simply mapped on to the relevant HID code. When Caps Shift is pressed a shift “modifier" is sent along with the letters just as would happen on a normal USB keyboard.

Where it gets a little more complex is when we are pressing Caps Shift and a number are pressed as these are used for navigation, caps lock, and delete. When this happens we don't send any modifier and map the key to the navigation, caps lock, and delete respectively. If a letter and a number is pressed we then do send the shift modifier that may give an unexpected outcome on a PC, but that will always be the case without writing a special keyboard driver for the keyboard, and doing so will maximise compatibility with most spectrum emulators.

When Symbol Shift is pressed we enter another mode. This is because we need to pretend some symbols are typed with shift pressed, and some without. As a compromise we send a single press and release of each symbol that is typed and do not return to the normal mode unless the Symbol Shift key is released. We also send an unmodified alphanumeric key if there is not sensible symbol to map to for emulator compatibility.

The keyboard de-bounce is achieved by a simple delay of around 100ms that is an age in modern times but needed for the old membrane keyboards that physically bounce. We have had to disable timer0 as it causes the USB to behave unreliably and this breaks the delay() function, so we call delayMicroseconds() several times to do the same thing.

  • 1 × ZX Spectrum An original Sinclair ZX Spectrum with a working keyboard.
  • 1 × Ardunio Pro Mini A 5V ATMEGA328 based clone will work fine.
  • 1 × 2.2KΩ Resistor
  • 2 × 68Ω Resistors
  • 2 × 3.6V Zener diodes Must be rated at 0.5W or less

View all 7 components

  • VUSB Arduino Library Issues

    Alistair MacDonald03/10/2015 at 17:34 0 comments

    Some people have reported an issue with the VUSB Arduino Library not working with the latest release of the Arduino IDE. This appears to because of an error in the library that is ignored in the older Arduino IDE but will cause it not to compile in the latest 1.6 release. I can confirm that it definitely does compile in 1.0.5. This error has been fixed in the original VUSB library but this fix has not made it's way through to the Arduino port. It is not ideal but the quick and easy solution is to downgrade the IDE to upload the firmware.

  • "Direct" mode added

    Alistair MacDonald10/11/2014 at 15:32 0 comments

    I have just added a "direct" mode that does not map the spectrum keys to the ones expected from a standard PC keyboard like before. This is useless most of the time but essential to get the ZX Keyboard working with some Spectrum emulators. To enable the direct mode a hardware switch has been added to the A4. Short to ground to enable direct mode. Leave open to stick with PC emulated mode.

    When active all alphanumeric keys, enter and space are directly mapped regardless of any shift buttons being pressed. Caps Shift is mapped to the left shift key and Symbol Shift to the left control key. This works with all the emulators that the original emulated PC keyboard mode.

  • Debugging the USB

    Alistair MacDonald09/05/2014 at 09:45 1 comment

    When I was experimenting with the V-USB library I found it useful to monitor the message being sent in to the computer an compare them with a traditional USB keyboard. The best tool I found for this on the Windows platform is the HHD's Free USB Analyser.

    One word of warning in the installer can pull in some "crapware" that was caught by the virus scanner but I called the company and the site (separate from there main site) is apparently legitimate.

    The reason why I mention this now is that I have had some problems with Virtual Box and VMWare USB integration and tracked it down to the analyser. Uninstalling it fixed the problem and you can reinstall without issue when it is needed again though.

    Happy debugging.

  • ZX Keyboard tested with JSSpeccy

    Alistair MacDonald08/12/2014 at 15:47 0 comments

    I have tested the ZX Keyboard with JSSpeccy created by the demo scene genius Matt Westcott running on a PC. The good news, and somewhat expectedly, it works.

    The bad news is that, like Fuse, the key mapping I have done to get the Keyboard to work with modern operating systems has broken compatibility. I am now looking at implementing an emulator compatibility mode by using a hardware switch or key combination.

  • Videos Uploaded

    Alistair MacDonald08/12/2014 at 14:12 0 comments

    I have just uploaded a couple of videos. One is a quick demonstration and walk though...

    ...and the other is it being used with the Fuse, the Spectrum emulator running on the Raspberry Pi...

  • Installing a Spectrum Emulator on the Raspberry Pi

    Alistair MacDonald08/10/2014 at 17:14 1 comment

    As the origins of this project was to use the keyboard with a Spectrum emulator running on the Raspberry Pi I decided it was time to actually do this. I have tested the keyboard with the Pi, but until now have not installed an emulator on it.

    Some good news is the Spectrum emulator Fuse has already been ported to the Raspberry Pi and is easy to install if you are using Raspbian. To install the Fuse “SDL” version (that will run in full screen from the command lane) just log in and from the command line run the following command...

    sudo apt-get install fuse-emulator-common fuse-emulator-sdl fuse-emulator-utils spectrum-roms

    Next we should make it start in full screen. From the console type in “fuse-sdl” and the emulator will start. Next press F1 (yes, sorry, the ZX keyboard does not have an F1 key so you will need to use another keyboard for now), select “Options”, and “Full Screen”. To make this the default press F1 and select “Options” again, then select “Save”. To exit use the F10 key.

    Now I think we will need a good old game to play. The best place to look for Spectrum games that are distributed legally is at http://www.worldofspectrum.org/ . The simple way to load a game using the ZX Keyboard is from the command line. For example we can load Manic Miner (assuming we downloaded the game and extracted it to the working directory) with the following command.

    fuse-sdl --tape "Manic Miner.tzx"

    Annoyingly I have come across one problem with Fuse and the ZX Keyboard. Fuse maps each key to a Spectrum key, letter to letter, number to number, shift becomes Caps Shift, and Ctrl become Symbol Shift. All other keys are ignored. This is a problem as we are interpreting and translating the symbols already. For example the Symbol Shift P as a double quote (a shifted 2), but the emulator treats this as the Caps Lock being pressed. We could make the ZX Keyboard work flawlessly by turning off our translation, but we would lose compatibility with PC software. I might look at building in a way to changing modes but we will have to cope with this for now.

  • Close up shots of the breakout board

    Alistair MacDonald08/09/2014 at 21:24 0 comments

    Pedro has asked for some close up shots of the USB breakout board. I aim to please....

    I confess none of this has been my best work, but it works and that is what I was worried about.

  • Is this project done the wrong way round?

    Alistair MacDonald08/07/2014 at 15:10 0 comments

    Some have asked if I thought the project through and if I should have made a modern keyboard fit a ZX Spectrum. Well I will completely agree that the ZX Spectrum has among the worst keyboards in the world, and I will also agree that this project has little practical use.

View all 8 project logs

  • 1
    Step 1

    Remove the old workings from an old ZX Spectrum. (but never destroy a working one!)

  • 2
    Step 2

    Solder the keyboard connectors to the Arduino Pro Mini via some wires.

  • 3
    Step 3

    Build the USB adapter using the 5 components. A copy of the circuit diagram is in the GitHub repository.

View all 8 instructions

Enjoy this project?

Share

Discussions

Jeroen Adolfse wrote 06/09/2019 at 21:22 point

I wonder if the script to read the keyboard is adaptable to output a recreated zx spectrum output. Since there is an excellent ZX baremulator that runs on a RaspberryPI without even having to use Linux. It would be great to use an old DKtronix or Lo>>profile keyboard with the ZX Baremulator.

  Are you sure? yes | no

Bjørn Hesthamar wrote 02/19/2015 at 08:08 point

This is funny because of all the things the Sinclair computers were known for, "good keyboard" was never one of them.

  Are you sure? yes | no

Alistair MacDonald wrote 02/19/2015 at 10:45 point

The irony has not been lost on me either. :-) 

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates