What is the RC522?
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.
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.
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.
Wiring & code
Setting up RC522 with ESP32
Library
MFRC522 by GithubCommunity (miguelbalboa)
Arduino IDE > Sketch > Include Library > Manage Libraries > search "MFRC522" > install "MFRC522" by GithubCommunity. Boards Manager: install "esp32 by Espressif Systems" and select ESP32 Dev Module
The ESP32 is a natural partner for the RC522: both run at 3.3 V, so there is no level shifting to think about, and the ESP32 adds what a bare reader lacks, a network connection. This guide wires the module to the ESP32's default SPI pins, proves the reader is alive, reads card UIDs, then builds a small Wi-Fi door log that decides locally whether a card is allowed and reports every tap to a web endpoint. A last sketch peeks inside NTAG stickers, which behave very differently from the MIFARE Classic card in the kit.
What you need
ESP32 DevKit (ESP32-WROOM-32) or similar
- Qty
- 1
- Note
- S3 and C3 boards work too: only the four SPI pin numbers change
RC522 module
- Qty
- 1
- Note
- With a MIFARE 1K card and key fob
Female-to-female jumper wires
- Qty
- 7
- Note
- Seven connections, IRQ stays free
NTAG213 sticker or keyring
- Qty
- 1
- Note
- Optional, for the last sketch
100 µF capacitor
- Qty
- 1
- Note
- Strongly suggested across 3V3 and GND at the module, because Wi-Fi bursts are hard on weak supplies
USB cable (data-capable)
- Qty
- 1
- Note
- Good cables matter more than usual on the ESP32
Build time about 25 minutes. Difficulty: beginner to intermediate once Wi-Fi is involved.
Wiring
| Component pin | ESP32 pin | Note |
|---|---|---|
| SDA (SS) | GPIO5 | Chip select. Any free output-capable GPIO works |
| SCK | GPIO18 | VSPI clock |
| MOSI | GPIO23 | VSPI data out |
| MISO | GPIO19 | VSPI data in |
| RST | GPIO22 | Reset. Any free output-capable GPIO works |
| GND | GND | Common ground |
| 3.3V | 3V3 | Power. The ESP32 is a 3.3 V board, so there is nothing to shift |
IRQ stays unconnected: the library polls the chip.
Picking pins on an ESP32
The classic ESP32 has two user SPI controllers; the Arduino core's default SPI object uses VSPI on GPIO18/19/23. Other ESP32 chips pick different defaults, and boards differ, which is why the sketches here call SPI.begin(SCK, MISO, MOSI, SS) explicitly instead of trusting the default.
Default SPI pins by chip
| Chip | SCK | MISO | MOSI | SS |
|---|---|---|---|---|
| ESP32 (classic, VSPI) | 18 | 19 | 23 | 5 |
| ESP32-S3 | 12 | 13 | 11 | 10 |
| ESP32-C3 | 4 | 5 | 6 | 7 |
Typical Arduino-ESP32 defaults for common dev boards. Your board's pins_arduino.h is the final word; because the sketches set pins explicitly, you can use any free GPIOs that suit your layout.
A few GPIO rules save a lot of debugging:
- Avoid GPIO6 to GPIO11 on classic ESP32 modules; they talk to the flash chip.
- GPIO34 to GPIO39 are input-only, so they cannot drive SS, MOSI, SCK or RST.
- GPIO5 is a strapping pin that is sampled at boot. It works well as SS because chip-select idles HIGH, but do not hold it LOW with a wire while the board starts.
Tip
No level shifter, no 5V pin, no divider. The ESP32 and the RC522 are both 3.3 V devices, so this is the simplest wiring of any board in the catalog. Power the module from the 3V3 pin and add a 100 µF capacitor at the module if you plan to use Wi-Fi.
Set up the IDE
- 1In the Arduino IDE open File > Preferences and add the Espressif board index URL if you have not already, then open Tools > Board > Boards Manager and install 'esp32 by Espressif Systems' (3.x).
- 2Choose Tools > Board > esp32 > ESP32 Dev Module (or your exact board) and select the Port.
- 3Open Sketch > Include Library > Manage Libraries, search for MFRC522 and install the one by GithubCommunity (the library by miguelbalboa).
- 4Open the Serial Monitor at 115200 baud after each upload. If upload stalls at 'Connecting...', hold the BOOT button until it starts.
Sketch 1 - Is the reader alive?
Start by asking the chip for its firmware version. It needs no card, and it proves wiring, power and pin numbers all at once.
#include <SPI.h>
#include <MFRC522.h>
// Classic ESP32 (VSPI) pins. S3/C3 boards: change these four numbers.
constexpr uint8_t SCK_PIN = 18;
constexpr uint8_t MISO_PIN = 19;
constexpr uint8_t MOSI_PIN = 23;
constexpr uint8_t SS_PIN = 5; // RC522 "SDA" pin = SPI chip select
constexpr uint8_t RST_PIN = 22;
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); // be explicit: no guessing which pins SPI uses
rfid.PCD_Init();
delay(4);
rfid.PCD_DumpVersionToSerial();
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 SPI wires."));
} else {
Serial.println(F("Reader is alive. Now try Sketch 2 and tap a card."));
}
}
void loop() {}Expect 0x92 (v2.0) or 0x91 (v1.0). Clone chips report other values and usually still work. 0x00 or 0xFF means nothing is answering: use the flowchart further down.
Sketch 2 - Read UIDs and blink on every tap
The ESP32 version returns the UID as an Arduino String, prints a timestamp and flashes the on-board LED (GPIO2 on most DevKit boards) for each read. Write down the UID of every card you own: you will need them next.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SCK_PIN = 18;
constexpr uint8_t MISO_PIN = 19;
constexpr uint8_t MOSI_PIN = 23;
constexpr uint8_t SS_PIN = 5;
constexpr uint8_t RST_PIN = 22;
constexpr uint8_t LED_PIN = 2; // on-board LED on most ESP32 DevKit boards
MFRC522 rfid(SS_PIN, RST_PIN);
String uidToString(const MFRC522::Uid &uid) {
String s;
for (byte i = 0; i < uid.size; i++) {
if (i) s += ':';
if (uid.uidByte[i] < 0x10) s += '0';
s += String(uid.uidByte[i], HEX);
}
s.toUpperCase();
return s;
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
rfid.PCD_Init();
Serial.println(F("Tap a card, fob or sticker..."));
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
Serial.printf("[%lu ms] UID %s type: ", millis(), uidToString(rfid.uid).c_str());
Serial.println(rfid.PICC_GetTypeName(rfid.PICC_GetType(rfid.uid.sak)));
digitalWrite(LED_PIN, HIGH);
delay(120);
digitalWrite(LED_PIN, LOW);
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}Sketch 3 - A Wi-Fi door log
This is where the ESP32 earns its place. The sketch keeps the decision local (the allow-list lives on the board, so the door still works if Wi-Fi is down) and reports every tap as a small JSON message to an HTTP endpoint of your choice: a Node-RED flow, a Home Assistant webhook, a tiny Flask server, or a spreadsheet automation.
Three design choices are worth copying into your own projects:
- Wi-Fi never blocks the reader. Connection happens in the background and is retried every 30 seconds from
loop(), so a dead router cannot freeze card reads. - The HTTP call has a 3 second timeout, so a dead server costs a moment, not the whole system.
- Allowed UIDs are compared as raw bytes with
memcmp.
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>
// ---- Edit these ------------------------------------------------------------
const char *WIFI_SSID = "your-wifi-name"; // 2.4 GHz network
const char *WIFI_PASS = "your-wifi-password";
const char *WEBHOOK_URL = "http://192.168.1.50:1880/rfid"; // any HTTP endpoint that accepts JSON
const char *DEVICE_NAME = "front-door";
// Allowed 4-byte UIDs (paste yours from Sketch 2).
const byte ALLOWED[][4] = {
{0xDE, 0xAD, 0xBE, 0xEF},
};
constexpr size_t ALLOWED_COUNT = sizeof(ALLOWED) / sizeof(ALLOWED[0]);
// ----------------------------------------------------------------------------
constexpr uint8_t SCK_PIN = 18;
constexpr uint8_t MISO_PIN = 19;
constexpr uint8_t MOSI_PIN = 23;
constexpr uint8_t SS_PIN = 5;
constexpr uint8_t RST_PIN = 22;
constexpr uint8_t LED_PIN = 2;
MFRC522 rfid(SS_PIN, RST_PIN);
bool isAllowed(const MFRC522::Uid &uid) {
if (uid.size != 4) return false;
for (size_t i = 0; i < ALLOWED_COUNT; i++)
if (memcmp(uid.uidByte, ALLOWED[i], 4) == 0) return true;
return false;
}
String uidToString(const MFRC522::Uid &uid) {
String s;
for (byte i = 0; i < uid.size; i++) {
if (i) s += ':';
if (uid.uidByte[i] < 0x10) s += '0';
s += String(uid.uidByte[i], HEX);
}
s.toUpperCase();
return s;
}
void startWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
// Non-blocking reconnect: the reader keeps working while Wi-Fi is down.
void keepWifiAlive() {
static uint32_t lastTry = 0;
if (WiFi.status() == WL_CONNECTED || millis() - lastTry < 30000) return;
lastTry = millis();
WiFi.disconnect();
startWifi();
}
void postScan(const String &uid, bool granted) {
if (WiFi.status() != WL_CONNECTED) return; // the door still works offline
HTTPClient http;
http.setTimeout(3000); // never let the network freeze the reader
http.begin(WEBHOOK_URL);
http.addHeader("Content-Type", "application/json");
String body = String("{\"device\":\"") + DEVICE_NAME + "\",\"uid\":\"" + uid +
"\",\"granted\":" + (granted ? "true" : "false") +
",\"uptime_ms\":" + millis() + "}";
int code = http.POST(body);
Serial.printf("POST -> %d\n", code);
http.end();
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
rfid.PCD_Init();
startWifi(); // connects in the background
Serial.println(F("Ready. Tap a card."));
}
void loop() {
keepWifiAlive();
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
String uid = uidToString(rfid.uid);
bool ok = isAllowed(rfid.uid);
Serial.printf("%s -> %s\n", uid.c_str(), ok ? "GRANTED" : "DENIED");
digitalWrite(LED_PIN, HIGH);
delay(ok ? 1000 : 150);
digitalWrite(LED_PIN, LOW);
postScan(uid, ok);
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}A request looks like this, and any server that accepts JSON can log it:
{"device":"front-door","uid":"DE:AD:BE:EF","granted":true,"uptime_ms":482113}Warning
The sketch uses plain HTTP so it stays short, which is fine on a trusted home network. Do not send UIDs across the internet over HTTP. For a public endpoint switch to HTTPS with WiFiClientSecure and certificate validation, and treat UIDs as sensitive data.
Tip
Brown-outs are the number one ESP32 + RC522 gremlin. Wi-Fi transmit bursts draw a few hundred milliamps, so a weak USB cable or port can drop the 3.3 V rail just as a card is read. If the board resets when it posts, try a shorter cable, a different port and a 100 µF capacitor at the module.
Sketch 4 - Look inside an NTAG sticker
NTAG213/215/216 stickers and keyrings are not MIFARE Classic cards. They have no sector keys; their memory is a simple list of 4-byte pages. Pages 0 to 3 hold the UID and lock bytes, and user memory (where NFC phones store NDEF messages) starts at page 4. NTAG213 has 144 bytes of user memory, NTAG215 has 504 and NTAG216 has 888.
This sketch prints the first 12 pages of user memory in hex and ASCII. Write a text record to the sticker with any NFC phone app first, and you will see your own text appear in the right-hand column.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t SCK_PIN = 18;
constexpr uint8_t MISO_PIN = 19;
constexpr uint8_t MOSI_PIN = 23;
constexpr uint8_t SS_PIN = 5;
constexpr uint8_t RST_PIN = 22;
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);
rfid.PCD_Init();
Serial.println(F("Tap an NTAG213/215/216 sticker or an Ultralight tag..."));
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
if (rfid.PICC_GetType(rfid.uid.sak) != MFRC522::PICC_TYPE_MIFARE_UL) {
Serial.println(F("Not an NTAG / Ultralight tag, skipping."));
rfid.PICC_HaltA();
return;
}
Serial.println(F("page hex ascii"));
// Pages 0-3 hold the UID and lock bytes. User memory starts at page 4.
// One MIFARE_Read returns 16 bytes = 4 pages, so step by 4. NTAG213 has pages 4-39.
for (byte page = 4; page < 16; page += 4) {
byte buf[18];
byte size = sizeof(buf);
MFRC522::StatusCode st = rfid.MIFARE_Read(page, buf, &size);
if (st != MFRC522::STATUS_OK) {
Serial.print(F("read failed at page "));
Serial.print(page);
Serial.print(F(": "));
Serial.println(rfid.GetStatusCodeName(st));
break;
}
for (byte p = 0; p < 4; p++) { // print one page (4 bytes) per line
Serial.printf("%4u ", (unsigned)(page + p));
for (byte b = 0; b < 4; b++) Serial.printf("%02X ", buf[p * 4 + b]);
Serial.print(" ");
for (byte b = 0; b < 4; b++) {
char c = buf[p * 4 + b];
Serial.print(c >= 32 && c < 127 ? c : '.');
}
Serial.println();
}
}
rfid.PICC_HaltA();
}Tip
Reading and writing a MIFARE Classic block (authenticate, write, read back) works on the ESP32 exactly as on the Uno. Use the Sketch 4 from the Arduino Uno guide and only swap the SPI.begin() line for the explicit-pin version used here.
Troubleshooting on the ESP32
ESP32-specific problems
Version 0x00 / 0xFF
- Cause
- SPI pins in the sketch do not match the wiring, or you are on an S3/C3 with different defaults
- Fix
- Set SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN to the GPIOs you actually wired; SPI.begin() with those four numbers
Board resets or 'Brownout detector was triggered'
- Cause
- Wi-Fi bursts collapse a weak 3.3 V rail
- Fix
- Shorter or better USB cable, different port, 100 µF at the module
Flash errors or crashes right after boot
- Cause
- A signal wire is on GPIO6 to GPIO11
- Fix
- Move it to a free GPIO (for example 5, 18, 19, 22, 23)
Pins refuse to drive the reader
- Cause
- GPIO34 to GPIO39 are input-only
- Fix
- Use an output-capable GPIO for SS, RST and MOSI
Wi-Fi never connects
- Cause
- 5 GHz network, wrong password, or the router hides the SSID
- Fix
- The ESP32 is 2.4 GHz only; check the credentials
Card reads are slow while Wi-Fi is down
- Cause
- A blocking reconnect loop in the sketch
- Fix
- Use the non-blocking keepWifiAlive() pattern from Sketch 3
Readings get worse when Wi-Fi is busy
- Cause
- Noise and supply dips near the antenna
- Fix
- Keep the RC522 a few centimetres from the ESP32's antenna, add the capacitor
ESP32 FAQ
Is any level shifting needed? No. Both the ESP32 and the RC522 use 3.3 V logic. Just power the module from the 3V3 pin.
Can I use different pins?
Yes. The ESP32's GPIO matrix lets SPI use almost any output-capable pins. That is why the sketches call SPI.begin(SCK, MISO, MOSI, SS) with named constants.
Can the RC522 wake the ESP32 from deep sleep when a card arrives? Not by itself. The chip has no autonomous card-detect mode, so something has to keep polling it. Battery projects usually wake on a timer every few hundred milliseconds, check for a card and go back to sleep, and hold RST LOW in between so the reader drops to its microamp power-down state.
Does Wi-Fi interfere with the reader? Not at the radio level: 2.4 GHz Wi-Fi and 13.56 MHz RFID are far apart. The real interaction is electrical: supply dips from Wi-Fi transmit bursts and noise coupling into long wires.
Can I read NFC tags written by a phone? Yes, NTAG stickers written by a phone can be read as raw pages (see Sketch 4). The library does not parse NDEF for you; for that, parse the bytes yourself or use a PN532 with an NDEF-aware library.
Can I read and write MIFARE Classic blocks? Yes, with the authenticate / write / read sequence from the Arduino Uno guide. It works unchanged on the ESP32.
Where next? Add a 0.96" OLED to show who just tapped, drive a latch with an SG90 servo or a relay, or compare with the Arduino Uno guide. For a complete build, see the RFID door lock project.
Understanding the ESP32 sketches
Pin setup: The classic ESP32 example explicitly assigns SCK to GPIO18, MISO to GPIO19, MOSI to GPIO23 and chip select to GPIO5. SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN) configures those pins for this SPI bus. GPIO22 is used for RC522 reset in these examples; if you add another peripheral, avoid assigning the same GPIO to two signals. ESP32-C3, S3 and other variants have different board pin availability, so select pins from the exact board pinout rather than copying the classic ESP32 numbers blindly.
Sketch 1 — reader check: After initializing SPI and the MFRC522, the sketch reads the version register. 0x00 and 0xFF usually point to a wiring, power, reset or pin-mapping problem. Confirm the exact board and physical connections before changing library code.
Sketch 2 — UID and LED: uidToString() iterates over the number of bytes actually returned by the reader, formats each byte as two hexadecimal digits and converts the result to uppercase. millis() supplies an uptime timestamp; it is not a wall-clock time. GPIO2 is common on some classic ESP32 DevKit boards but is not a universal built-in LED pin—change LED_PIN to match your board or use an external LED with a series resistor.
Sketch 3 — Wi-Fi event log: The allow-list decision is made locally before the HTTP request, so a network outage does not change the result of the local UID comparison. keepWifiAlive() retries Wi-Fi without waiting in a long connection loop. postScan() sends a JSON event only when Wi-Fi is connected and applies a short HTTP timeout. The example uses plain HTTP for a trusted local-network demonstration only; do not expose card identifiers or credentials over an untrusted network. The UID comparison remains a demonstration, not cryptographic authentication.
Sketch 4 — NTAG/Ultralight page display: The reader identifies the tag family using its SAK value and then requests groups of four pages. MIFARE_Read() returns 16 page-data bytes plus two CRC bytes, so the buffer is 18 bytes. The sketch displays the first 12 user-memory pages (4 through 15) as hexadecimal and printable ASCII. The MFRC522 library documents Ultralight/NTAG support as partial; successful UID detection does not guarantee every NTAG model or memory operation is supported. This is a limited raw-page demonstration, not a complete NDEF parser.








