How to Add an RFID Reader to Your ESP32 (RC522 Wiring and Code)
RFID sounds like it should be complicated, but on an ESP32 it is one of the quickest ways to make a project feel clever. You tap a card, and something happens. That is the whole idea, and it opens up a lot: a tap-to-activate costume effect, a hidden trigger sewn into a jacket, a little access system for your workshop, an interactive piece of art. In this tutorial we wire up an RC522 reader, read the ID from your tags, and then make the board respond only to the tag you choose. It takes about an hour, nothing needs soldering, and you can follow the full build in my video.
What RFID is, in one paragraph
The reader creates a small electromagnetic field. When a tag comes into range, the tiny chip inside it powers up from that field and sends back its unique ID, which is just a short series of numbers. The tag needs no battery. Your ESP32 reads the number and compares it with the ones you have told it about. Match, and you trigger one response. No match, and you trigger another. Tags need to be close to the reader for this to work, so think of it as a tap rather than a wave.
What you need
- An ESP32 development board, 30-pin, such as the ESP32 Development Board 30-Pin (USB-C)
- An RFID-RC522 reader (13.56 MHz, SPI) with a card and key fob or two
- A solderless breadboard and jumper wires
- Optional: a 0.96 inch I2C OLED for feedback
- A USB-C data cable (not a charge-only cable)
- PlatformIO in VS Code, or the Arduino IDE
If you want it all in one box, the RFID + ESP32 Video Kit has these parts, and it is a level 1, no-solder build.
Step 1: Power and ground
Push the ESP32 into one side of the breadboard so the pins you need stay accessible, then add the RC522 beside it. Run a wire from the ESP32's 3.3V pin to the power rail, and from the rail to the RC522's 3.3V pin. Do the same for ground. This matters: the RC522 runs on 3.3V, so do not connect it to 5V.
Step 2: The SPI connections
These are the connections I used in the video, and they are standard for most ESP32 boards:
- RC522 SDA (also labelled SS) to GPIO 5
- RC522 SCK to GPIO 18
- RC522 MOSI to GPIO 23
- RC522 MISO to GPIO 19
- RC522 RST to GPIO 2
- The IRQ pin is not used
As always, check the pinout for your particular board before you plug in. If you want a refresher on reading ESP32 pinouts, the guide on the blog covers it.
Step 3 (optional): Add the OLED
The screen is only there for feedback, but I find it so useful that I rarely skip it. It uses I2C, so it needs power, ground, SDA to GPIO 21 and SCL to GPIO 22.
Step 4: Set up PlatformIO and read your tag IDs
Create a new project and add the reader library to platformio.ini:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps = miguelbalboa/MFRC522
Then a short sketch that prints the ID of any tag you hold up to the reader:
#include <Arduino.h>
#include <SPI.h>
#include <MFRC522.h>
const uint8_t SS_PIN = 5;
const uint8_t RST_PIN = 2;
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
Serial.println("Scan a tag");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
Serial.print("UID:");
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();
rfid.PICC_HaltA();
}
Upload it, open the serial monitor and scan each of your cards, stickers and key fobs in turn. Write down the ID for each one. You will need these numbers in the next step.
Step 5: Make it react to the right tag
The idea is simple. Store the ID of the tag you want to recognise, then compare every scan against it. In the video I use this to show a heart on the OLED for the right tag and a little dancing skeleton for any other, and the full version of that code is on my GitHub, linked in the video description. Here is the core of the logic:
// Replace with the bytes you wrote down in Step 4
byte myTag[4] = {0xDE, 0xAD, 0xBE, 0xEF};
bool matches() {
if (rfid.uid.size != 4) return false;
for (byte i = 0; i < 4; i++) {
if (rfid.uid.uidByte[i] != myTag[i]) return false;
}
return true;
}
Call matches() after a successful read. If it returns true, do your special thing; otherwise do the fallback. The placeholder bytes above are only an example, so use your own.
Troubleshooting
- Nothing prints: check the data cable first, then confirm the 3.3V connection to the reader.
- Reads are unreliable: hold the tag flat and close to the reader, and keep metal away from it.
- Garbled or zero IDs: re-check MISO, MOSI and SCK, which are the easiest wires to swap by accident.
- Your board has different pins: use its SPI pins and update SS_PIN and RST_PIN to match.
Where to take it next
Once the tag check works, the response can be anything. Light a NeoPixel, play a sound, unlock a servo, or send a message over WiFi. For wearables, the RC522 is on the larger side, but the same tags work with smaller readers, so you can prototype on the breadboard and shrink the hardware later. A tag sewn inside a sleeve or a bag strap can become a hidden trigger that only you know about.
Watch the whole build here: The Easiest Way to Add RFID to ESP32 (15 Minute Build). I would love to hear what you plan to trigger with it, so tell me in the comments.
Christine