Arduino and the RFID reader ID-12
December 8th, 2008 • Physical Computing
This tutorial is all about wiring the RFID reader ID-12 and retrieving RFID tags via an Arduino board. There are already a few good tutorials with working code out there, but all of them are quite basic and therefore I want to show you how to use it in more complex applications.
There are basically 2 major differences in comparison to other implementations:
1. software serial
The ID-12 uses a serial connection to send the tags. All the other implementations are using the hardware serial pin (RX) to retrieve the information and send it to the computer. The problem of this setup with the ID-12 blocking the receive pin is that you can’t send any data from the computer to the arduino board anymore. This implementation is using a software serial connection on an other I/O pin in order to maintain bidirectional communication.
2. RFID tag removed
The ID-12 has no native method to determine whether a RFID tag is still in range or has been removed already. With a little workaround you can get this information as well.
but enough talking, get this thing started:
Wiring

Connect pin 11 to +5V, 1 and 7 to ground, 2 and 9 to Arduino pins 4 and 5;
Arduino code
First of all you have to get the AF_SoftSerial library (you will need an Arduino board with an ATmega168) or NewSoftSerial (for ATmega328). Uncompress and place it in the hardware/library directory under your Arduino software installation.
After installing the AFSoftSerial library you can download my arduino code and upload it to your board. I don’t want to go through the code in detail, you can always leave a comment or get in touch with me if you have problems with it.
... void loop () { byte action = 0; unsigned int now = millis(); updateID12(false); // connection to computer established if(connected == true) { clearTag(rfidTagTemp, 6); // serial connection with rfid reader if (rfidSerial.available()) { // wait for the next STX byte while(rfidSerial.available() && action != 0x02) action = rfidSerial.read(); // STX byte found -> RFID tag available if(action == 0x02) { if(readID12(rfidTagTemp)) { nowLastRfid = millis(); rfidTagSeen = true; updateCurrentRfidTag(rfidTagTemp); } } } else if(rfidEnabled && rfidTagSeen == true && (now - nowLastRfid) >= RFID_REMOVED_TIME) { rfidTagSeen = false; updateCurrentRfidTag(rfidTagTemp); } } // serial connection with computer while (Serial.available() > 0) { // read action byte action = Serial.read(); switch (action) { // PING case 0x01: Serial.print(0x01, BYTE); // (C)ONNECT case 0x43: // clear serial while(rfidSerial.available()) rfidSerial.read(); connected = true; break; // (D)ISCONNECT case 0x44: connected = false; break; } } // delay 100 milliseconds delay(100); } ....
The first part of the code is for reading the RFID tags and checking if the tag is still in the range of the reader.
The second part handles the communication with the computer. You can define specific bytes as marker and accordingly get the value of different sensors, execute functions on the hardware or whatever you want, e.g. in order to use the RFID reader and get an output of the RFID tags on your console you have to send the connect marker which is a "C" or 0×43 in hex.
Update 08.02.2010 – Fix for the Atmega 328 chip
Finally a fix for the Duemilanove (which is using an Atmega 328 chip). There is a new Sofware Serial Library out there which works with the Duemilanove, it’s called NewSoftSerial. Just use this library instead of the old one and everything should work. Thanks Thijs for the information
30 Responses (Add Your Comment)
-
steeley February 23, 2009at 9:59 pm
-
Joeri Dehouwer March 30, 2009at 7:00 pm
I’m using your code in combination with a duemilanove and for some reason it stops working after a few reads. The ID-12 apparently still recognizes the tags (the LED blinks) but the data is not transmitted… Any idea why?
-
Thanks for sharing, it’s great help!
-
Hi,
I can’t seem to get the reader working using this code. I am using a Decimilia board and have the correct AFSoftSerial library but it doesn’t seem to work.
It connects when i send a “C” but then doesn’t seem to read the tag… its not getting into this if statement:
if (rfidSerial.available())
{
// read tag…
}I’m new to all this but do you have any ideas?
Many thanks,
Steve.
-
It definitely works as I had it working and reading rfid tags through the serial port. I used the example in the arduino playground.
It must be a problem with the AFSoftSerial library… but i’m not sure how to go about resolving it? It couldn’t be to do with the ide could it? I’m using the latest one downloaded from the liquidware blog – Antipasto Arduino?
Thanks for your help in advance…
-
Ah, I don’t think I used the PWM pins for the AFSoftSerial I just used pins 4 and 5 on the Deicimila… I’ll try using the standard IDE from arudino and have a look at the pins…
Thanks.
-
After a second (or more like 10th check) i realised had it wired up wrong! It works perfectly… thanks for humouring me…
Now to get working with the Stealth TouchShield and maybe a bit of ActionScript if I’m feeling lucky…
Thanks again,
Steve.
-
Superb! Exactly what I was looking for. Thanks for sharing this piece.
-
Hm, i have just tried this project on Arduino Deumilanove and it does not work. When it comes to this statement action = rfidSerial.read(); it reads zero only once and there is no connection to RFID anymore.
Any suggestions?
Thanks for sharing this…
-
Hi, i need something like this but i don’t know if this reader works for me.
I need to detect when a tag is remove from the range. This can be done reading continually tags but the tags have to be read as fast as posible.
Could you tell me how fast can you detect a tag? I mean how many times per second can the reader detect a tag. Thanks in advance and thanks for sharing this. -
Hi, thanks for your fast reply. I wonder if i could use it without any delay because i would like to get several readings per second.
Is the delay for your code or because it is the time the reader takes to restart after the reset?
Is it posible to read several times per second with this reader?
Thank you so much -
Ok, thanks
-
Gerrit November 3, 2009at 6:00 pm
Is it possible to read multiple tags at the same time using the ID-12?
-
Hi,
I build the your example using a Diecimila with an Atmega 168 and it works great.
After that I tried it wit the Deumilanove wit Atmega 320 but I couldn’t upload the program…Have you all ready did some tests to find the “Deumilanove” problem?
Thank anyway!
-
thomas December 2, 2009at 4:59 pm
Hi,
real nice work!,
I would like to build this however i got the duemilanove :( ,
did anyone find out what the problem is with the newest arduino? -
manray January 29, 2010at 9:55 pm
I am having trouble with the Arduino file finding the custom library. I am using a Duemilanove and maybe going to use a Mega.
-
Really great help.
I’m building an object with arduino using your schemas and sketch…
I’ll tell you my progress soon.
It works :-)
interesting code- seems you have to make a big effort with these devices…
Also took some time to get your code working- problems most likely with the ID-12 reseting(??)
I have found the ID-12 very unreliable,and it does not meet the claims
made by its makers for range, sensitivity etc.