Although I’ve seen many pictures of PS/2 keyboards plugged into iPhones on the Internet, no one has yet published a detailed howto on how to get this working yourself. Until now, that is.
In this article I will show you how you can make a PS/2 keyboard to iPhone converter, including all hardware and software instructions. As the bugs get worked out, or improvements are made, I’ll update this page.
Motivation: The Great Apple-Elf Conspiracy
After using an iPhone for a couple years, I have started to believe that the developers of the iPhone actually hate those of us with broad, round thumbs. I can’t make very efficient use of the virtual keyboard, even in landscape, and they refuse to add real support for Bluetooth or really any external keyboards. I have a friend with pointy little thumbs who can type like 36 wpm on the thing. I’ve also noticed that of all the Apple employees I’ve actually met, they all seem to be relatively nimble and elf-like, with pointy little thumbs. Coincidence?
Perhaps if Apple employed more broad-thumbed people, they wouldn’t be so stubborn about actively disallowing using external keyboards on the iPhone.
If you are wondering why someone would want to be typing quickly into an iPhone, I can say with great certainty that you are on the wrong web site. Unless you got here by searching for “Elf Conspiracy”, then you should email me. Please include a picture of your thumbs for verification.
- Jailbroken iPhone – The official SDK doesn’t allow access to the serial port due to the Elf Conspiracy, so the jailbreak is required. All you need to know about jailbreaking is at the iPhone Dev Team Blog.
- Arduino Diecimila or Dumilanove (or clone), available from many places. This howto assumes that your Arduino is assembled.
- An iPod Breakout board, like this one from Sparkfun. Any similar product will work.
- A Female PS/2 (Din 6) Keyboard connector. Pull one off an old computer or buy one from Digikey or similar vendor.
- Wire, preferably several colors and about 24 gauge. I’m using a spool from RadioShack in the pics below.
- One 500k Resistor. I’m using an axial through-hole resistor from RadioShack, but you could add this to your Digikey order as well.
- Soldering Iron and Solder. If you don’t know how to solder, you can learn here and here.
- (Optional) Pin Headers, like these. I break these off and solder them to the ends of wires, so that they plug nicely into the Arduino.
- (Optional) Sweet 9v Battery harness so your Arduino becomes portable available from the Maker Store.
- (Optional) Stereo Headphone Jack from RadioShack or Digikey.
Wiring Things Up
Once all your ingredients arrive, fire up the soldering iron and have a quick look at the iPod Connector Pin-Out.
We’ll be making use of four of these pins for the keyboard: 11, 13, 16 and 21. The definition of pin 21 says we need to place a 500k resistor between pin 21 and ground to enable serial communications to the iPhone, so we’re going to stick a resistor between pin 21 and pin 16, which is the serial ground. We’re also going to be adding pin headers to pins 11 and 13, which will act as the TX and RX pins for serial communications. Since we’ll ONLY be sending to the iPhone, we only make use of the iPhone’s RX pin, so we’ll also be adding a piece of wire to connect pin 11, the unused TX pin, directly to the ground at pin 16.
You can also optionally add the stero headphone jack to pins 2, 3 and 4 right now and it will work whenever the breakout is plugged in. This has nothing to do with the keyboard, but if you have a 1G iPhone with the lame non-standard headphone jack, this will fix it.
Here is the front of the fully wired-up breakout board. Click on the image to have a closer look.
The backside is below.
I’ve put the jumper between pin 11 and pin 16 on the back of the breakout board. It can be a bit tricky to fit everything on to pin 16, so I recommend first sticking the resistor through the hole, then wrapping the end of the jumper wire around it and soldering them together before clipping off the resistor lead.
Another tactic which may be useful here is to first tin the ends of your wire, then blob a little solder on the hole you want to stick the wire to. Use the side of the soldering iron to warm your blob of solder and the SLIDE the tinned end of the wire into the blob. Remove the iron, let blob cool, then let go of wire.
There is no path you can take through the pin numbering of the PS/2 Connector that makes any kind of sense outside the context of the deranged ramblings of a committee meeting. Don’t think too hard about the pin numbers, just check out the picture above.
We’ll be making use of four pins here, and connecting all of them to the Arduino. Ground will be wired to Ground on the Arduino, VCC will be wired to 5V, and the Data and Clock pins will be brought over to two of the Arduino’s digital pins (3 and 4).
The bottom of the PS/2 Connector is even less intuitive than the numbering scheme. I recommend checking this picture, but also verifying that the pins on the bottom of your connector match the numbered pins you think they do before soldering anything. You can check for connectivity with a regular multimeter by setting it to measure resistance and connecting one probe to the pin on the bottom and sticking the other in the hole. If there is any resistance at all, then that pin is connected to that hole.
I’m using the Green wire for the Clock pin, the Red wire for VCC, the White wire for Data, and the Black wire for ground.
Once you’ve got the connectors wired up, strip the other ends of the wires and solder the leads to pin headers. This will let you plug them in to the Arduino easily. If you didn’t get the pin headers, you can try carefully tinning the wires to make them stay in the Arduino pin holes better.
I attached some wires to the pin headers on pins 11 and 13 of the iPhone breakout board. The Black wire is to Ground (pin 11) and the Red wire is to VCC (pin 13). I’ve then soldered pin headers to the leads for the two wires from the breakout board and the four coming from the PS/2 connector.
I’m using an Arduino Diecimila, since this program doesn’t require a better chip. This is shown with a 9V battery harness from SparkFun for portability.
Next, we connect the PS/2 Connector to the Arduino. Connect the Clock wire to Digital Pin 3, the Data wire to Digital Pin 4, and connect the Ground wire to Ground on the Arduino and the VCC wire to the 5V pin.
To connect the iPhone breakout, simply connect the lead from Pin 13 to the TX Pin on the Arduino (Digital Pin 1) and then connect the lead from Pin 11 to any available Ground on the Arduino.
IMPORTANT: To avoid trouble with flashing the Arduino, please disconnect the TX Pin on the Arduino before flashing. More on this later.
After that, all the soldering is done. Now it’s time to move on to programming the Arduino!
The Arduino Code
The Arduino software clocks data out of the keyboard, translates the keyboard scan codes to key codes, and handles presses of the shift and caps lock keys.
First off, download and install the Arduino development environment from here. Follow the directions on the site, but be sure to install the appropriate FTDI driver from the drivers directory in the Arduino installation.
Next, you’ll need an additional Arduino library for PS/2. Download the file “ps2.zip” from this page. To install, unzip the download to a folder and move that folder to be a subdirectory of the “hardware/libraries” directory under your Arduino installation. On OSX, you can go to Arduino.app and “Show Package Contents” first.
Once Arduino and the ps2 library are installed, download the source code from here. Open the Arduino application, create a new project, and paste the source code into it. Save, and then go to Sketch->Verify/Compile to make sure that it builds. If it doesn’t, make sure the library is installed correctly.
On a side note, I actually wrote absolutely no code for this project. I started out trying to use the PS2KeyboardExt2 library, but that library is based on interrupts and while it can run on an Arduino that is also speaking serial at 9600 bps, once I cranked the serial up to 19200 bps, the interrupts stopped working in a stable way. So I yanked all of the nice code out of PS2KeyboardExt2, including the key definitions and the nice handling of shifts and caps lock and reworked it into a program that doesn’t use interrupts and makes use of a different, much simpler PS/2 library. This makes it capable of handling 19200 bps serial in a reliable manner.
Now, to program the Arduino!
Disconnect the lead going to Pin 1 on the Arduino. Then, connect the Arduino to your computer via USB cable. You may need to restart the Arduino application so that it detects the new USB serial device correctly. Load the saved sketch with the source, and then hit the Upload button to program the Arduino.
Once the program is uploaded, plug the keyboard into the PS/2 connector. You should see the lights flash. You can open up the Serial Monitor in the Arduino application and try typing some letters on the keyboard. You should see those letters show up in the Serial Monitor. Try turning the Caps Lock on and off, the light on the keyboard should go on and off and the characters should come out correctly capitalized.
The iPhone Client
Now to set up the iPhone client program.
For the client code, I’m using a program I found on Anthony Pray’s Google Code page. This program reads input from the serial port at 19200 bps and then injects the appropriate keyboard event using a VNC client library. I’ve made a local text-only mirror of the source here, which can be easily fetched with wget.
Since we’ll be injecting keypresses by VNC, you’ll also need to be running a VNC Server on the iPhone. We’re going to use Veency (which is awesome and you should install anyway).
Go to Cydia or Icy and install the following packages:
- Veency – Provides the VNC Server, configure it to run at startup
- LibVNCServer – Provides libvncclient
- iPhone 2.0 Toolchain – Provides a build environment (gcc, libgcc, ldid, libz are required if you go another route)
- MobileTerminal – So you can access the iPhone terminal
- wget – So you can pull down the source file
Now to download and build the source. Either open up MobileTerminal or SSH into your phone, and then do the following:
- wget http://awgh.org/files/TouchClient.c
- gcc -static-libgcc -o TouchClient TouchClient.c -lvncclient
- ldid -S TouchClient
The last step, using ldid, fakes signing the binary. Without it, the iPhone OS will kill your process immediately.
To start the program, run it from Mobile Terminal with:
This will cause Veency to pop up a dialog asking if you want to accept the VNC connection. Hit Accept.
I’ve noticed that running this from MobileTerminal keeps the program alive even when you leave MobileTerminal, but it would be better to use launchd. I haven’t figured that out just yet.
Finishing Touches and Future Work
To finish up, reconnect the lead from Pin 13 on the iPhone breakout to the TX pin (pin 1) on the Arduino. Disconnect the USB cable and switch the Arduino to external power. Plug the iPhone breakout into your iPhone.
- TouchClient is running on your iPhone
- The PS2 software is running on your Arduino
- The Keyboard is plugged into the connector, and the connector wired to the Arduino correctly
- Veency is running and you have accepted the connection from TouchClient
- Nothing else is screwed up
You should be able to type on the PS/2 keyboard and have those keystrokes translated to the appropriate iPhone keystrokes. This will work anywhere in the iPhone, in any app or native feature.
This solution isn’t perfect, but it’s a general-purpose approach to using the Arduino to add PS/2 keyboard support to almost anything that can read simple serial messages.
- The scan code mapping could be moved entirely to the iPhone, and the hardware part of this project could be reimplemented on a much cheaper and lower power consumption chip, like a PIC. This would reduce the cost of the unit by about $30, although it would require a PIC programmer.
- Not all special keys are correctly mapped, but if you look in the Arduino code and then at the iPhone code, you’ll see that this is an incredibly simple process. Please post any changes you make back here as a comment!
- Rather than being a crazy wire hack, I’d like to see this fit into a snug little enclosure for real portability. Any ideas in this department would be appreciated.
- I would like to give TouchClient a password for Veency so that there isn’t that annoying Accept/Decline pop-up.
- This exact method could be used to add a Bluetooth keyboard. Simply add a Bluetooth-Serial module to the iPhone breakout and keep using TouchClient & Veency.
That’s it. I hope you enjoyed the howto! Looking forward to your comments.