Arduino as a Keyboard–working example April 2012

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.

tmp7E03The software listing indicated I should short an input to ground on the board… and sure enough "Hello world" appeared in Notepad on my PC – problem solved.

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.

tmpCF8BThe 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.

DIY Arduino or “UberBareBoard”

If you’ve been reading my other blogs recently you’ll know that I’ve rekindled my interest in electronics, once blown away by our government and their anti-small-business gold-plated CE regulations. The good thing about hobbies is that you don’t have to CARE about such things.

And so it was that I started looking at processor boards out there to make use of the popular Arduino design. I say design, it’s really very little more than a manufacturer’s minimum demonstration – but the important thing is the volume of support software out there – in particular ethernet software making it possible to create Internet-enabled devices such as my “EtherStat” unit which has now been on test for the past couple of weeks without a hitch.

UberBareBoardNone of the boards out there are without issue and so three of us collectively decided we’d have our own board and I took on the design job.

Armed with Eagle PCB package with which I’m well familiar (though I had some catching up to do) and a little imagination I set off putting this together -  I should at this time thank friends Jonathan and Aidan for the encouragement and also the latter for checking my work and generating the “gerber” files etc.

The result – here it is..I nicknamed it ”UberBareBoard” as it has everything except the kitchen sink in it (feel  free to zoom in on any of these images which should expand or “lightbox” if you click on them).

The board follows the same form factor as the original Arduino though that was only by chance as I spotted an open-source version of the board outline and connectors. Having put several commercial boards together into various test projects, I came across the same problems every time – not enough power connectors, regulator getting hot, parts too close together, same pins used multiple times- and so what you see here is fairly simple but with sufficient extra pins to make life easy for lash-ups.

In the bare-board image above you’ll see on the front-left a 10-way connector – that’s for the cheap Chinese radio boards. Lower left there’s a 6-way connection – that’s pretty standard as are the outer connectors giving access to the 19 I/O pins and power and ground. Note top left the regulator is mounted flat on copper board to dissipate heat and there’s room for a standard power connector (standard that is unless you’re Maplins who have a HOPELESS and hopelessly over-priced selection of connectors).

Populated UberBareBoardAt this point it’s probably better to look at the populated board. Front right you’ll see a LED which is attached to D13, a far better place than making a power led as you can see the board working on power up (it flickers). Over on the right I’ve left room for a 3-pin infra-red receiver and a LED to go with that. Top right is the reset button, left from there the standard programming connector and left again (ie upper middle) you’ll see a power (+5 and GND) connector and a 2-pin battery connector. Relevant diode and charge resistor are fitted. The empty socket is for a 24c256 chip (64K EEPROM) and centre front you’ll see 2 diodes, they’re to drop the 5v supply sufficiently to use with the radio or Ethernet modules.

Mass production? No, we scoured the web for cheap UK prototype suppliers and ended up sending off to China for a set of 10 prototypes at a reasonable price – somewhat over a fiver each – and this for fully tinned, masked, cut to size prototypes of production quality. I’ll be using these guys again – turnover was little over a week.

I’m happy to say that apart from not having sufficiently small 1k resistors to mount them horizontally (that’s what I get for using standard library parts and not checking) and getting the mounting hole layers wrong (hence no mounting holes, quickly corrected with a drill) the boards work perfectly – and I have lots of plans for them.

DIY Arduino

While waiting for the first Raspberry Pi to arrive, I’m continuing to work with Arduino clones. I use the name repeatedly just as it’s familiar to those in the field, but essentially we’re simply talking about a minimal ATMEL-based processor board with reset and power components. Having discovered that the Chinese do prototype boards quite cheaply and quickly I figured I’d have a go at doing my own. The Eagle PCB package is free for anyone wanting to have a go up to a certain size board. I’ve used this package over the years but not for a decade so it’s nice to see they’re still in operation and obviously they are very popular with the hobby community.

Anyway, here’s the board so far. This is a “jack of all trades” board as I put stuff in that “might come in handy”.

As you can see, it has it’s own proper 5v regulator (fastened down with some heatsink area) and power connector, standard Arduino-type general connectors and the usual microchip, xtal and a crude 3.6v supply via a couple of diodes. The connectors include the normal programming connector, ICSP and one (lower left) specifically for the cheap Chinese RF modules. ~I’ve also put in a 24c65 socket there as the Atmel chip has only 1k of EEPROM for storing logs etc. and the socket I’ve added lets you add a 24c65 or 24c256 chip for up to 64KB of logging or other storage (can’t use for programs).

The next step is to find out the realities of actually getting prototypes made in China. I looked all over the UK and I might be missing something but there’s no-one here to even remotely compete on prototypes – these work out at around a fiver each for 10-off which means if I’ve made a mistake it’s not going to break the bank – but it’ll be weeks before they turn up – so more information at the time… I’ve jokingly called the board “UberBareBoard”… I’ll find out how super it is (or not) when they arrive.

 

MyDuino !

 

On the subject of iPads -  has anyone noticed the utter lack of anything new and exciting recently – same with iPhone – it’s as it people are running out of ideas. A good old fashioned (but not old) adventure would be nice, not one of those silly “guess the hidden objects” but a proper adventure…

Arduino and I2C–the 24C65

The 24c65 is a great little 8-pin serial EEprom chip with 8k of  EPROM in it – so if you want some extra memory to store stuff in your Arduino board or similar – this would appear to be a nice chip – except that the standard Arduino library uses 7-bit addressing.

