The RC522 is the little blue board that turns a card or key fob into a password. Built around NXP's MFRC522 chip, it creates a 13.56 MHz magnetic field, wakes any ISO/IEC 14443A tag that comes within a few centimetres, and hands your microcontroller the tag's unique ID over SPI, fast enough that a tap feels instant.
It can also read and write the memory inside MIFARE Classic and NTAG tags, which is why it sits behind so many door locks, attendance systems, tool check-outs and "tap to start" gadgets. This page explains how it works, what every pin does, which tags it can and cannot read, and the mistakes that cost people an evening. When you are ready to build, jump to the Arduino Uno guide or the ESP32 guide.
Run the RC522 in your browser
Ready-wired circuit with code. No hardware, no install.
RC522 at a glance
Specifications
- Frequency
- 13.56 MHz
- Supply voltage
- 3.3 V
- Interface
- SPI
- Typical range
- 3-5 cm
- Standard
- ISO/IEC 14443A
- Chip
- NXP MFRC522
- Chip supply range
- 2.5 V to 3.6 V
- Tags it can talk to
- MIFARE Mini / 1K / 4K, Ultralight, NTAG, DESFire (UID)
- Max SPI clock
- 10 Mbit/s
- Other chip interfaces
- I2C, UART (not broken out on this module)
- Supply current, TX on
- 60 mA typical, 100 mA max
- Power-down current
- 5 µA max (RST held low)
- Logic level
- 3.3 V
- FIFO buffer
- 64 bytes
- Crystal
- 27.12 MHz
- Board size
- about 60 × 40 mm
- Header
- 8 pins, 2.54 mm pitch
- Usually included
- 1 MIFARE Classic 1K card + 1 key fob
Chip figures come from the NXP MFRC522 datasheet. Range, board size and the included tags are typical for the common blue module and vary by seller. NXP quotes up to 50 mm for the chip with a suitable antenna; the tiny PCB loop on this board and a credit-card tag give 3-5 cm in practice.
What is on the board
Almost everything on the module exists to serve one chip. The MFRC522 does the radio work, the framing, the CRC checks and the MIFARE crypto, and talks to your microcontroller over SPI. Around it sit the parts that make the radio side behave: a 27.12 MHz crystal (the 13.56 MHz carrier is derived from it), an EMI filter that cleans up the transmitter output, and a matching network of capacitors that tunes the printed loop antenna so it resonates at exactly 13.56 MHz.
That tuning is the reason the module is picky about its surroundings. Stick a metal plate behind the antenna, tape it to a phone, or screw it down onto a steel enclosure, and the resonance shifts and the range collapses. A few millimetres of plastic or acrylic in front of the coil is fine; metal near it is not.
One more thing worth knowing: the green or red power LED only proves that 3.3 V is present. It says nothing about whether the SPI wiring is right.
Pinout
| Pin | Name | Description |
|---|---|---|
| 1 | SDA | SPI chip select (SS / NSS), active LOW. The silkscreen says SDA because the same pin is the I2C data line on the bare chip. |
| 2 | SCK | SPI clock, driven by the microcontroller. |
| 3 | MOSI | SPI data from the microcontroller to the reader. |
| 4 | MISO | SPI data from the reader back to the microcontroller. |
| 5 | IRQ | Interrupt output from the chip. Optional; the common libraries poll instead, so leave it unconnected. |
| 6 | GND | Ground. Must be shared with the microcontroller. |
| 7 | RST | Reset / power-down. LOW puts the chip into hard power-down, a rising edge restarts it. Any free digital pin works. |
| 8 | 3.3V | Supply input, 3.3 V only. Never connect this to 5 V. |
Pin order is the order printed on the board, left to right.
Read this before you wire it. The pin labelled SDA is not an I2C data line on this module. The board is strapped for SPI, so SDA is simply the chip-select (many libraries call it SS or NSS). The bare MFRC522 chip can also speak I2C and UART, but those modes need the mode-select pins changed, which the standard blue module does not expose, so treat it as an SPI-only device.
The other detail people trip over is RST. It is a real hardware reset and a power-down switch: held LOW the chip draws only microamps, and the first rising edge wakes it up again. Most libraries pulse it for you during start-up, which is why it needs to go to a GPIO and not to a fixed level.
How a reader and a tag actually talk
An RFID system has two halves. The reader (the RC522 plus your microcontroller) is formally called the PCD, the proximity coupling device. The tag is the PICC, the proximity integrated circuit card. A passive tag has no battery: it is a small chip joined to a coil, and it lives entirely off the reader's field.
At 13.56 MHz the two coils behave like a loosely coupled transformer. The reader's coil makes an alternating magnetic field; when a tag's coil enters it, a voltage is induced, the tag's chip wakes up, and it answers by loading the field in a pattern the reader can detect. There is no line of sight involved, which is why a tag works through a wallet, a plastic casing or a thin wall, and why range is measured in centimetres rather than metres.
Reading a card is a short conversation, and each step unlocks the next. For a simple "who is this?" you only need the first three.
- REQA / ATQA - the reader asks "is anyone there?" and the tag answers with a short code describing its family and UID length.
- Anti-collision and SELECT - if several tags are in the field, the protocol sorts them out bit by bit until one is selected. The result is the UID, the tag's serial number, and the SAK byte, which says what kind of tag it is.
- Authenticate - tags with protected memory (MIFARE Classic) will not give up data until you prove you know the sector key.
- Read / write - 16-byte blocks for MIFARE Classic, 4-byte pages for NTAG and Ultralight.
The RC522 library wraps all of this into a few calls, but keeping the picture in mind makes error messages like "timeout" or "authentication failed" much easier to read.
Which tags work with the RC522
The reader speaks ISO/IEC 14443 type A at 13.56 MHz. If a tag is not in that family it is invisible to the module, no matter how close you hold it. The cheap round "ID" fobs sold for old door systems are often 125 kHz and need a completely different reader.
MIFARE Classic 1K memory in one picture
The card that ships with most RC522 modules is a MIFARE Classic 1K. Its 1,024 bytes are split into 16 sectors of 4 blocks, 16 bytes each. Block 0 is factory-programmed (it contains the UID), and the last block of every sector is a trailer: it stores the two secret keys for that sector (Key A and Key B) plus the access bits that decide what each key may do.
That leaves 47 blocks you can use freely. Every example on SolderHub that writes to a card uses block 4 (first data block of sector 1) for exactly this reason.
Danger
Never write to a sector trailer (blocks 3, 7, 11, 15 ... 63) unless you fully understand access bits. One wrong byte there permanently locks the whole sector and there is no factory reset. Also leave block 0 alone: on genuine cards it cannot be changed, and on 'magic' cards you can easily corrupt it.
RC522 or something else?
The RC522 is the right first reader for most projects: it is cheap, runs from 3.3 V and has a mature Arduino library. It is not the answer to every RFID problem, so here is how it compares with the other readers you will meet.
RC522 vs the alternatives
RC522 (MFRC522)
- Frequency
- 13.56 MHz
- Tags
- ISO 14443A: MIFARE, NTAG
- Host interface
- SPI on the module
- Pick it when
- You want the cheapest way to read cards, fobs and stickers
PN532
- Frequency
- 13.56 MHz
- Tags
- 14443A/B, FeliCa, NFC peer-to-peer
- Host interface
- I2C, SPI or UART
- Pick it when
- You need real NFC features or more tag families
PN5180
- Frequency
- 13.56 MHz
- Tags
- 14443A/B, ISO 15693, NFC
- Host interface
- SPI
- Pick it when
- You need ISO 15693 tags or a longer, more tolerant read
RDM6300 / EM4100 reader
- Frequency
- 125 kHz
- Tags
- EM4100 read-only fobs
- Host interface
- UART
- Pick it when
- You already own 125 kHz fobs
UHF reader
- Frequency
- 860-960 MHz
- Tags
- EPC Gen2 labels
- Host interface
- UART or USB
- Pick it when
- You need metres of range and can pay for it
Read the first two columns as a filter: if your tags are not on that frequency and standard, the reader is the wrong choice regardless of price.
Power and wiring that just works
- 3.3 V only. The chip is specified for a 2.5 V to 3.6 V supply. A 5 V supply on the 3.3V pin can destroy it, and many modules have no regulator to catch the mistake.
- Mind the current. While the antenna is on, the datasheet puts the transmitter at 60 mA typical and 100 mA maximum. A weak 3.3 V source (a long USB cable, a tiny on-board regulator shared with other loads) shows up as random read failures or resets when a card arrives. A 100 µF capacitor close to the module is cheap insurance.
- Short wires. SPI is fast. Keep the four signal wires short and away from the antenna side of the board, and share one good ground.
- One SPI bus, many devices. The RC522 can share SCK, MOSI and MISO with an SD card or a display, as long as every device has its own chip-select pin and only one is selected at a time.
- Several readers. Give each RC522 its own SS pin (and RST if you like). They can share everything else.
- Mount it behind plastic. Acrylic or plastic up to a few millimetres thick in front of the coil is harmless. Keep metal away from both sides.
Warning
5 V microcontrollers (Arduino Uno, Nano, Mega) drive their outputs at 5 V, which is outside the 3.3 V design of the module. Many builds run that way without visible trouble, but it is unproven for the long term. For anything permanent, level-shift the signals the Uno sends to the reader (SS, SCK, MOSI, RST). The Uno guide shows a two-resistor divider that does it for pennies.
Ready to build? Pick your board for wiring, ready-to-run sketches and board-specific fixes:
- RC522 with Arduino Uno - 5 V board, level-shifting advice, UID reader, card whitelist and block read/write
- RC522 with ESP32 - native 3.3 V, pin map for ESP32 / S3 / C3, a Wi-Fi door log and an NTAG page reader
A reality check on security
A UID is a serial number, not a secret. It is sent in the clear, it can be read from a few centimetres away, and many cheap "magic" cards let you write any UID you like onto them. On top of that, the encryption inside MIFARE Classic (Crypto1) has been publicly broken for years, so its keys can be recovered with off-the-shelf hardware.
That is fine for a hobby door, an attendance sheet, a game or a tool tracker. It is not fine for anything that protects money, people or valuable equipment. If a lock matters, add a second factor (a keypad PIN or a fingerprint such as the R307 sensor), or move to cards with modern AES-based security (DESFire EV2/EV3, NTAG 424 DNA) and a reader and library that support them properly.
Project ideas that start with a tap
- Door or cabinet lock - allow-list of UIDs, a relay or servo as the latch, and an LCD to say who came in. The Arduino RFID Door Lock with Access Log & LCD Display project builds exactly this.
- Attendance logger - an ESP32 timestamps every tap and posts it to a spreadsheet, MQTT or Home Assistant.
- Tap-to-start machine - a workshop tool, 3D printer or laser cutter that only runs after an authorised card is presented.
- RFID jukebox or kids' toy - one sticker per song, story or character; a tap picks what plays.
- Inventory and tool check-out - tag every tool, scan it out and in, keep a running log.
- Vending or token machine - a card stores a balance in a data block that the machine reads and updates.
Common problems
'Communication failure, is the MFRC522 properly connected?' or version 0x00 / 0xFF
- Likely cause
- No SPI conversation: wrong or loose wiring, no 3.3 V, SS or RST pin mismatch
- What to do
- Check 3.3 V at the module with a multimeter, confirm GND is shared, match SS/RST to the sketch, solder the header
Version prints but a card is never detected
- Likely cause
- Wrong tag frequency, tag too far or off-centre, metal near the antenna
- What to do
- Use a 13.56 MHz MIFARE or NTAG tag, hold it flat and centred 1-3 cm from the coil, move metal away, try a second tag
Card reads once, then nothing until it is removed
- Likely cause
- Normal: the card stays active after a read
- What to do
- Call PICC_HaltA() and PCD_StopCrypto1() when you are done with a card
Random misses or a reset when a card arrives
- Likely cause
- Weak 3.3 V supply, long wires, noise
- What to do
- Add a 100 µF capacitor at the module, shorten wires, power from a proper 3.3 V regulator
'Authentication failed' / STATUS_ERROR on a Classic card
- Likely cause
- Wrong key, or you are addressing a block in a sector whose key was changed
- What to do
- Try the factory key FF FF FF FF FF FF; authenticate against the sector's trailer block
Read and write work on the kit card but not on a sticker
- Likely cause
- The sticker is NTAG or Ultralight, not MIFARE Classic
- What to do
- Use page-based reads (4 bytes per page) and no authentication; see the ESP32 guide's NTAG sketch
Reader works on the bench but not in the enclosure
- Likely cause
- Metal, thick material or a ground plane next to the antenna
- What to do
- Mount behind a few millimetres of plastic with metal at least a few centimetres away
Garbage characters in the Serial Monitor
- Likely cause
- Baud rate mismatch
- What to do
- Set the monitor to match Serial.begin() - 115200 in all SolderHub sketches
Board-specific problems (Uno pins, ESP32 SPI defaults, brown-outs) are covered in the board guides.
Frequently asked questions
What does the RC522 read, and what can't it? Any 13.56 MHz ISO 14443A tag: MIFARE Classic, Ultralight, NTAG and the UID of DESFire cards. It cannot see 125 kHz fobs, and it cannot do NFC peer-to-peer or card emulation the way a PN532 can.
How far does it read? Plan on 3 to 5 cm with a card and a little less with a small fob or sticker. NXP quotes up to 50 mm for the chip with a well-matched antenna; the small printed loop on this board does not get there.
Can it read my phone? Not reliably. Phones present an app-defined or randomised ID, so the UID is not stable. If you want phone interaction, use a PN532 and an app that writes NDEF tags.
Does it work through a wall, wallet or enclosure? Through plastic, cardboard, wood and a wallet, yes, within the usual range. Metal blocks or detunes it.
Can I use I2C or UART instead of SPI? The chip supports them (I2C up to 3400 kBd, UART up to 1228.8 kBd), but the standard blue module is wired for SPI, so use SPI.
Which library should I use? The Arduino MFRC522 library by miguelbalboa (listed as "GithubCommunity" in the Library Manager). It handles detection, UID reading, MIFARE authentication, reads and writes.
What do the firmware version numbers mean? Genuine NXP chips report 0x91 (v1.0) or 0x92 (v2.0). Other values are common on clone chips, which usually work fine; 0x00 or 0xFF means the reader is not answering at all.
Can I clone a card or change its UID? Normal cards have a read-only UID. Special "magic" cards allow it, which is one more reason a UID alone should not guard anything important.
Does it work with Raspberry Pi, Pico or STM32? Yes, anything with an SPI bus and 3.3 V logic. The concepts and wiring here carry over; only the library changes.
How much power does it use? Up to about 60 mA typical (100 mA max) while the antenna is on. Held in reset via the RST pin, the chip drops to about 5 µA or less, so battery projects should switch it off between scans.
Datasheets and references
- NXP MFRC522 datasheet - the source for the electrical figures on this page
- MFRC522 Arduino library on GitHub - source, examples and the README on wiring and tag support
Warning
Security limitation: a UID is not a secret or a secure credential. The allow-list sketches in this guide are learning demonstrations only. Many low-cost RFID cards expose a UID that can be read, copied or emulated, and UID-only checks do not authenticate the card or its holder. Do not use this method as the sole control for a real door, payment, attendance record or other security-sensitive system. For a real deployment, choose a credential and reader system with an appropriate cryptographic authentication design, protect keys, use a secure controller and fail-safe lock hardware, and assess the complete system—not just the reader.
Wiring & code
Board integration
Library
MFRC522 by GithubCommunity (miguelbalboa)
Arduino IDE > Sketch > Include Library > Manage Libraries > search "MFRC522" > install "MFRC522" by GithubCommunity
This guide takes an RC522 from a bag of parts to a working card reader on an Arduino Uno in about twenty minutes. You will build the four required 5 V-to-3.3 V signal dividers, check that the reader is alive, read the UID of a card, create an allow-list that lights a green or red LED, and write text into a MIFARE Classic card before reading it back. The wiring, resistor values and sketches are kept consistent throughout the guide.
What you need
| Item | Qty | Note |
|---|---|---|
| Arduino Uno R3 (or Nano, or Uno R4) | 1 | Any board with hardware SPI on D11, D12 and D13 uses the wiring below unchanged |
| RC522 module | 1 | Usually sold with a MIFARE 1K card and a key fob |
| Jumper wires | 8 | Female-to-male if your module header is soldered, male-to-male for a breadboard |
| LED (green) and LED (red) | 1 each | Optional, for the allow-list sketch |
| 220 Ω resistors | 2 | One per LED |
| USB cable | 1 | For power and upload |
| 100 µF capacitor | 1 | Optional, across 3V3 and GND near the module if reads drop out |
| 1 kΩ resistors | 4 | Series resistors for SS, SCK, MOSI and RST voltage dividers |
| 2 kΩ resistors | 4 | Pull-down resistors for the four 5 V to 3.3 V dividers |
Build time about 20 minutes. Difficulty: beginner.
Wiring
| Component pin | Arduino Uno pin | Note |
|---|---|---|
| SDA (SS) | D10 | Chip select. Uno D10 output must pass through a 1 kΩ series / 2 kΩ to GND divider before RC522 SDA (SS). |
| SCK | D13 | Uno D13 SCK output: use a 1 kΩ series / 2 kΩ to GND divider before the RC522. |
| MOSI | D11 | Uno D11 MOSI output: use a 1 kΩ series / 2 kΩ to GND divider before the RC522. |
| MISO | D12 | Direct connection. RC522 MISO is a 3.3 V output read by the Uno. |
| RST | D9 | Uno D9 RST output: use a 1 kΩ series / 2 kΩ to GND divider before the RC522. |
| GND | GND | Connect grounds directly; all four dividers share this ground. |
| 3.3V | 3V3 | Power. Use the 3V3 pin, never 5V |
IRQ stays unconnected. For SS, SCK, MOSI and RST, build four separate dividers: Uno pin → 1 kΩ → RC522 signal; the RC522-side node also connects through 2 kΩ to GND. MISO is direct.
Why these pins? On the Uno, SPI lives on three fixed hardware pins: D11 (MOSI), D12 (MISO) and D13 (SCK). Only the chip-select (D10) and reset (D9) are free to move. If you use a Mega 2560, the hardware SPI pins are different (50 MISO, 51 MOSI, 52 SCK, 53 SS), so rewire to those.
Optional LEDs for the allow-list sketch
| Component pin | Arduino Uno pin | Note |
|---|---|---|
| Green LED anode (+) | D7 | Through a 220 Ω resistor; cathode to GND |
| Red LED anode (+) | D6 | Through a 220 Ω resistor; cathode to GND |
Warning
Power the RC522 from a regulated 3.3 V supply only—never from the Uno 5 V pin. The Uno 3V3 output is rated for 50 mA. The MFRC522 datasheet lists 60 mA typical / 100 mA maximum for the transmitter supply under a continuous-wave test condition; that figure is not a universal measurement of total current drawn by every RC522 breakout. Actual module current depends on the board, antenna and operating mode. Because the Uno 3V3 pin has limited headroom, use a separate regulated 3.3 V supply if the module is unstable or its measured current exceeds the available budget; connect the grounds together. A 100 µF capacitor close to the module can help with supply dips.
The 5 V question
The Uno's outputs swing to 5 V; the RC522 is a 3.3 V part. Plenty of tutorials connect them directly and it usually keeps working; some even describe the signal pins as "5 V tolerant". We have not found that guarantee for a 3.3 V-powered MFRC522, so we treat 5 V signalling as unproven and out of spec. If you want a permanent build, only the lines the Uno sends need attention.
Which lines need level shifting
| Line | Direction | Needs shifting? | Simple fix |
|---|---|---|---|
| SS (D10) | Uno to RC522 | Yes | Divider: 1 kΩ in series, 2 kΩ to GND |
| SCK (D13) | Uno to RC522 | Yes | Same divider |
| MOSI (D11) | Uno to RC522 | Yes | Same divider |
| RST (D9) | Uno to RC522 | Yes | Same divider |
| MISO (D12) | RC522 to Uno | No | A 3.3 V signal is read as HIGH by the Uno (threshold is about 3 V) |
A 1 kΩ + 2 kΩ divider turns 5 V into about 3.3 V. At SPI speeds the library uses it is fast enough. A 4-channel logic level converter does the same job more neatly.
Install the library
- 1In the Arduino IDE open Sketch > Include Library > Manage Libraries (or press Ctrl+Shift+I).
- 2Search for MFRC522 and install the one by GithubCommunity (the library by miguelbalboa). Version 1.4.12 is current at the time of writing.
- 3Choose Tools > Board > Arduino Uno and select the correct Port.
- 4Open the Serial Monitor at 115200 baud after every upload.
Tip
Tutorials that tell you to download a ZIP from GitHub are out of date: the library is in the Library Manager now, so you also get automatic update prompts.
Sketch 1 - Is the reader alive?
Always start here. It does not need a card; it only asks the chip for its firmware version over SPI. If this works, your wiring, power and SS/RST pins are right, and any later problem is about tags or code, not hardware.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SS_PIN = 10; // RC522 "SDA" pin = SPI chip select
constexpr uint8_t RST_PIN = 9;
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
delay(4); // some modules need a moment after init
rfid.PCD_DumpVersionToSerial(); // prints e.g. "Firmware Version: 0x92 = v2.0"
byte v = rfid.PCD_ReadRegister(MFRC522::VersionReg);
if (v == 0x00 || v == 0xFF) {
Serial.println(F("No answer from the reader. Check 3.3 V, GND and the SS/SCK/MOSI/MISO/RST wires."));
} else {
Serial.println(F("Reader is alive. Now try Sketch 2 and tap a card."));
}
}
void loop() {}You should see a firmware version of 0x92 (v2.0) or 0x91 (v1.0). Clone chips often report something else and still work. A value of 0x00 or 0xFF means nothing is answering: go to the troubleshooting flowchart below.
Sketch 2 - Read any card's UID
This is the sketch you will reuse in almost every RFID project. PICC_IsNewCardPresent() asks "is a card in the field?", PICC_ReadCardSerial() reads its UID, and the two calls at the end tell the card to stand down so it is not read again and again while it sits on the reader.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SS_PIN = 10;
constexpr uint8_t RST_PIN = 9;
MFRC522 rfid(SS_PIN, RST_PIN);
// Prints a UID as AA:BB:CC:DD (always two hex digits per byte).
void printUid(const MFRC522::Uid &uid) {
for (byte i = 0; i < uid.size; i++) {
if (i) Serial.print(':');
if (uid.uidByte[i] < 0x10) Serial.print('0');
Serial.print(uid.uidByte[i], HEX);
}
}
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
Serial.println(F("Tap a card, fob or sticker..."));
}
void loop() {
// Both calls must succeed: "a card is in the field" and "I read its serial".
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
MFRC522::PICC_Type type = rfid.PICC_GetType(rfid.uid.sak);
Serial.print(F("UID: "));
printUid(rfid.uid);
Serial.print(F(" (")); Serial.print(rfid.uid.size); Serial.print(F(" bytes) type: "));
Serial.println(rfid.PICC_GetTypeName(type));
rfid.PICC_HaltA(); // park this card so it is not re-read in a loop
rfid.PCD_StopCrypto1(); // clear any authentication state
}Tap each card, fob and sticker you own and note the UIDs it prints. A MIFARE Classic card normally shows 4 bytes (for example 39:C3:BB:99); NTAG stickers show 7. You will paste your own UIDs into the next sketch.
Sketch 3 - A card allow-list with green and red LEDs
This turns the reader into the heart of an access-control system: known cards light the green LED, everyone else gets the red one. Replace the two example UIDs with the ones you collected in Sketch 2.
The comparison uses memcmp on raw bytes instead of building strings. It is shorter, uses no heap memory and cannot be fooled by differences in upper and lower case.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SS_PIN = 10;
constexpr uint8_t RST_PIN = 9;
constexpr uint8_t GREEN_LED = 7;
constexpr uint8_t RED_LED = 6;
// Paste the UIDs that Sketch 2 printed for YOUR cards (4-byte UIDs).
const byte ALLOWED[][4] = {
{0xDE, 0xAD, 0xBE, 0xEF},
{0x12, 0x34, 0x56, 0x78},
};
constexpr size_t ALLOWED_COUNT = sizeof(ALLOWED) / sizeof(ALLOWED[0]);
MFRC522 rfid(SS_PIN, RST_PIN);
bool isAllowed(const MFRC522::Uid &uid) {
if (uid.size != 4) return false; // this list only stores 4-byte UIDs
for (size_t i = 0; i < ALLOWED_COUNT; i++) {
if (memcmp(uid.uidByte, ALLOWED[i], 4) == 0) return true;
}
return false;
}
void blink(uint8_t pin, uint16_t ms) {
digitalWrite(pin, HIGH);
delay(ms);
digitalWrite(pin, LOW);
}
void setup() {
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
Serial.println(F("Access control ready. Tap a card."));
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
if (isAllowed(rfid.uid)) {
Serial.println(F("ACCESS GRANTED"));
blink(GREEN_LED, 1000);
} else {
Serial.println(F("ACCESS DENIED"));
blink(RED_LED, 400);
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}Tip
To open a real door, replace the green LED with a relay module or a servo. The logic does not change. The SolderHub Simulator has a servo 'gate' variant of this circuit you can try first.
Sketch 4 - Write text to a card and read it back
MIFARE Classic cards are tiny databases. This sketch authenticates with the factory key, writes 16 bytes into block 4 (the first data block of sector 1), reads them back and prints both the text and the hex. A static_assert at the top stops the sketch compiling if you pick block 0 or a sector trailer by accident.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SS_PIN = 10;
constexpr uint8_t RST_PIN = 9;
constexpr uint8_t BLOCK = 4; // first data block of sector 1
// Refuse to compile if someone changes BLOCK to something dangerous.
static_assert(BLOCK != 0 && (BLOCK % 4) != 3,
"Pick a data block: not block 0 (manufacturer data) and not a sector trailer.");
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void finish() {
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; // factory default key
Serial.println(F("Tap a MIFARE Classic card to write and read block 4."));
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
MFRC522::PICC_Type type = rfid.PICC_GetType(rfid.uid.sak);
if (type != MFRC522::PICC_TYPE_MIFARE_MINI &&
type != MFRC522::PICC_TYPE_MIFARE_1K &&
type != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Not a MIFARE Classic card, skipping."));
finish();
return;
}
// Every sector is locked by the key stored in its trailer block.
const byte trailer = (BLOCK / 4) * 4 + 3;
MFRC522::StatusCode st = rfid.PCD_Authenticate(
MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailer, &key, &rfid.uid);
if (st != MFRC522::STATUS_OK) {
Serial.print(F("Auth failed: ")); Serial.println(rfid.GetStatusCodeName(st));
finish();
return;
}
// 16 bytes exactly: 15 characters + a zero.
byte data[16] = {0};
memcpy(data, "SolderHub RC522", 15);
st = rfid.MIFARE_Write(BLOCK, data, 16);
if (st != MFRC522::STATUS_OK) {
Serial.print(F("Write failed: ")); Serial.println(rfid.GetStatusCodeName(st));
finish();
return;
}
Serial.println(F("Write OK"));
byte buf[18]; // 16 data bytes + 2 CRC bytes
byte size = sizeof(buf);
st = rfid.MIFARE_Read(BLOCK, buf, &size);
if (st != MFRC522::STATUS_OK) {
Serial.print(F("Read failed: ")); Serial.println(rfid.GetStatusCodeName(st));
finish();
return;
}
Serial.print(F("Block 4 text: "));
for (byte i = 0; i < 16; i++) Serial.print(buf[i] >= 32 && buf[i] < 127 ? (char)buf[i] : '.');
Serial.print(F("\nBlock 4 hex : "));
for (byte i = 0; i < 16; i++) {
if (buf[i] < 0x10) Serial.print('0');
Serial.print(buf[i], HEX);
Serial.print(' ');
}
Serial.println();
finish();
}Danger
Do not change BLOCK to 3, 7, 11 or any other trailer block (every fourth block counting from 3). Writing bad keys or access bits there locks the sector for good.
Troubleshooting on the Uno
Work through the flowchart from the top; each question rules out one layer of the problem.
Uno-specific problems
Version 0x00 / 0xFF, or 'Communication failure'
- Cause
- Wiring, no 3.3 V, or SS/RST pin mismatch
- Fix
- Re-check the table above against your sketch's SS_PIN and RST_PIN; measure 3.3 V at the module
Works on USB, resets when a card is tapped
- Cause
- The Uno's 3V3 pin is overloaded
- Fix
- Separate 3.3 V regulator and a 100 µF capacitor, grounds joined
Nothing works on a Mega 2560
- Cause
- Hardware SPI is on 50-53, not 10-13
- Fix
- Use 50 (MISO), 51 (MOSI), 52 (SCK) and 53 or any pin for SS
Another SPI device (SD card, display) stops working
- Cause
- Two devices answering at once
- Fix
- Give every SPI device its own chip-select pin; keep only one LOW at a time
Compile error: MFRC522.h: No such file
- Cause
- Library not installed
- Fix
- Install 'MFRC522' by GithubCommunity from the Library Manager
Compile error: 'MFRC522::StatusCode' has not been declared
- Cause
- Old library version
- Fix
- Update the library to 1.4.x
Same card is reported over and over
- Cause
- Missing PICC_HaltA() / PCD_StopCrypto1()
- Fix
- Add both calls at the end of loop(), as in all sketches here
Arduino Uno FAQ
Does this work on an Uno R4, Nano or Pro Mini? Yes. They use the same SPI pins (D11, D12, D13), so the wiring and sketches work unchanged. A 3.3 V Pro Mini is a particularly good match because it needs no level shifting.
Can I use a different SS or RST pin?
Yes. Change SS_PIN and RST_PIN in the sketch and move the wire. MOSI, MISO and SCK cannot move on an Uno because they are hardware SPI.
Can I run two RC522 readers on one Uno?
Yes. Share SCK, MOSI, MISO and power, give each reader its own SS pin, and create two MFRC522 objects.
Why does the sketch print at 115200 baud? The reader can dump a lot of text quickly, and the library's own examples use 115200. Make sure the Serial Monitor matches.
How long can the wires be? Keep them as short as practical; under about 20 cm is comfortable. Long SPI wires are a classic cause of intermittent failures.
What should I try next? Add an LCD to show the card holder's name, a servo to open a gate, or a buzzer for feedback. The SolderHub Simulator has ready-made starting points for each.
Where next? Show who tapped on an LCD1602 I2C display or a 0.96" OLED, open a latch with an SG90 servo, or add feedback with a passive buzzer. Prefer Wi-Fi logging? Try the ESP32 guide.
Understanding the Uno sketches
Sketch 1 — reader check: SPI.begin() starts the Uno hardware SPI peripheral. The MFRC522 object uses D10 as chip select and D9 as reset. PCD_Init() configures the reader, and PCD_ReadRegister(VersionReg) reads its version register. A value of 0x00 or 0xFF is a useful sign that communication has failed, but a plausible version value alone does not prove that every tag operation will work.
Sketch 2 — UID reading: PICC_IsNewCardPresent() checks for a newly detected card; PICC_ReadCardSerial() retrieves its serial/UID. uid.size tells the sketch how many UID bytes were received, so the print function does not assume every card has a four-byte UID. The loop prints each byte in hexadecimal, padding values below 0x10 with a leading zero. PICC_HaltA() halts the selected card, while PCD_StopCrypto1() clears the reader authentication state.
Sketch 3 — LED allow-list: ALLOWED is an array of two example four-byte UIDs; replace them with values read from your own cards. isAllowed() first checks the UID length, then uses memcmp() to compare the raw bytes. This example intentionally rejects seven-byte UIDs rather than accidentally comparing only part of them. blink() turns on the selected LED for the requested duration and then turns it off. This example demonstrates program flow, not secure access control: see the security warning on the component page.
Sketch 4 — MIFARE Classic block access: The sketch first checks the detected card type, then calculates the sector trailer for block 4 and authenticates with Key A. It writes exactly 16 bytes to the data block and reads the block into an 18-byte buffer because the library reserves room for the 16 data bytes plus CRC bytes. The factory key FF FF FF FF FF FF is commonly used on new cards, but it is not safe to leave as a production credential. Use a spare test card: writing a block changes the card contents. The static assertion prevents selecting block 0 or a block whose number ends in 3, but this simple guard is intended for the example block in the small-sector layout; do not treat it as a general block allocator for every sector of a MIFARE Classic 4K card.
