Task: Read out the UID of a RFID tag and display the UID as one contiguous decimal number on the serial monitor.
Required equipment: Arduino / RFID Kit / cables
The RFID (“radio frequency identification”) reader is used to read out a certain code, which is send from a RFID transmitter (also called “RFID tag”) by radio. Each RFID tag has only one unique code. The RFID Kit is useful to realize projects like for example a locking mechanism or other similar projects in which a person should be identified with a tag.
RFID tags may come in different shapes, like a key chain or a card in credit card format.
On the following image you can see on the left side two RFID tags, on the right the RFID receiver RFID RC522 and pin header which have to be soldered to the receiver (There are also versions with already soldered pin headers on the receiver).
How does it work? A RFID receiver contains a small copper coil that generates a magnetic field. An RFID transmitter also includes a copper coil that picks up the magnetic field and generates an electrical voltage insinde the transmitter. This voltage is used by a small electronic chip to get it to emit an electrical code by radio. The transmitter directly receives this code and processes it, so that the microcontroller is able to process the received code.
It is also possible to codify a RFID tag. Due to the complexity it is not mentioned in this tutorial. But you can find several other tutorials for this on the web.
Read out and process the data of RFID tags with the Arduino
Required equipment: Arduino UNO or MEGA, RFID reader, at least one RFID tag, breadboard, cables, one LED, one 200 Ohm resistor
Using a Arduino microcontroller, we want to read out a RFID tag. If it is the right tag the LED should light up for 5 seconds.
Wiring of the RFID reader with the Arduino board:
On the image underneath the RFID reader has soldered pins bent by 90° on it (like the already soldered version). This way it is possible to plug the reader vertical on the breadboard.
Programming
Reading out and processing the data of an RFID receiver would require,as well as other complex tasks, a lot lines of code. Therefore we are going to use one of many existing libraries from the internet. The one we used in this tutorial can be found on https: //github.com/miguelbalboa/rfid. To work with the library you have to download it and save it in the Arduino program folder. You just have to click on “Download ZIP” and save the unziped data on your hard drive.
You have to unpack the folder into the Arduino software folder and save it under “libraries”.
Usually, the folder has been saved under „C: Programmearduinolibraries…“ (If you have saved it somewhere else you have to use the “libraries” folder there).
After the data is unzipped and saved correctly, there should appear a data with the name “rfid-master” in the “libraries” folder. Now you MUST delete the hyphen from the name.
So you rename the data into “rfidmaster”. If you have followed this steps correctly, the library is ready to be used in the Arduino software.
Sketch 1
First of all we are going to read out the UID (“Unique Identification Number”). It is the individual sign of every RFID tag. We are going to use the sketch underneath (Attention, the sketch only works if the library has been added to the software the way it is explained before). This program is only destined for UNO R3 microcontroller. If you want to use MEGA2560 or other controllers, you have to adjust the pins.
#include <SPI.h> //Include SPI library
#include <MFRC522.h> //Include RFID library
#define SS_PIN 10 //SDA on Pin 10 (different on MEGA)
#define RST_PIN 9 //RST on Pin 9 (different on MEGA)
MFRC522 mfrc522(SS_PIN, RST_PIN); //Name RFID receiver
void setup() //Starting setup
{
Serial.begin(9600); //Starting serial connection
SPI.begin(); //set up SPI connection
mfrc522.PCD_Init(); //Initialize RFID receiver
}
void loop() //Loop part starts here
{
if (! mfrc522.PICC_IsNewCardPresent()) //If a card is near by…
{
return; //move on..
}
if (! mfrc522.PICC_ReadCardSerial()) //If a RFID tag has been chosen…
{
return; //move on…
}
Serial.print(„The ID of the RFID-TAG is:“); // Show “The ID of the RFID-TAG is:“ //on the serial monitor
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i], HEX); //Now the UID, which consists out of four separate blocks is //read out and sent to the Serial Monitor one by one. The ending Hex means that the four blocks of the UID //output as HEX number ( including letters ).
Serial.print(“ „); //The command “ Serial.print ( “ “ ); “ ensures that there is a space between the read //blocks.
}
Serial.println(); //This line creates a line break on the serial monitor
}
If everything has worked it should look like this on the serial monitor (except of your own UID):
It is not very easy to work with these HEX numbers one after another. So we change the line “ Serial.print ( mfrc522.uid.uidByte [i] , HEX ) ; “ to “ Serial.print ( mfrc522.uid.uidByte [i] , DEC ; “ . Then you will get the individual parts of the UID as a decimal number.
Sketch 2
Now the UID code is shown as a decimal number, but it is still divided into four blocks. We are going to change the code in a mathematical way to effect that the UID is shown as a single contiguous number (decimal number).
Why are we doing this? If we want to use this sketch later on, for example to let a LED light up or turn a stepper around depending on the right recognized RFID tag, it will be easier to use a IF command with one contiguous number. Example:
“If the RFID Code is 1031720, a LED should turn for 5 seconds”.
The command would be more complicated this way: “If the first block is 195 and the second block is 765 and the third block is 770 and the fourth block is 233 a LED should turn on for 5 seconds”.
A disadvantage of the decimal way is the fact that the sketch can be a little bit unsafe because not all four blocks (max. 12 numbers) can be shown as a contiguous number.
If you want it to be completely safe you would have to test every single block.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code=0; //We are using “code” as a new variable to save the UID as contiguous number later on. By //using “long” instead of “int” we are able to save a longer number.
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code=((code+mfrc522.uid.uidByte[i])*10); //Now the four blocks are read out and the code gets “stretched” //by the factor 10 every passing (Actually, we should use the factor 1000, but the number would become too //large).
}
Serial.print(„The Card number is:“); //Finally the number code (you can’t say UID anymore) gets displayed //on the serial monitor.
Serial.println(code);
}
Great, we are now able to get the individual identification number (on the serial monitor) from a RFID Tag. In this case the number of the tag is 1031720.
And now? We want a LED to turn on for 5 seconds if the wanted RFID Tag gets hold in front of the RFID Reader.
Sketch 3
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode (2, OUTPUT); //Pin 2 gets defined as output (here we are going to connect the LED)
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code=0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code=((code+mfrc522.uid.uidByte[i])*10);
}
Serial.print(„The Card number is:“);
Serial.println(code);
//Here begins the extension of the sketch
if (code==1031720) //If the number code is 1031720…
{ //Open program part
digitalWrite (2, HIGH); //…the LED on Pin 2 should light up …
delay (5000); //..for 5 seconds..
digitalWrite (2, LOW); // …and than turn off.
} //End of the program part
} //End of the sketch