The work card you open the door with, or a metro card: that's RFID. The RC522 module lets you build the same idea with an Arduino in half an hour.
How does it work?
- The module contains a coil (antenna) that creates a field at 13.56 MHz.
- The card or key fob contains a chip and a coil, with no battery. It takes its power from the field and replies with its data.
- The distance is short: centimetres.
Card types
| Card | Frequency | Memory | With the RC522 |
|---|---|---|---|
| MIFARE Classic 1K | 13.56 MHz | 1 KB | Yes (the one supplied with the module) |
| MIFARE Ultralight | 13.56 MHz | 64 bytes | Yes |
| NTAG213 / 215 | 13.56 MHz | 144–504 bytes | Yes, and phones can read it |
| EM4100 and similar | 125 kHz | Just a number | No |
Many older work and building cards are 125 kHz. The RC522 can't see them at all, and they need a different module (such as the RDM6300). If a card won't read, that's the first question.
And if your phone has NFC, you can read 13.56 MHz cards with it (with an app such as NFC Tools), which is an easy way to find out a card's type.
Wiring (SPI)
| RC522 | Arduino Uno |
|---|---|
| SDA (SS) | 10 |
| SCK | 13 |
| MOSI | 11 |
| MISO | 12 |
| RST | 9 |
| 3.3V | 3.3V |
| GND | GND |
Warning: the module is 3.3 V. Don't connect VCC to 5V. The signal pins should also be 3.3 V; many people run it with an Uno directly and it works, but a level shifter is safer. With an ESP32 there's no problem. See SPI.
Testing the wiring
Before anything else, add this line to setup:
rfid.PCD_DumpVersionToSerial();
If it prints a version number (such as 0x92 or 0x91), the wiring is fine. If it prints 0x00 or 0xFF, there's a wrong SPI wire or the module isn't powered. Clone modules sometimes print another number (such as 0x12) and work normally.
Reading the card (the MFRC522 library)
#include <SPI.h>
#include <MFRC522.h>
MFRC522 rfid(10, 9);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
rfid.PICC_HaltA();
}
The UID is usually 4 bytes on MIFARE Classic, and 7 bytes on NTAG and Ultralight, so use rfid.uid.size and don't assume a fixed number.
Comparing against an allowed list
byte allowed[][4] = {
{0xDE, 0xAD, 0xBE, 0xEF},
{0x12, 0x34, 0x56, 0x78}
};
bool isAllowed() {
if (rfid.uid.size != 4) return false;
for (byte k = 0; k < sizeof(allowed) / 4; k++)
if (memcmp(rfid.uid.uidByte, allowed[k], 4) == 0) return true;
return false;
}
Write in the numbers you got from the code above. And if you want to add cards without uploading code: make a master card that, when read, enters a "register the next card" mode, and save the new one in the EEPROM.
Writing to the card
A MIFARE Classic 1K card is divided into 16 sectors, each of 4 blocks × 16 bytes. You can write a name or a balance into a block, after authenticating with the sector's key (the default is usually FF FF FF FF FF FF). The library's examples (ReadAndWrite) contain the complete code.
Danger: the last block of each sector (the sector trailer) holds the keys and access permissions. If you write a wrong value there, the sector can be locked forever. And never write to block 0; it holds the UID and manufacturer data.
A card lock project
- Read the UIDs of the allowed cards and store them in the code (or in EEPROM).
- When a card approaches, compare.
- If allowed, drive an electric lock with a transistor or MOSFET or a relay, with a diode across the coil.
And add the details that make a difference: a green and red LED and a buzzer for feedback, a lock that opens for only a few seconds and relocks by itself, and the card owner's name shown on a display.
Range
- 2–5 cm usually with cards, and less with small fobs.
- Metal kills the field: the module on a steel door or inside a metal box won't read well. Put a few millimetres of plastic or wood between them.
- If you'll mount it behind a cover, keep the cover thin (2–3 mm of plastic).
Security: watch out
The UID is easily read, and there are cheap cards onto which any other card's UID can be written. So relying on the UID alone isn't real protection. For a learning project or a personal drawer it's fine, but for a house door or an important place, use commercial systems with real encryption.
Common faults
| Fault | Usual cause |
|---|---|
| Version 0x00 or 0xFF | Wrong SPI wiring or no power |
| The card never reads | A 125 kHz card, or metal nearby |
| It reads once and stops | You forgot PICC_HaltA(), or the supply is weak |
| The module is hot | It was connected to 5 V |
| It reads the card several times | You need to ignore the same UID for a few seconds |
FAQ
Can I have more than one reader on the same Arduino? Yes, each with a different SS pin, and the rest shared on SPI.
Could a phone imitate the card? Some phones can imitate certain cards, which is another reason the UID isn't protection.