Full width home advertisement

Post Page Advertisement [Top]

 I lost my house keys three times last year. Once in a coat I donated without checking the pockets, once somewhere between my car and the front door that I never actually figured out, and once — embarrassingly — inside my own house while frantically searching outside for twenty minutes. After the third time, I finally built an RFID door lock using an ESP32, and now I unlock my workshop by tapping a keychain fob against a small reader. No keys to lose, ever.

This guide covers the full build: the RFID reader, the lock mechanism, and the code that ties it together, plus the mistakes I made so you can skip straight past them.

DIY ESP32 RFID Smart Door Lock 1

How RFID Actually Works Here

Each RFID card or keychain fob has a unique ID number baked into it. When you tap it near the reader module, the reader picks up that ID over a short-range radio signal and passes it to the ESP32. The ESP32 then checks that ID against a list of "approved" IDs in its code — if it matches, it triggers the lock to open. If it doesn't match, nothing happens. That's genuinely the whole concept; everything else in this build is just wiring and code to make that happen reliably.

What You'll Need

  • 1x ESP32 development board
  • 1x MFRC522 RFID reader module (cheap, widely available, and the standard choice for this kind of project)
  • A few RFID cards or keychain fobs (usually included with the MFRC522 kit)
  • 1x Relay module (to control the lock mechanism)
  • 1x Solenoid lock or servo motor, depending on your door setup
  • 1x Buzzer (optional, for audible feedback)
  • 2x LEDs — one red, one green (optional, for visual access granted/denied feedback)
  • Jumper wires and a breadboard for prototyping

A practical note before you start: build and test this entire project on your desk first, fully working, before you ever mount anything on an actual door. I did not do this the first time, and ended up standing outside my own workshop at 11pm troubleshooting code while the door was locked from the inside. Learn from my mistake.

Step 1: Wiring the RFID Reader

The MFRC522 communicates over SPI, so the wiring is a bit more involved than a simple sensor, but it's very standard:

  • SDA → GPIO 21
  • SCK → GPIO 18
  • MOSI → GPIO 23
  • MISO → GPIO 19
  • RST → GPIO 22
  • VCC → 3.3V (important — this module does not tolerate 5V well)
  • GND → GND

That VCC point is worth repeating: the MFRC522 wants 3.3V, not 5V. Feeding it 5V is one of the most common ways people accidentally fry this module on their first attempt.

DIY ESP32 RFID Smart Door Lock 2

Step 2: Installing the Library and Reading Card IDs

In the Arduino IDE, install the MFRC522 library through the Library Manager. Before writing any lock logic, run this simple sketch first to find out the unique ID of each of your cards:

cpp
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 21
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("Scan a card...");
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  Serial.print("Card UID: ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
  mfrc522.PICC_HaltA();
}

Open the Serial Monitor, scan each card you want to authorize, and write down the UID it prints for each one. You'll need these values in the next step.

Step 3: Wiring the Relay and Lock Mechanism

  • Relay VCC → 5V
  • Relay GND → GND
  • Relay IN → GPIO 4

If you're using a solenoid lock, it typically needs 12V and draws more current than a simple GPIO pin can supply — that's exactly why the relay is there, acting as a switch between the ESP32's low-power signal and the lock's higher-power circuit. If you're using a servo motor instead (a common choice for cabinet-style locks or simpler builds), you can skip the relay and wire the servo signal pin directly to a GPIO pin, since servos draw far less current.

Step 4: The Full Access Control Code

Here's a working version that checks scanned cards against a list of authorized UIDs and unlocks accordingly.

cpp
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 21
#define RST_PIN 22
#define RELAY_PIN 4
#define GREEN_LED 26
#define RED_LED 27

MFRC522 mfrc522(SS_PIN, RST_PIN);

byte authorizedUID[4] = {0x2B, 0xB8, 0x59, 0xB1}; // replace with your own card's UID

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
}

bool checkUID() {
  for (byte i = 0; i < 4; i++) {
    if (mfrc522.uid.uidByte[i] != authorizedUID[i]) return false;
  }
  return true;
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  if (checkUID()) {
    Serial.println("Access granted");
    digitalWrite(GREEN_LED, HIGH);
    digitalWrite(RELAY_PIN, HIGH);
    delay(3000);
    digitalWrite(RELAY_PIN, LOW);
    digitalWrite(GREEN_LED, LOW);
  } else {
    Serial.println("Access denied");
    digitalWrite(RED_LED, HIGH);
    delay(1000);
    digitalWrite(RED_LED, LOW);
  }

  mfrc522.PICC_HaltA();
}

For multiple authorized cards, store each UID as a separate array and loop through checking against all of them, rather than hardcoding just one like this simplified example.

Common Problems (And How I Actually Fixed Them)

The reader doesn't detect any cards. Double-check the SPI wiring, especially MOSI and MISO — it's very easy to swap these two by accident, and the module will simply stay silent if they're crossed.

It works on the breadboard, but fails once I move it to the actual door. Longer wire runs can introduce SPI communication issues. If you're mounting the reader somewhere away from the ESP32, keep the wires as short as reasonably possible, or consider mounting the whole board near the door instead of running long SPI wires.

The relay clicks, but the solenoid lock doesn't move. Solenoid locks usually need 12V and a decent amount of current. Check that your relay is actually switching the 12V supply, not just the ESP32's 5V or 3.3V rail — this was exactly the mistake that had me standing outside my own workshop that one night.

Random unauthorized access attempts get logged, but I want to actually be notified. Once the basic version works, adding Wi-Fi and sending a Telegram or push notification on every access attempt (granted or denied) is a natural next step — and honestly one of the more satisfying upgrades once you've got the core lock logic solid.

DIY ESP32 RFID Smart Door Lock 3

Final Thoughts

This project has quietly become one of the most-used things I've built. It sounds like a small convenience — tap a fob instead of using a key — but once it's running, you stop thinking about keys entirely, which is a strange and genuinely nice feeling after years of patting your pockets before leaving the house.

Just build it, test it fully on the bench, and don't mount it on a working door until you're confident it unlocks reliably. Ask me how I know.

Bottom Ad [Post Page]