RFID is a popular tagging and identification technology. Radio frequency identification involves one RFID tag and the second RFID reader. RFID tags and labels are low-cost and easily available. Compared to barcode tracking systems, RFID solutions have several advantages. One of the apparent advantages of RFID over barcodes is that it does not require a line of sight for scanning. RFID readers catch the encoded information on RFID tags using radio frequency signals. RFID is a perfect contactless tagging and identification technology handy to implement and use. That is why RFID is the preferred choice for several applications like access control systems, automatic toll collection, contact-less payment, automated check-out systems, and inventory tracking.
There are several RFID readers available. One of the popular RFID readers is NXP’s MFRC522. This RFID reader can be interfaced with a controller/computer using UART, SPI, and I2C protocols. The RFID reader/writer IC in MFRC522 uses a 13.56 MHz radio frequency for its operation. MFRC522 RC522 can read/write RFID cards and tags built using ISO/IEC 14443 protocol. This includes MIFARE-compatible RFID tags like MIFARE-mini, MIFARE-1K, MIFARE-4K RFID tags, Key Fob, and NTAG RFID cards.
This project interfaces the MFRC522 RC522 RFID reader with Arduino and reads NUIDs of MIFARE-compatible cards and key fobs. We also demonstrate a simple access control system using the RFID reader and the RFID tags using Arduino.
- Arduino UNO/Arduino Mega x1
- MFRC522 RC522 RFID Reader x1
- MIFARE Classic-1K RFID cards and key fobs x Minimum 2
- Connecting wires/jumper wires
MFRC522 RC522 RFID reader
An RFID solution uses electromagnetic waves in radio frequency for data communication. The system consists of two components – one is an RFID transceiver, and the second is an RFID transponder. The RFID Transceiver reads/writes encoded data to RFID transponders over a low-frequency radio signal. The Transponders are RFID cards, fobs, tags, or stickers with a built-in EEPROM that stores encoded data.
Each RFID transponder has an identification number that may be a 7-byte long Unique Identity Number (UID) or 4-byte long Non-Unique Identity Number (NUID). The main component of an RFID card/tag/sticker is an RFID chip. The RFID chips can be read-only or programmable. The UID or NUID of a read-only card cannot be changed or modified, including other data stored on the chip. The programmable chips usually come with a preprogrammed chip ID code that can be overwritten with additional memory using an RFID transceiver.
NXP’s RC522 RFID reader is designed to read/write RFID transponders based on ISO/IEC 14443 protocol, including MIFARE and NTAG RFID cards/tags/stickers. The reader is referred to as Proximity Coupling Device (PCD), and the tags/stickers/cards are referred to as Proximity Integrated Circuit Card (PICD). The reader can communicate with a controller/computer using SPI, I2C, and UART protocols. It reads/writes tags over 13.56 MHz frequency with an operating range of 50 mm. The RC522 reader has an MFRC522 chip, a 27.12 MHz oscillator, RF antenna, and other components onboard. The operating voltage of the reader is 2.5~3.3V. However, the communication port on the reader is 5V tolerable. Therefore, it can be directly interfaced with 5V microcontrollers like Arduino.
The reader has the following pin configuration.
RC522 RFID reader supports SPI, I2C, and UART protocols for communication with a controller/computer. For Arduino, we have a library that supports only SPI protocol. Plus, SPI is the fastest among all the three serial communication protocols. That is why we are interfacing the RC522 reader with Arduino using a physical SPI port. To interface RC522 with Arduino via SPI, follow the circuit connections mentioned in the table below.
Arduino library for RC522 RFID reader
The circuit connections can be made after placing the module on a breadboard. The connections follow the below circuit diagram.
At present, there are several Arduino libraries available for RC522. The library used in this project is Arduino MFRC522 Library from miguelbalboa, available at Github. Download the library as a ZIP file. Navigate to Sketch->Include Library->Add .ZIP Library in Arduino IDE. Select the rfid-master.ZIP, and the library for RC522 is added to the Arduino environment.
Dumping RFID tag data to Serial Monitor
The Arduino library for RC522 itself comes with a lot of examples. One of the useful examples is DumpInfo. This example reads all the data on the RFID transponder chip and prints it to the Serial Monitor. Navigate to Files->Examples->MFRC522->DumpInfo.
Load the sketch to Arduino and run Serial Monitor. On reading a MIFARE Classic-1K RFID tag, the Serial Monitor output looks as follows.
Memory map of MIFARE Classic-1K RFID card
The above images are screenshots of reading a MIFARE Classic-1K RFID card. As seen from the above images, RC522 read all the data stored in the on-chip memory of the RFID card. The on-chip memory is divided into 16 sectors, where each sector consists of 4 blocks. Each block stores 16 bytes. In each sector, the first three blocks are data blocks, and the fourth block is the trailer block which contains two secret keys and access bits. The access bits determine the sector’s read/write or read-only status. The first block contains manufacturer information in the first sector, i.e., sector 0, and a trailer block follows only two data blocks.
How the project works
In this project, we read the NUIDs of the RFID cards/key fogs and display the read NUID on Serial Monitor in both decimal and hexadecimal format. The NUID is 4-bytes long and identifies a particular RFID tag/label/card/sticker.
Upload the above sketch to Arduino UNO/Arduino MEGA after making the proper circuit connections. Open Serial Monitor and set the baud rate to 9600. On contacting a tag/card/key, the NUID of the respective tag/card/key is read and displayed on the Serial Monitor in decimal and hexadecimal formats. The Sketch also compares the tags/keys with a given NUID to demonstrate access control using RFID cards/keys.
The sketch begins with importing SPI and MFRC522 libraries for working with RC522 RFID readers via SPI protocol. The pin assignments for SDA and Reset are defined, and an object of MFRC522 is instantiated with those pins. A variable to store the MIFARE key is defined. A byte array ‘ nuidPICC’ is defined to store 4 bytes of RFID NUID. Variables are declared to store string representations of the NUID in decimal and hexadecimal formats. Similarly, a variable to store a string representation of the MIFARE key is defined.
In the setup() function, the baud rate for serial communication with Serial Monitor is set to 9600 bps using Serial.begin() method. The SPI protocol is initialized using the SPI.begin() method. The RFID module initializes the PCD_Init() method on the RFID object. The MIFARE key bytes are set to FF and stored in an upper-case string representation. The MIFARE key is published on the Serial Monitor.
In the loop() function, RFID card/key/tag is detected by calling PICC_IsNewCardPresent() method on the RFID object. Next, the card NUID/UID is read by calling the PICC_ReadCardSerial() method on the RFID object. The MIFARE card type is detected using the PICC_GetType(rfid.uid.sak) method and displayed on the Serial Monitor. Next, the individual bytes of the NUID is accessed using the rfid.uid.uidByte property. First, the bytes are serialized in decimal format and converted to a string object. The string containing a decimal representation of NUID is published on the Serial Monitor. Next, the bytes are serialized in hexadecimal format and converted to a string object. The string containing a hexadecimal representation of NUID is published on the Serial Monitor. The read bytes of NUID is stored in a character array. This character array is compared to a predefined NUID using user-defined compareNUID() function. The function returns a boolean value used to determine access control. The reader is stopped from reading the card by calling PICC_HaltA() method on the rfid object. Finally, the encryption is stopped by calling PCD_StopCrypto1() method on the RFID object.
You may also like:
Filed Under: Arduino, Electronic Projects, Tutorials