How to Use the RFID-RC522 Module With Arduino
by Belal Elsherbini in Circuits > Arduino
8039 Views, 4 Favorites, 0 Comments
How to Use the RFID-RC522 Module With Arduino
In this Instructable, I'll give a walkthrough on the fundamental working principle of the RFID module coupled with its tags and chips. I'll also provide a brief example of a project I made using this RFID module with an RGB LED. As usual with my Instructables, I'll give a brief overview within the first few steps and will leave a comprehensive, detailed explanation in the last step for those who are interested.
Supplies
RC522 RFID Module + identification tag and card - https://www.amazon.com/SunFounder-Mifare-Reader-Ar...
RGB LED + three 220 ohm resistors
Hardware Connections
In this project I used the Arduino Mega, but you could use any microcontroller you'd like since this is a relatively low-resource project, the only thing that would be different is the pin connections for SCK, SDA, MOSI, MISO, and RST since they are different on every board. If you are not using the Mega, refer to the top of this script that we will be using shortly: https://github.com/miguelbalboa/rfid/blob/master/e...
RFID:
SDA (white) - 53
SCK (orange) - 52
MOSI (yellow) - 51
MISO (green) - 50
RST (blue) - 5
3.3v - 3.3v
GND - GND
(Note: Although the reader strictly requires 3.3V, the pins are 5V tolerant, which allows us to be able to use this module with Arduinos and other 5V DIO microcontrollers)
RGB LED:
Red Cathode (purple) - 8
GND - GND
Green Cathode (green) - 9
Blue Cathode (blue) - 10
Software
Now onto the software.
First, we need to install the MFRC522 library to be able to get, write, and process RFID data. The github link is: https://github.com/miguelbalboa/rfid, but you could also install it via the library manager in the Arduino IDE or on PlatformIO. Before we can create our own, custom program to deal with and process RFID data, we first need to get the actual UID's for our card and tag. For that, we need to upload this sketch: https://github.com/miguelbalboa/rfid/blob/master/e...
(Arduino IDE: examples > MFRC522 > DumpInfo)
(PlatformIO: PIO Home > libraries > installed > MFRC522 > examples > DumpInfo)
What this sketch does is essentially extract all information present in a card, including the UID in hexadecimal form. For example, the UID of my card is 0x72 0x7D 0xF5 0x1D (see picture). The rest of the printed out data structure is the information present in the card that we can read or write to. I will go more in depth in the last section.
Software (2)
As usual with my Instructables, I will explain the software in line-by-line comments so that each part of the code can be explained in relation to its function in the rest of the script, but what it essentially does is identify the card being read and either grants or denies access. It also reveals a secret message if the correct card is scanned twice.
RFID; Explained
In the reader, there is a Radio Frequency module and an antenna which generates an electromagnetic field. The card, on the other hand, contains a chip which can store information and allow us to alter it by writing to one of its many blocks, which I will go into more detail in the next section as it falls under the RFID's data structure.
The working principle of RFID communication is fairly straightforward. The reader's antenna (in our case, the antenna on the RC522 is the embedded coil-like structure on the face) which will send out radio waves, which in turn will energize a coil in the card/tag (within close proximity) and that converted electricity will be used by the transponder (device which receives and emits radio frequency signals) within the card to send back the information stored within it in the form of more radio waves. This is known as backscatter. In the next section, I will discuss the specific data structure used by the card/tag to store information that we can either read or write to.
RFID; Explained (2)
If you look at the top of the output of our script uploaded earlier, you will notice that the card's type is PICC 1 KB, meaning that it has 1 KB of memory. This memory is allocated into a data structure composed of 16 sectors that carry 4 blocks, each of which carry 16 bytes of data (16 x 4 x 16 = 1024 = 1 KB). The last block in each sector (AKA Sector Trailer) will be reserved for granting read/ /write access to the rest of the sector, which means we have only the first 3 blocks to work with in terms of storing and reading data.
(Note: the first block of sector 0 is known as the Manufacturer Block and contains vital information such as manufacturer data; changing this block could completely lock your card so be careful when attempting to write data to it)
Happy tinkering.