Ever tap your credit card at a terminal or wave a key fob to magically unlock an office door? That’s RFID (Radio Frequency Identification), and it’s the closest thing we have to actual wizardry in electronics.
In this lesson, we are going to add an RFID reader to our Arduino. With it, you can identify specific tags and finally build that secret locking box to hide your snacks from your roommates.
The RC522 is one of the most popular and affordable RFID reader modules for Arduino.
An RFID system has two main parts:
Need the parts? You can grab an RC522 RFID Module kit on Amazon, which usually includes the reader, a white card, and a blue key fob.
The RC522 communicates using the SPI (Serial Peripheral Interface) protocol. SPI requires a few specific pins on the Arduino Uno:
Because SPI communication with the RC522 chip is complex, we will use a library to do the heavy lifting.
Open the Arduino IDE Library Manager (Sketch > Include Library > Manage Libraries...), search for MFRC522 (by GithubCommunity), and install it.
This sketch will read a tag and print its Unique ID (UID) to the Serial Monitor.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
// Create an MFRC522 instance
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin(); // Initialize the SPI bus
rfid.PCD_Init(); // Initialize the RFID reader
Serial.println("Place your card near the reader...");
}
void loop() {
// Look for new cards
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
// Verify if the NUID has been read
if (!rfid.PICC_ReadCardSerial()) {
return;
}
Serial.print("Card UID detected: ");
// Print the UID in HEX format
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
// Halt PICC (stop reading the same card repeatedly)
rfid.PICC_HaltA();
}
Upload the code and open the Serial Monitor. Wave your card or fob over the blue reader. You should see a hexadecimal string appear (e.g., DE AD BE EF).
Right now, we are just printing the ID. But in a real project, you would save your card’s ID in a variable and write an if statement: If the scanned ID matches my saved ID, turn the servo 90 degrees. Welcome to building your own security systems!