WHAT????? Yup, it’s for small devices with a few memory locations – whereas accessing several KB of addresses needs a 16 bit address! Also the Arduino library is a tad, erm, LARGE so off I went to find a replacement.

I stumbled on this… http://dsscircuits.com/articles/arduino-i2c-master-library.html  and that’s nice, a tight library for controlling EEPROMs but again this does not handle the larger variety… but down at the bottom of the blog, somewhat hidden in with my stupid questions is this… An EEPROM library with 16 bit addressing. Not only THAT but if you compile it into a little “Hello World” writer and reader, you are looking at 3234 bytes including the library. NICE!

Check out this code – it’s a bit rubbish as the printout doesn’t check for zero at the end of the string…. but it works… there is also the 24C256 which gives you 64k to play with…. that’s a lot of logging!!!  I’m sure it would not take too long to write some wrapper routines around this lot. Incidentally the chips have 3 address lines normally tied to ground – so you can fasten up to 8 of them together – so addressing half a meg of data would be no big deal and with a tiny library.

By default internal pullups are enabled so you don’t even need resistors if the leads are short – just power (will work regardless if you’re on 5v or 3v3), ground and 2 pins – that’s A4 and A5. What could be easier. Incidentally you DO need that delay after the first write (which takes much longer than a read – or the second one will fail – there is no check that I can see. (I tried I2c.available() and it just locked up). You can write up to 32 bytes at a time as that is the internal buffer size of the chip – if you wanted to write more, a simple function call would easily split the data up.

 

#include <I2C.h>

void setup()
{
Serial.begin(57600);
I2c.begin();
I2c.setSpeed(1);
I2c.write(0x50,1000,"Replaced here");
delay(10);
I2c.write(0x50,2000,"and replaced here");
delay(10);
}

void loop()
{
Serial.println("Starting");
I2c.read(0x50,1000,30);
while (I2c.available())
{
Serial.print((char)I2c.receive());
}
Serial.println("");
I2c.read(0x50,2000,30);
while (I2c.available())
{
Serial.print((char)I2c.receive());
}
Serial.println("");

}

Testing lights

This is kind of neat… British as well – simply a little board with half a dozen LEDS on it…and resistors – and pins off the bottom. Next time you’re testing that single-chip micro project and you need a bunch of test lights-  this sure beats soldering a bunch of them up on Veroboard…

 

You get 8 of them on a board the size if a chip – 0.11” spacings… for around £4 or so – check out http://www.ebay.co.uk/sch/majenko/m.html?item=190656722359&sspagename=STRK%3AMEWNX%3AIT&_trksid=p3984.m1439.l2648&_trksid=p4340.l2562

 

tmpC0A6

I wouldn’t care, but it turned up just after I’d soldered a bunch of LEDs to a board to do some tests!!!

 

Peter Scargill.

Much Cheapness

I’ve christened this version of the Arduino “CheapDuino” – I do hope someone hasn’t registered the name…

The idea here was to see how cheaply I could put one of these together for and find out how easy or otherwise it might be. The “Veroboard” was the most expensive and that’s because Maplins make an obscene mark-up and I was desperate. I’ve a load more coming from China but I could not wait (usually takes around 2 weeks).

CheapDuino

What you see above (excluding the green Ethernet board on the left which is yet to be mounted) is a working Micro. I’ve put a few of these together and this is pretty much the minimum that is practical to use. Firstly, you can never have enough ground connections so I’ve put 3 of those, 3 of the 5v connections – and a 3.5v (approx) out as many peripherals run on 3v3 or thereabouts (like the Ethernet board). There’s an input pin over on the right.  I had to cheat a little – there are 3 wires and one decoupling cap underneath (for the 3v5 output).  If I’d had SLIGHTLY wider board I could have gotten the lot on the top. The board came with split track which made life easy – I had to cut only 3 tracks underneath.

This board ( call it Arduino but in fact all it has in common is the same BOOT code and compatibility) when it’s done will form the controller part of my Internet Thermostat (the other part has a similar micro and an LCD display but draws power from the main board, fed only by a 4-core telephone lead).  As to costs, you’re probably looking at £8 tops if you avoid Maplin for the board… the Atmel 328 chip was the most expensive item, £2.99 un-programmed and I’ve put together a simple programmer which simply fastens to the 60way connector on the (unpowered board) to flash the chip – once done all it needs is a 9v plug-in-the-wall and an Ethernet connection.

Things have come along – but only recently – since I used to play with PIC chips many years ago (I still have an award stuck on the wall) it seems people are still messing with those things – and until recently the libraries of code for the Atmel were pretty dire but today, with a little R&D it’s quite possible to perform miracles with these – talking to the Internet is one of them – add in the cost of the Ethernet board from China and you’re still not much over £12 – which puts this in a different ballpark to the likes of the Raspberry Pi (if Farnell ever get their act together).

This little fellow has some work to do before it ends up in my main project – I need to test the WATCHDOG and several other things to see how reliable they are – but it’s nice to know that you can knock something like this up in maybe an hour if need be at very low cost.

For reference, the unit has 32K of Program memory, 2K of RAM, 1K of EEPROM and  host of timers, A/D convertors, hardware interrupts etc all thankfully made pretty easy to use thanks to the freely available Arduino IDE. Oh, and 13 digital lines and 6 analog inputs….