Build an ESP32 RFID door lock with an RC522 reader, OLED, relay and buzzer. Wiring table, connection diagram and full commented Arduino code.
Need it ready-made? Buy Ready Project
This ESP32 RFID door lock reads a MIFARE keyfobon an RC522 (MFRC522) reader, checks the card UID against a short list in the sketch, and unlocks a 12 V solenoid through a relay for five seconds. A 0.96" SSD1306 OLED shows ACCESS GRANTED or ACCESS DENIED, a green or red LED lights to match, and an active buzzer gives a short double-beep or a longer reject tone.
It is a medium, beginner-friendly access-control build: one ESP32 DevKit, an RC522, an I2C OLED, a 5 V relay module and a lock. There is no Wi-Fi in the basic sketch, so the lock keeps working if your router is down. Use it on a cupboard, a drawer, a cabinet or a low-voltage door strike while you learn SPI, I2C and relay switching.
This is a learning project, not a certified security lock. Cheap MIFARE Classic UIDs can be copied, so do not use it as the only lock on an exterior house door.
How the ESP32 RFID Door Lock Works
The RC522 talks to the ESP32 over SPI. When you hold a card near the antenna, the chip sends the card’s UID (usually four bytes on a typical keyfob). The sketch compares those first four bytes with the AUTH_UIDS list you typed in after reading the Serial Monitor.
- Match: the OLED shows ACCESS GRANTED and OPEN, the green LED lights, the buzzer beeps twice, and GPIO26 pulls the relay IN pin low. The relay contacts feed 12 V to the solenoid for five seconds, then the lock closes again.
- No match: the OLED shows ACCESS DENIED, the red LED lights and the buzzer sounds three longer beeps. The relay stays off.
The OLED uses the ESP32’s default I2C pins, GPIO21 (SDA) and GPIO22 (SCL). SPI uses the VSPI pins: GPIO18 (SCK), GPIO23 (MOSI), GPIO19 (MISO), with GPIO5 as chip-select (the pin labelled SDA on the RC522) and GPIO4 as reset.
Power is split on purpose. The RC522 and the OLED must run from 3.3 V — 5 V will often kill an RC522. The relay module takes 5 V on VCC from the ESP32 VIN pin (the same 5 V that USB supplies). The solenoid has its own 12 V adapter. Join the 12 V negative to the ESP32 GND so the relay coil and contacts share a common ground.
Components Required
- ESP32 DevKit V1 (30-pin DOIT board or similar) – 1
- RC522 RFID reader module (MFRC522, 13.56 MHz) – 1
- MIFARE Classic 1K cards or keyfobs – 2 or more
- 0.96" SSD1306 I2C OLED (128×64) – 1
- 5 V relay module, optocoupler type, one channel – 1
- 12 V solenoid lock or fail-secure door strike – 1
- 12 V DC adapter sized for the lock (often 1 A or more) – 1
- Active buzzer, 3.3 V (or a buzzer module with an I/O pin) – 1
- LEDs: green and red, 5 mm – 1 each
- 330 Ω resistors – 2
- Jumper wires and a breadboard
- USB cable for the ESP32 (also supplies 5 V to VIN)
Wiring Table (Pin-to-Pin)
| Part | Part Pin | ESP32 Pin | Notes |
|---|---|---|---|
| RC522 | 3.3V | 3V3 | 3.3 V only. Do not use VIN or 5 V |
| RC522 | GND | GND | Common ground |
| RC522 | RST | GPIO4 | Reader reset |
| RC522 | SDA (SS / NSS) | GPIO5 | SPI chip-select. This pad is labelled SDA on most RC522 boards |
| RC522 | SCK | GPIO18 | VSPI clock |
| RC522 | MOSI | GPIO23 | ESP32 data out |
| RC522 | MISO | GPIO19 | ESP32 data in |
| RC522 | IRQ | not connected | Leave open |
| OLED SSD1306 | VCC | 3V3 | 3.3 V |
| OLED SSD1306 | GND | GND | Common ground |
| OLED SSD1306 | SCL | GPIO22 | Default I2C clock |
| OLED SSD1306 | SDA | GPIO21 | Default I2C data |
| Relay module | VCC | VIN (5 V) | Coil / optocoupler supply from USB 5 V |
| Relay module | GND | GND | Common ground |
| Relay module | IN | GPIO26 | Active LOW on most modules: LOW = unlock |
| Relay contacts | COM | +12 V adapter + | Switched lock supply |
| Relay contacts | NO | Solenoid + | Normally open, so the lock is off until a valid card |
| Solenoid | − | 12 V adapter − | Join this negative to ESP32 GND |
| Active buzzer | + | GPIO25 | HIGH = beep. Use a transistor for a loud 5 V buzzer |
| Active buzzer | − | GND | Common ground |
| Green LED | Anode through 330 Ω | GPIO27 | Access granted |
| Green LED | Cathode | GND | Common ground |
| Red LED | Anode through 330 Ω | GPIO14 | Access denied. GPIO14 is safe after boot |
| Red LED | Cathode | GND | Common ground |
Do not wire anything to GPIO0, GPIO2 or GPIO12 if you can avoid it — those pins affect how the ESP32 boots. GPIO6 to GPIO11 are used by the flash chip and must stay unused. The pins in the table above stay clear of both problems.
If your relay module has a separate JD-VCC jumper, you can remove the jumper and feed 5 V into JD-VCC while VCC stays at 3.3 V. That keeps the optocoupler on 3.3 V logic. With the jumper left in place, VCC on VIN (5 V) and a 3.3 V IN signal still works on most modules.
Connection Diagram
The diagram below matches the wiring table. The generated illustration is the featured image for this post; the labelled drawing under it is the pin-accurate version to build from.

