One of the things I wanted to do right at the beginning of learning about the ATMEL microcontrollers (Arduino etc) was to make USB devices – that is, devices that appear to a PC as a USB gadget, for example a keyboard. There are many uses for such devices if they are simple and cheap enough – a little box to insert your username and password for example or to time an operation regardless of what package the PC is using – sky’s the limit, if you can do it.
Sadly when scouring the web for the necessary library I first found stuff that was frankly "academic" by which I mean so incredibly complex you were losing the will to live trying to do something really basic like "hello world". Some people really haven’t grasped the idea of creating “re-usable components”.
Then I hit on what looked like pay-dirt. A general USB library – including a keyboard library that needed just a few lines of code to make a “hello world” keyboard emulator for example. Well, of course, it didn’t work that way, the library was written in 2008 and NO WAY would the code compile in the current 2012 Arduino IDE… I ended up abandoning the effort, disappointed.
Then this evening, I’d given up for the night and was reading "Practical Arduino by Jon Oxer" (I know, sad but hey, this is how I learn and how I get to sleep, using my spare moments to read up on the technology) and I noticed the same library again… sure enough, I followed the link, grabbed the library and sure enough, it failed to compile.
And then I looked at the bottom of the web page, a little note from 2010, some minor changes. I keyed them in and VOILÀ – code compiled… so much for the early night. I got out of bed, grabbed a USB lead, ripped it to bits and added the extra components and, taking my life into my hands, plugged the lead into my USB hub. After a few second’s silence (which seemed like ages waiting for smoke to appear) the PC announced a new keyboard had been plugged in. Wheeeeeeeeeeeeeeeee.
So, what to do – if you’re reading this far, chances are you were looking for a solution. You’ve found it… This is April 2012 and the software works in the current Arduino IDE.
So – first things first – grab the library here. The layout is weird so don’t just dump it into Arduino as-is. Take the keyboard directory and put it in your Arduino library directory. Grab the keyboard demo file – and if you try to compile it will fail. Make the trivial modifications as detailed at the BOTTOM of this page…and you’re up and running. Note that you have to use pins D4, D5 and D2 – you can’t go changing pins… and on that same page are instructions – I basically took a standard USB to mini-usb lead, chopped off the mini end – which left me shielding and 4 cores.
The shielding I ignored and there are black (ground), red (5v), white (D-) and green (D+) leads.. you must add in the trivial components (I did it on a bit of Veroboard which include a couple of 68r resistors, a 2k2 resistor and a pair of 3v6 zener diodes – they MUST be small – ie 1.2 or 1.4w – as the larger ones have more capacitance and may hinder the USB.
I used 3v9 zeners as I had them handy but DON’T COPY ME – just because my hub has not blown up yet doesn’t mean to say it won’t. Use 3v6. We’re talking pennies for these components unless you go to Maplin.
So to test the USB keyboard software I used a fully formed Arduino (well, my own UberBareBoard as detailed elsewhere).. but really all you need for this is the chip, an Xtal or ceramic resonator, a decoupling 0.1u cap and… erm, that’s it, oh and a 10k pull-up for the reset.. you really could make a TINY board to do this. Oh, I guess you really need a pretty LED light as well, just to prove it’s on.
That’s it, it really was THAT simple – from getting off my backside and turning the soldering iron on to having “hello world” on the screen took no more than 15 minutes.
Now the fun starts – coming up with applications for this.