Building The ZX Raspberry – Part Two

In which our hero enters the (keyboard) matrix…

Ok, time to burn stuff. After hatching a plan in Part One, I’m now ready to proceed with my Frankenspeccy. But first, some more planning – what am I going to build? Well, this… 

Click to Embiggen

Click to Embiggen

I need to connect all 13 lines from the Speccy’s membrane to the microcontroller so it can read them. This will be done by outputting current on the address lines (the group of 8 or KB2) and reading the results on the data lines (the group of 5, KB1). Due to a problem with reading currents known as floating, I need to use pull-up resistors on the data lines. This has been taken from the original Instructables article but since them I’ve realised that the Pro Micro may well have built-in pull-up resistors on the chip as the Arduino Uno does. In other words, this might not have been a necessary step. Still, won’t hurt. Update: I was correct, see Part 4.

A pull-up resistor setup means switches work backwards to how you would normally think. If you detect a high state on the microcontroller, the button is not being pressed. If low, your button is cutting off the supply and is considered pressed. A tad counter-intuative but it makes life easier.

5V from the Pro Micro is sent to the resistors and then back into the inputs. So, everything initially registers as high current. The KB1 membrane ribbon is also connected to the stripboard. If it connects with a line on KB2, i.e. a button is pressed, it will draw the current to the address line and the data pin will read low. A key has been pressed. Our controller will scan each data line on each address line to detect keypresses. A scan of 40 combinations in all (8 x 5). Even this cheap piece of hardware will be able to do this several thousand times a second.

On the address lines you’ll notice a row of diodes. I’ve added this as a test build of the original circuit revealed a problem: no more than one key could be pressed at once. This was due to a restriction in the software design and also a short-circuit that could be caused by pressing more than one key on the same data line. One would ground to the other’s address line. This is easily fixed by only allowing current to flow in one direction, i.e. line up some diodes.

When Caps and P are pressed together, a short occurs so nothing flows to the data line

When Caps and P are pressed together, a short occurs so nothing flows to the data line

The final bill of materials is:

1 x ZX Spectrum case with keyboard membrane (Do NOT hurt a working Speccy. I’ll tell on you.)
1 x SparkFun Pro Micro (or equivalent)
2 x Pieces of stripboard large enough to accommodate the components and keyboard connectors
6 x 10k resistors
8 x Diodes (e.g. 1N4148)
1 x ZX Spectrum Keyboard connector pair (can be found on eBay or harvested from a broken Spectrum)
1 x Push-to-make momentary contact button
Lots of ribbon cable or wire

Common-sense disclaimer time. Should you wish to undertake this project I am not responsible for anything you do to your Speccy (or anyone else’s for that matter) or yourself, your family and small pets. If you are not confident in what you are doing, do not proceed.

The first step was to give the casing a darn good clean, then cut up two pieces of stripboard; one for each membrane connector. It is very important you carefully work our where to place these on the case, as the ribbons from the membrane must not be put under any stress, such as meeting mortgage payments or awkward social situations. Ideally, take your cue from a ZX Spectrum motherboard and line them up accordingly. My KB1 connector ended up a little higher in the case as the ribbon had been trimmed. I then marked out the position using some masking tape.


Measure Twice and all that…


The ZX Spectrum keyboard connectors are very delicate and chances are you’re using old ones that have been harvested. Make sure everything is clean and, if required, manipulate the contacts carefully so they make a good connection with the ribbon. Most important of all, make sure you solder them into position the correct way around! Only one side of the ribbon has its contacts exposed and these need to match up with the flat side of each connector.


The ZX Spectrum keyboard connectors


Soldering the two stripboards is straightforward enough, but here are some things to be aware of:

  • Make sure you cut the tracks between the resistors and the diode, otherwise they won’t be much use
  • Remember that the 5V line needs to go to all the resistors, so short out the lines on the stripboard with some wire or solder blobs.
  • Work out where the Pro Micro is going to site and make sure you have long enough cable (too much is good, you can always cut it back)
  • Measure, measure and measure again
KB1 Assembly

KB1 Assembly

Solder each line to the Pro Micro (this is a little more delicate) as shown in the diagram. Input names can vary (this is the SparkFun version) on different controllers. So long as it’s going into a pin that can be configured as a digital input, you’re good.

I had a few shorts on my first attempt (the joy of cutting tracks on stripboard) so be sure to go over everything with a multimeter.

You’ll also notice another component: A button. Early on I realised I had a problem. In order to use the emulator I had in mind on the Pi I was going to need the use of function keys (F1, F2 etc). The ZX Spectrum keyboard has no space for these and I wanted to keep all original functionality in place. I decided to build in a keyboard ‘mapping’ mode. By detecting the switch being pressed (just as if it was an extra key) I could toggle which mode the software was in and effectively have two or more keyboards. Pressing the button will take it into ‘special key’ mode which would re-map keys to whatever I wanted.

The button assembly is identical to the keys except that it goes straight to ground. The Pro Micro has a few spare ground contacts.

A Completed Mess Of Wires

A Completed Mess Of Wires

Once all soldered together I very carefully attached the original keyboard membrane to the connectors and hooked up the Pro Micro to my Mac. Of course, nothing happened because I haven’t uploaded any software to the microcontroller yet. Still, nothing went bang, so that was nice too.

I would like to boast that I got the software up and running in minutes and all was well. Err, no. It took four complete re-writes of the code before I finally figured everything out. I’ll spare you the gory details here but it turns out writing keyboard controllers is not the walk in the park you’d think. The biggest hitch was the Raspberry Pi being very sensitive about how much information was pushed through it. Code that performed very well on my Mac would stutter or break altogether on the Pi. The less ‘chatty’ I made my code, the better it got.

Anyway, here is the final (yeah, right) sketch. After initial setup, if performs two nested loops. The first selects one address line and sets it low, allowing it to receive current. The nested loop goes through the five data lines looking for a low signal. If one is found, I now know the data line and address line it is on. A simple array mimicking the ZX Spectrum keyboard matrix allows me to map it to a letter which I can output to the USB master via the insanely easy-to-use Keyboard object in the Arduino IDE. To allow for multiple keypresses (essential for gaming) I had to track each individual key state and detect releases as well as presses. So, the keyboard is as ‘real time’ as possible.

If you want to play with this (and please, be my guest) take a copy and have fun. I’ll probably continue to tinker too. Either way, it’s just a matter of cutting and pasting the above into a Sketch in the Arduino IDE and uploading it to the Pro Micro.

Lots of testing next. I made sure all the keys worked (obviously) but also that things like extended mode would work correctly in ZX Spectrum emulators. It took a good few days work to get it all perfect.

If you’re only after a USB keyboard, then all there is left to do is to glue or stick in the stripboard at your marked locations, mount the key mode button somewhere useful (if you’re going to have your regular keyboard to hand, you don’t really need the button anyway) and plug in a USB cable to the Pro Micro (I got an angled one for a snug fit). Reassemble the ZX Spectrum and you’re done.

Completed keyboard - just add USB

Completed keyboard – just add USB

But that’s not my tempo.

Next, it’s time to get the Speccy an ice cream. Raspberry flavour.

Part 3…