Project

RFID Door Lock with Access Logging Over WiFi

RFID locks for doors, lockers, drawers — the parts cost $20, the firmware is short, and the result actually works. The trick is that the MFRC522 module is pickier about wiring than tutorials suggest; once that's right, the rest is mostly UI. This project uses 13.56 MHz Mifare cards (the standard plastic ones) and a 12 V solenoid lock that fits any standard door frame.

How it works

flowchart TD Card[Mifare card
presented to reader] Card -->|RFID 13.56 MHz| Reader[MFRC522 SPI module] Reader -->|UID 4 bytes| ESP32 ESP32[[ESP32]] --> Check{UID in
allowlist?} Check -->|Yes| Solenoid[Drive relay
3 sec on] Check -->|No| Buzz[Buzzer beep] Solenoid --> Log Buzz --> Log Log[POST event to server
id, granted, timestamp] -.-> ESP32

Standard flow. Cards live in flash; the server log is for audit trail. The lock uses fail-secure logic — power loss leaves it locked, not unlocked.

Bill of materials

  • ESP32 dev board — $7
  • MFRC522 RFID reader module + 2 cards + 1 keyfob — $5
  • 12 V solenoid lock (5 mm bolt throw) — $9
  • 5 V relay module rated 10 A — $2
  • 12 V / 1 A power supply (for solenoid) — $5
  • 5 V / 1 A power supply (for ESP32) — $4
  • Active piezo buzzer — $1
  • Project box, screws, mounting plate — $4

Total around $37. Two PSUs because the solenoid is 12V and the ESP32 is 3.3V/5V; trying to derive 5V from 12V on this scale is more trouble than another wall wart.

Wiring

MFRC522    ESP32
SDA    --- GPIO 5  (SS / chip select)
SCK    --- GPIO 18 (SPI clock)
MOSI   --- GPIO 23 (master out)
MISO   --- GPIO 19 (master in)
GND    --- GND
RST    --- GPIO 22
3.3V   --- 3.3V

Relay      ESP32 / 12V supply
IN     --- GPIO 26
VCC    --- 5V (ESP32)
GND    --- GND
COM    --- 12V supply (+)
NO     --- Solenoid (+)
12V GND -- Solenoid (-) and ESP32 GND (common)

Buzzer + - GPIO 27
Buzzer - - GND

The MFRC522 is famously fussy about ESP32 — it really needs 3.3V (don't feed it 5V), short wires (under 15 cm), and stable power. Many cheap modules have a poor regulator that browns out under load. If you get intermittent reads, decoupling caps near the MFRC522's 3.3V pin help.

Firmware design

The main loop checks for a card every 100 ms. When one is presented, the firmware reads the 4-byte UID, looks it up against an in-memory allowlist (loaded from NVS at boot), and either pulses the relay or beeps the buzzer. Every event POSTs JSON to a logging endpoint with the UID, granted/denied flag, and millis timestamp.

Adding new cards is done by entering "enroll mode" — hold a button at boot, present a card; its UID gets added to the allowlist. Removing cards is via the web UI. The allowlist persists in NVS flash.

Going further

  • Sectors and keys: instead of just reading UID (clonable), use Mifare's sector-key authentication. Much harder to clone but more code.
  • Battery backup: a small Li-ion in line with the ESP32 means it keeps logging even during a power cut. The solenoid stays locked (fail-secure) so this is safe.
  • Tamper switch: a small microswitch behind the project box that triggers an alert if the box is opened.

Key code: main loop

This is the heart of the firmware, taken from the working sketch. The complete file (with config template, library list, and the rest of the helpers) is around 97 lines and is included in the downloadable project package — request it via the form below.

void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
        delay(100);
        return;
    }
    String uid = "";
    for (byte i = 0; i < rfid.uid.size; i++) {
        if (rfid.uid.uidByte[i] < 0x10) uid += "0";
        uid += String(rfid.uid.uidByte[i], HEX);
    }
    uid.toUpperCase();
    Serial.printf("[card] %s ", uid.c_str());

    bool granted = isAllowed(uid);
    if (granted) {
        Serial.println("GRANTED");
        unlock();
    } else {
        Serial.println("DENIED");
        beepDenied();
    }
    logAccess(uid, granted);
    rfid.PICC_HaltA();
    delay(500);  // debounce
}
Project package

Get the complete project package

The article above shows the core firmware and the principles behind it. The complete project package — assembled, tested, and ready to flash — is available by email request. We send it manually, and we read every request.

  • Complete Arduino sketch (.ino) with full error handling
  • List of required libraries with version numbers
  • Printable wiring diagram (PDF)
  • Bill of materials with current part numbers
  • Build guide and troubleshooting tips
  • Configuration template (WiFi, MQTT, etc.)

We send the package by email within 24 hours, usually faster. Free, no spam, no mailing list. Your email is used once, for this reply.

Share your thoughts

Worked with this in production and have a story to share, or disagree with a tradeoff? Email us at support@mybytenest.com — we read everything.