What are RFIDs?
RFID stands for Radio Frequency Identification and it is basically a technology that allows us to be able to store useful identity data in common objects we use everyday. These objects, that are made to carry such data are called RFID tags. An example is the rectangular card the receptionist gave to me as my key. It could even be a button, a sticker, a bracelet, the color of your dog’s chain, or just anything tangible. Though these tags may be papers or rubbers, They are manyfactured to have micro-circuits in them. The circuits in them have a small storage device that allow them to store the data they carry.
The other end of the RFID technology is the RFID reader/writer, which are also simle circuits designed to be able to read the information that are already stored in the RFID tags.
These RFID readers/writers are able to also write new informations to the tags. It means you can even store something precious in it. Think about it.
RFID tags and RFID readers communicate by means of short distance radio frequencies. They were very expensive and not common in the past but today you can grab an RFID set from the Aaenics store or you already have it if you bought an arduino kit with an RFID set already inside.
In this tutorial we are going to be exploring the RC522 module and the default Mifare tags that it comes with. The RC522 module may come unsolded so do well to selder the pins so that you can use it on bread boards.
This lesson is quite in depth since you need to really understand certain important things before you can personalize it. So brace yourself and lets do this thing!!!
Lets begin by setting the RFID enviroment up.
This is the setup we will be using throughout the project.
For visualizations we will use the Serial monitor for simplicity.
Pay attention to the pin connections. The RFID reader will communicate with the Arduino Board using SPI(Serial peripheral interface) that is why you can see the MOSI, MISO, SCK, SDS/SS pins being used.
The RC5522 module pin out
The RCC5522 pin can communicate with the arduino using communication protocols such as SPI, i2C and UART but in this setup we are using the SPI connections.
- VCC: This should be connected to the 3.3v of your arduino
- GND: To the ground of your arduino
- RST: This is the reset pin of the Rc522. When the pin goes LOW, it powers the whole RFID circuitory down and when it comes back high, it starts again at fresh. Connect it to pin 9 of your arduino. It can be connected to any other digital pin though
- MISO: This is the Master-In-Slave-Out pin. It is one of the neccessary pins to establish an SPI connection. Connect it to pin 12 of Arduino UNO.
- MOSI: This is the Master-Out-Slave-In pin. It is also one of the neccessary pins to establish an SPI connection. Connect it to pin 11 of arduino UNO.
- SCK: This is the serial clock pin. It is also neccessary for SPI connection. Connect this to pin 13 of Arduino UNO
- SS/SDA: This is the Serial data pin. It acts as the signal input pin for the SPI connection.
- IRQ: This is an interupt pin, It can tell the microcontroller when an RFID tag comes around but we will leave it unconnected for this project.
Establish these simple connections and lets continue
The first codes
The first codes we are going to upload to this setup is going to do something simple. When you bring an RFID tag infront of the RFID reader, it will read all the information on the RFID tag and spit them on the serial monitor. Programming an RFID from scratch is very hectic so we are going to make use of a powerful library that will make things quite simple for us.
Open your libraries manager at Tools>Manage Libraries and search for this MFRC522. Install this particular library!
Next We are going to run one of the examples in the library named “Dump Info”.
What this example does is to scan any RFID tag and spit all the storage layout and all the data in the RFID tag out unto the Serial monitor. So when you run this code, make sure to also open your serial monitor. Also when you send the card close to the reader, let it finish reading everything before you withdraw it.
Understanding the Layout of the RFID storage.
The layout printed on the screen may look scary at first sight but dont worry its nothing big. Am going to try and break it down as best as possible for you to understand.
What you are seeing printed on the screen is the whole memory space of the Mifare RFID tag. It has a storage capacity of 1kB(1KiloByte). This entire 1KB is what you see on the screen.
- As you can see, it has been devided into 16 different sectors counting from 0 to 15 from the buttom of the screen.
- Each sector is also divided into 4 Blocks.
- Each block can hold 16 bytes of data.
So when you do the math, 16 Sectors x 4 Blocks x 16 Bytes = 1024 Bytes. But 1024Bytes = 1 KiloByte.
If you pick Sector 14 for example it has 4 blocks starting from Block 56 to Block 59. All the blocks can store 16 bytes of data but the topmost block, that is the 4th block of each sector is a special one. They are called Sector Trailers and they contain Access Bits which give Read/Write permissions to the rest of the blocks in the sector. So for every sector you are free to store any other information in the rest of the 3 blocks.
However, Sector 0, which is the first sector of every Mifare RFID tag, has a special case.
In secotor 0, apart from block 3 which contains its Access Bits, Block o is also the manufacturer block containing the Unique Identifier(UID) of the entire Tag.
NOTE: This UID is unique to every card and it also risky to change whatever information stored in there.
Writing to an RFID tag
Now that you are farmiliar with the Mifare tag’s layout, lets try and store our own information on the tag. Remember that, there are some special blocks we dont have to edit though.
In the codes below we are going to write any information of our choice in any of the editable blocks. I chose block 13. Copy these codes and run it. You can change the block you want to write to and you can change the information as well.
Since every block can hold only 16 bytes it means it can hold only 16 characters so be mindful of what you can store in a block.
If you store something in a block, it is stored as Hexadecimal numbers. But when you decode them they appear as the original words again. You should read about hexadecimal numbers if you do not know them.
So on the left you can see block 13 is now having some hexadecimal values in there, it is the same as “This is Aaenics” which you can change in the codes.
In the next Section of this tutorial we are going to build a simple project where we assign a “Master Key” to a system. The only way someone can get access to the sytem is when the person has a specific RFID tag.
Building an RFID secured system
So we are going to modify the circuit a bit and add a two LEDs of different colors and piezo buzzer. When the user brings the valid RFID tag, a blue LED will blink with a piezo buzzer calmly to indicate “Access Granted” but when the user uses an invalid card, the red LED will blink wih the buzzer harshly to indicate “Access Denied”.
Just this simple right?
So modify your privious circuit as follows. Connect the buzzer and the LEDs to any of your digital pins.
In the codes below the program has to know the UID of the card you want to allow access to your system even before you run the codes. So make sure to run the previous “Dump Info” codes we run earlier in other to see and copy its UID. With Mifare Tags, only the first 4 bytes of the UID block is enough to be the unique identifier so copy just the first 4 bytes.
The only new thing in these codes is the new getID() function created below the void loop() function. This getID() function does a simple job; it is the one responsible to check if there is any RFID card presesnt. If a card is present it will extract the first 4 bytes of the uid block, convert it into a string and store it in the tagID variable which is later used for the comparison in the void loop() function.
After uploading it you can test other cards apart from the one you assigned and see the effect. The blue key holder in the RFID set is also an RFID tag so dont forget to test that one too.