Sunday, June 3, 2012

Arduino as RFID Reader to HID Text Input

RFID is an interesting technology with a lot of potential uses for Arduino projects. Parallax RFID tag readers are a great RFID solution for use with Arduino because they're so simple to implement, plus they only need two pins to interface with Arduino. One pin activates the reader and the other transmits the code via serial connection. Radioshack has the Parallax brand readers on clearance. My local Radioshack actually gave me some for free because they couldn't find them in the system. Once I had a reader and tags, the first thing I did was use the software serial library to relay the RFID tags via serial to the computer. Receiving the tag via the serial monitor was interesting, but fairly limited in utility. I decided I wanted to relay the tag code as keyboard input so that it could be used for something such as a password, but that required the Arduino to act as a USB human interface device (HID).

I had a little trouble figuring out how to make the Arduino act as a keyboard so I decided to share what I've learned by posting an Arduino as HID device tutorial below. Check out the demo and tutorial below.

Demonstration:

Using Parallax RFID Tag Reader with Arduino:

The first thing I wanted to do was read in the tag codes via serial. The Arduino site has some great examples on how to use the Parallax RFID reader to do just that.

Arduino as Keyboard / HID:

Intro:
When I began to research using the Arduino Uno as a keyboard, I was surprised to learn that the chip that handles the USB connection for the Arduino Uno is actually a re-programmable Atmega16U2 or 8U2 (depending on Arduino Uno version) setup as a virtual USB serial device. The Arduino resets whenever a serial connection is initiated. During each reset, the Arduino bootloader checks to see if a new program is being transmitted to it via serial. It then proceeds to the setup function and finally the loop function of the uploaded code. The 16u2 or 8u2 used for USB on the Uno can be programmed to act as several different HIDs that receives commands from the main chip via serial instead of just relaying the serial connection over USB.

Reprogramming Atmega16u2 or 8u2:

Thankfully, the hard work of programming the USB connected chip to act as a HID has been accomplished by others. All you need to do is download the appropriate hex files from this site and flash it to the USB chip using a program from Atmel called FLIP. The only trick is that it needs to be in DFU mode. Just short the two pins identified in the image to enter DFU mode. The Arduino USB connection should re-enumerate as a different device as soon as you short the pins highlighted on the left.

One small word of warning: The downside of turning the Arduino into a human interface device is that the Arduino cannot be programmed using the Arduino IDE unless the USB connected Atmega is programmed to be a serial relay again. This page has more details about DFU mode and a link to the hex file that returns the Arduino to its regular state. The steps for using FLIP are displayed below:

After entering DFU mode (see above), select the USB connection.

Select the HEX file that you want to load. This should be Arduino-keyboard-0.3.hex or Arduino-usbserial-atmega16u2-Uno-Rev3.hex for example. These HEX files are pre-compiled instructions for the microcontroller that runs the USB connection. Thanks to the Arduino community for making this so easy by providing these files!

Once you hit run, these icons should turn green if everything worked. Once this is complete, disconnect and reconnect the Arduino to leave DFU mode and enter normal or HID operation.

That's all there is to it. If you have any question or you have any tips or corrections, please comment.

Arduino Code:

Here's the code I utilized in the demo. Remember, to program the Arduino with the IDE before using FLIP to make the Arduino show up as keyboard.

Thanks to the Arduino community for all the great info I found that made this project so easy! I would like to specifically thank darran from hunt.net.nz for sharing his Arduino as HID hex files used in this project. Please post any comments or questions below.