Arduino Code for the ESP32
In the Arduino IDE install the esp32 by Espressif Systems board package and choose ESP32 Dev Module. Then install MFRC522 (by GithubCommunity), Adafruit SSD1306 and Adafruit GFX Library from the Library Manager. Upload the sketch, open the Serial Monitor at 115200 baud, scan each card, and paste the printed { 0x.., 0x.., 0x.., 0x.. } line into AUTH_UIDS. Upload once more so those cards are authorised.
/*
ESP32 RFID Door Lock with MFRC522 RC522 reader and SSD1306 OLED
Board: "ESP32 Dev Module" (DOIT ESP32 DevKit V1 or similar)
Libraries (Arduino Library Manager):
- MFRC522 by GithubCommunity
- Adafruit SSD1306
- Adafruit GFX Library
Scan a card once with the Serial Monitor open at 115200 baud.
Copy the printed UID bytes into AUTH_UIDS below, then upload again.
Pin map (VSPI + default I2C):
RC522 RST -> GPIO4
RC522 SDA -> GPIO5
RC522 SCK -> GPIO18
RC522 MOSI -> GPIO23
RC522 MISO -> GPIO19
RC522 3.3V -> 3V3 (never 5 V)
OLED SDA -> GPIO21
OLED SCL -> GPIO22
Relay IN -> GPIO26 (active LOW on most modules)
Buzzer + -> GPIO25
Green LED -> GPIO27 through 330 ohm
Red LED -> GPIO14 through 330 ohm
*/
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
static const uint8_t RST_PIN = 4;
static const uint8_t SS_PIN = 5; // RC522 SDA / NSS
static const uint8_t RELAY_PIN = 26;
static const uint8_t BUZZER_PIN = 25;
static const uint8_t LED_OK_PIN = 27; // green = access granted
static const uint8_t LED_NO_PIN = 14; // red = access denied
// Most hobby 5 V relay modules energise the coil when IN is pulled LOW.
static const bool RELAY_ACTIVE_LOW = true;
static const uint32_t UNLOCK_MS = 5000;
static const uint32_t SCAN_COOLDOWN_MS = 1200;
static const uint8_t SCREEN_W = 128;
static const uint8_t SCREEN_H = 64;
static const uint8_t OLED_ADDR = 0x3C; // try 0x3D if the screen stays blank
MFRC522 rfid(SS_PIN, RST_PIN);
Adafruit_SSD1306 display(SCREEN_W, SCREEN_H, &Wire, -1);
// Replace these example UIDs with the ones your cards print in Serial.
// The placeholders below will not match a real keyfob.
const uint8_t AUTH_COUNT = 2;
const uint8_t AUTH_UIDS[AUTH_COUNT][4] = {
{ 0xDE, 0xAD, 0xBE, 0xEF }, // example card 1 — replace me
{ 0x11, 0x22, 0x33, 0x44 } // example card 2 — replace me
};
uint32_t lastScanMs = 0;
void setUnlocked(bool unlocked) {
if (RELAY_ACTIVE_LOW) {
digitalWrite(RELAY_PIN, unlocked ? LOW : HIGH);
} else {
digitalWrite(RELAY_PIN, unlocked ? HIGH : LOW);
}
}
void beep(uint16_t onMs, uint16_t offMs, uint8_t times) {
for (uint8_t i = 0; i < times; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(onMs);
digitalWrite(BUZZER_PIN, LOW);
if (i + 1 < times) {
delay(offMs);
}
}
}
void showIdle() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("ESP32 RFID Lock"));
display.drawFastHLine(0, 12, SCREEN_W, SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(16, 28);
display.println(F("Scan card"));
display.display();
}
void showUidLine(const MFRC522::Uid &uid) {
display.setTextSize(1);
display.setCursor(0, 54);
display.print(F("UID:"));
uint8_t n = uid.size;
if (n > 4) {
n = 4;
}
for (uint8_t i = 0; i < n; i++) {
display.print(' ');
if (uid.uidByte[i] < 0x10) {
display.print('0');
}
display.print(uid.uidByte[i], HEX);
}
}
void showResult(bool granted, const MFRC522::Uid &uid) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(granted ? F("ACCESS GRANTED") : F("ACCESS DENIED"));
display.drawFastHLine(0, 12, SCREEN_W, SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(granted ? 28 : 34, 24);
display.println(granted ? F("OPEN") : F("STOP"));
showUidLine(uid);
display.display();
}
void printUid(const MFRC522::Uid &uid) {
Serial.print(F("UID ("));
Serial.print(uid.size);
Serial.print(F(" bytes):"));
for (uint8_t i = 0; i < uid.size; i++) {
Serial.print(F(" "));
if (uid.uidByte[i] < 0x10) {
Serial.print('0');
}
Serial.print(uid.uidByte[i], HEX);
}
Serial.println();
if (uid.size >= 4) {
Serial.print(F("Copy into AUTH_UIDS as: { "));
for (uint8_t i = 0; i < 4; i++) {
Serial.print(F("0x"));
if (uid.uidByte[i] < 0x10) {
Serial.print('0');
}
Serial.print(uid.uidByte[i], HEX);
if (i < 3) {
Serial.print(F(", "));
}
}
Serial.println(F(" }"));
}
}
bool isAuthorized(const MFRC522::Uid &uid) {
if (uid.size < 4) {
return false;
}
for (uint8_t i = 0; i < AUTH_COUNT; i++) {
if (memcmp(uid.uidByte, AUTH_UIDS[i], 4) == 0) {
return true;
}
}
return false;
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_OK_PIN, OUTPUT);
pinMode(LED_NO_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_OK_PIN, LOW);
digitalWrite(LED_NO_PIN, LOW);
setUnlocked(false);
Serial.begin(115200);
delay(200);
Serial.println(F("ESP32 RFID door lock"));
Serial.println(F("Scan a card to print its UID, then add it to AUTH_UIDS."));
SPI.begin(); // VSPI: SCK=18, MISO=19, MOSI=23
rfid.PCD_Init();
Wire.begin(21, 22); // SDA, SCL
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("OLED not found at 0x3C. Check wiring or try address 0x3D."));
}
showIdle();
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
uint32_t now = millis();
if (now - lastScanMs < SCAN_COOLDOWN_MS) {
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
return;
}
lastScanMs = now;
printUid(rfid.uid);
bool granted = isAuthorized(rfid.uid);
showResult(granted, rfid.uid);
if (granted) {
digitalWrite(LED_OK_PIN, HIGH);
digitalWrite(LED_NO_PIN, LOW);
beep(80, 80, 2);
setUnlocked(true);
delay(UNLOCK_MS);
setUnlocked(false);
digitalWrite(LED_OK_PIN, LOW);
} else {
digitalWrite(LED_OK_PIN, LOW);
digitalWrite(LED_NO_PIN, HIGH);
beep(350, 120, 3);
delay(800);
digitalWrite(LED_NO_PIN, LOW);
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
showIdle();
}
How to Build It
- Wire the RC522 to 3V3, GND, RST (GPIO4), SDA (GPIO5), SCK (GPIO18), MOSI (GPIO23) and MISO (GPIO19). Double-check that the reader is not on 5 V.
- Wire the OLED: VCC to 3V3, GND to GND, SCL to GPIO22, SDA to GPIO21.
- Wire the relay module: VCC to VIN, GND to GND, IN to GPIO26.
- Connect the 12 V adapter positive to relay COM, relay NO to the solenoid positive, and the solenoid negative to the 12 V adapter negative. Join that negative to an ESP32 GND pin.
- Fit the active buzzer on GPIO25 and the two LEDs (green on GPIO27, red on GPIO14) each with a 330 Ω resistor.
- Plug the ESP32 into USB, select the right port, and upload the sketch.
- Open Serial Monitor at 115200 baud, scan a card, copy its UID into
AUTH_UIDS, and upload again.
Testing the Lock
- On reset the OLED should say Scan card and the relay should be off (lock closed).
- Scan a card that is not yet in the list. The OLED shows ACCESS DENIED, the red LED lights and the buzzer sounds three long beeps. The solenoid must stay locked.
- Add that card’s UID to the sketch and upload again.
- Scan the same card. You should see ACCESS GRANTED, a double beep, the green LED, and the relay clicking on for five seconds.
- Measure the solenoid supply while it is unlocked: it should see about 12 V only during those five seconds.
- If the relay clicks the wrong way (on at idle, off for a valid card), set
RELAY_ACTIVE_LOWtofalseand upload again.
Troubleshooting
| Problem | Fix |
|---|---|
| RC522 not found / UID never prints | Confirm 3.3 V (not 5 V) on the reader, and that SDA went to GPIO5, SCK to 18, MOSI to 23, MISO to 19 and RST to 4. Try a shorter set of Dupont wires. |
| OLED stays blank | Swap SDA and SCL if you may have crossed them. The sketch uses address 0x3C; if your module is 0x3D, change OLED_ADDR. |
| Relay never clicks | Check VIN has 5 V while USB is plugged in. Confirm IN is on GPIO26. Some modules need the opposite polarity — flip RELAY_ACTIVE_LOW. |
| Relay clicks but the lock does not move | The contacts (COM / NO) are separate from the IN pin. Check the 12 V adapter, the solenoid polarity and the common ground. |
| Every card is denied | You are still using the example UIDs. Copy the line the Serial Monitor prints into AUTH_UIDS and upload again. |
| ESP32 will not boot or upload | Unplug GPIO12 if you used it, and remove any wire from EN or GPIO0. Use a data-capable USB cable. |
| Buzzer is silent or only clicks | A passive buzzer needs a tone signal. Use an active buzzer, or a module with its own driver transistor. |
Ideas to Extend This Project
- Store authorised UIDs in ESP32 Preferences (NVS) so you can enrol a card from the Serial Monitor without recompiling.
- Add a hidden master card that puts the lock into enrol mode for the next scan.
- Log each attempt to an SD card or, later, to a local web page. If you add Wi-Fi, put
YOUR_WIFI_SSIDandYOUR_WIFI_PASSWORDin the sketch — never commit real credentials. - Drive a fail-secure 12 V door strike in a proper enclosure, with the 12 V wiring kept off the breadboard.
- Add a mechanical key override or a push-button inside the room so you cannot lock yourself out during testing.
FAQ
Can I power the RC522 from 5 V? No. The MFRC522 is a 3.3 V part. 5 V on the 3.3V pin, or 5 V SPI signals from a 5 V Arduino without level shifters, is a common way to destroy the module. The ESP32 is 3.3 V, so it is a good match.
Will this work with an ESP32-C3 or ESP32-S3 board? Yes, if you change the pin numbers to pins that exist on that board and update SPI.begin() if you leave the default VSPI pins. The rest of the sketch stays the same.
My card prints seven UID bytes. Many NFC tags do. The sketch still compares the first four bytes, which is enough to tell typical cards apart. For a stricter check, extend AUTH_UIDS and memcmp to seven bytes.
Is a cloned card a risk? Yes, on MIFARE Classic. Treat this build as a cupboard lock or a learning demo. A production door needs a reader that uses encrypted mutual authentication, not UID matching alone.
Can I use a magnetic lock instead of a solenoid? A maglock is usually fail-safe (it unlocks when power is removed). Wire it on the relay NC contact so a valid card drops power for five seconds, and understand that a power cut will also unlock the door.
Related Projects
- Arduino Mega Smart Parking System with RFID
- ESP32 Digital Spirit Level with MPU6050
- Heart and Pulse Monitoring System with ESP32
- LoRa GPS Tracker with ESP32 and NEO-6M
Browse more ESP32 Projects on Arduino Project Hub.
1 thought on “ESP32 RFID Door Lock with RC522 & OLED”