Arduino, RFID and AS3

As part of a mini side project I’ve been working on lately I wanted to get a desktop Adobe Air application reading RFID tags with Arduino. I’m by no means an expert at all this, but I learnt a lot while doing this so I thought I’d pass the knowledge on.
Ingredients:
These are the elements I used to make this all work:
- Arduino (I got mine from Oomlout, its a good little starter kit)
- RFID module (I used the Seeed Studio ‘RDM 125KHz card mini-module‘)
- At least one 125KHz RFID tag (I used this one from SparkFun)
- AS3glue (You can download that here)
- Flash
Step 1, Wiring up the RFID module to the Arduino:
This part is simple, especially if you have a breadboard. Basically there are 3 parts to the RFID module, the Data/power, the antenna and a light for when it reads a valid tag.
Here’s a diagram of the connections from the RFID module:

Connected to the Arduino:
Once this is all connected you can test its working by scanning your RFID tag, the LED should light up when it’s read.
Step 2, The Arduino code:
Now we need to write the code and upload to the Arduino. Like the wiring, it’s actually fairly simple. When the tag is read by the RFID module the tag’s id is sent to Arduino as bytes. We just need to loop through those bytes and forward that id to the computer using Firmata. All the code for Firmata is already added with the Arduino software, so you just need to include it in your code.
The code to do all this is below, simply upload it to your Arduino:
#include #include #define rxPin 2 #define txPin 3 char code[20]; int val = 0; int bytesread = 0; SoftwareSerial RFID = SoftwareSerial(rxPin, txPin); void setup() { Serial.begin(9600); RFID.begin(9600); pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); } void loop() { val = 0; bytesread = 0; while(bytesread < 12) { val = RFID.read(); if(val == 3) { break; } if(val != 2) { code[bytesread] = val; bytesread++; } } if(bytesread >= 12) { Serial.print(START_SYSEX, BYTE); Serial.print(code); Serial.print(END_SYSEX, BYTE); } }
If that all goes well you should see something like this in the serial monitor when scanning a RFID tag (the tag’s id is between the first and last characters, so in this case the tag’s id is 4500B89EACCF):

Step 3, Going from Arduino to Flash/Air:
I used the AS3glue library (download here) to handle the communication between Arduino and Flash, but it can’t receive message directly from Arduino- that’s where Serproxy comes in.
You’ll find Serproxy in the AS3glue zip file and you’ll need to amend some settings before it’ll work. You can edit the settings by opening serproxy.cfg in any simple text editor. Its well annotated so it should be simple to setup for your system but there is one thing you’ll need to change differently to the recommendations. comm_baud needs to be set to 9600 (not 57600). Also you will probably want to set the timeout to something higher, I went with 86400 (24 hours).
For the AS3 code you need to import net.eriksjodin.arduino.Arduino and net.eriksjodin.arduino.ArduinoSysExEvent. Then create a new instance of Arduino, add an ArduinoSysExEvent.SYSEX_MESSAGE event listener and read event data to get the tag. Simple as that!
package { import flash.display.MovieClip; import flash.events.Event; import flash.events.IOErrorEvent; import net.eriksjodin.arduino.Arduino; import net.eriksjodin.arduino.events.ArduinoSysExEvent; public class RFID extends MovieClip { private var arduino:Arduino; function RFID() { setup(); } public function setup():void { arduino = new Arduino("127.0.0.1", 5331); arduino.addEventListener(Event.CONNECT, onSocketConnect); arduino.addEventListener(IOErrorEvent.IO_ERROR, onSocketError); arduino.addEventListener(ArduinoSysExEvent.SYSEX_MESSAGE, onReceiveSysEx); } private function onSocketConnect(e:Event):void { trace('Connected to Arduino'); } private function onSocketError(e:IOErrorEvent):void { trace('Error connecting to Arduino, is serproxy running?'); } private function onReceiveSysEx(e:ArduinoSysExEvent):void { var newCode:String = String(e.data); trace("Received tag: " + e.data); } } }
You’ll need serproxy running for it to all work and when flash connects you should get this message:

Then when you scan your RFID tag Flash should trace:
Connected to Arduino Received tag: 4500B89EACCF
All done!
Well I hope that was easy enough to follow. Have fun!
My name is Steve Gardner and I'm a Lead Front End Developer at Jigsaw.
I work with stuff like Flash and HTML5 to make things like games, mobile apps and websites. I also occasionally play around with Arduino.
This blog serves no real purpose except to spread joy and happiness to every single human being on the planet and, on occasion, discuss things I make and find on the internet.






Comments
What's that you say?