Build Project

Home Automation

ESP32 Smart Door Lock

Build an ESP32 RFID smart door lock with MFRC522 SS GPIO5, RST GPIO22, servo output GPIO26, access granted/denied logic, wiring, code, and safety limits.

IntermediateAges 12+90-150 minUnder $35Parent Safe
ESP32 smart door lock prototype with keypad, servo lock, and RFID-style access card
Jump to a section

What You Will Build

A tabletop RFID access-decision demonstration. An MFRC522 reads a compatible card UID; after explicit provisioning, a matching UID commands a small unloaded servo for five seconds, then returns to its starting command. Enrollment mode prints UIDs without starting servo pulses. No real door, solenoid or certified security system is supported.

Before You Start

Classic ESP32-WROOM DevKit with the named pins exposed; Arduino IDE 2.x and Arduino-ESP32 3.3.2 review baseline, not a latest-release claim. Other chip families require their own pin review. Serial Monitor: 115200 baud. Documentation/code review does not establish compilation or hardware operation; neither has been performed for these changes. Use a documented 3.3 V SPI MFRC522 breakout and compatible 13.56 MHz ISO/IEC 14443-A card (for example MIFARE Classic); a 125 kHz fob is not compatible and arbitrary NFC phones/cards are not promised. Servo: positional FEETECH FS90 as documented by Pololu #2818, which supports 3.3 V control. FS90R, SG90 clones and solenoid locks are excluded. Keep the servo unloaded; the narrow 1400/1600 µs commands are not guaranteed lock angles.

Parts

Wiring

Use 3V3 and GND for the selected reader; SCK18, MISO19, MOSI23, SS/SDA5 and RST22 match explicit SPI.begin(18,19,23,5). Leave IRQ unused. FS90 signal uses GPIO26 with 10 kΩ to GND; positive uses external regulated 5 V and ground joins supply negative and ESP32 GND. Keep external positive separate from USB. Read the actual connector documentation, not generic colors or this logical drawing.

Logical MFRC522 SPI and externally powered FS90 circuit; not physical connector pin order

Open wiring diagram full size

  1. 1

    Disconnect USB and servo power. Wire the reader by labels using the table.

  2. 2

    Leave servo power off; upload enrollment-mode code and open Serial at 115200. Scan a compatible card and record its complete UID byte sequence.

  3. 3

    Replace ALLOWED_UID with those bytes, including all 4, 7 or 10 bytes as printed. Set PROVISIONED=true only for the demonstration card; re-upload.

  4. 4

    Wire the selected unloaded servo and pull-down, verify shared ground and separate positive supply, then enable external servo power with the area clear.

  5. 5

    Verify unknown cards leave the commanded starting state; an allowed card requests 1600 µs for five seconds then 1400 µs. Actual position and mechanical locking are not sensed.

Wiring Reference

SignalESP32 PinDirectionNotes
MFRC522 3.3V / GND3V3 / GNDPowerNo 5 V supply or bus signals on this selected breakout
MFRC522 SCKGPIO18OutputSPI clock
MFRC522 MISOGPIO19InputReader to ESP32
MFRC522 MOSIGPIO23OutputESP32 to reader
MFRC522 SDA / SSGPIO5OutputSPI chip select, not I2C SDA; classic boot strap, verify reset loading
MFRC522 RSTGPIO22OutputIRQ unused
FS90 signalGPIO26Output10 kΩ to GND; supported 3.3 V signal
FS90 positive / groundExternal +5 V / shared GNDPowerDo not join external positive to USB-powered board positive

Libraries and Setup

  • Arduino-ESP32 3.3.2 review baseline, built-in SPI.
  • Library Manager: MFRC522 by GithubCommunity / Miguel Balboa, version 1.4.12; ESP32Servo version 3.0.9. These exact sources were reviewed, not compiled here. MFRC522 maintenance is limited; this UID comparison is not a production access-control design.

Configure the Example

The default PROVISIONED=false prints each scanned UID and leaves servo pulses disabled. Use a private demonstration card, replace the entire ALLOWED_UID array with its printed hexadecimal bytes and then set PROVISIONED=true. The dummy array is not a usable credential. UID length is checked as well as content. A UID can be copied on some cards; it is an identifier, not secure authentication. Start with an unloaded FS90 and review START_US=1400 and OPEN_US=1600. Do not enlarge travel or attach a latch without measuring the mechanical range and reviewing stall/recovery behavior.

Control and Recovery Behavior

ConditionResult
EnrollmentPrint complete UID; no servo attachment/pulses.
Provisioned bootRequests the starting pulse immediately if servo attachment succeeds. No position sensing.
Allowed cardRequests open pulse for five seconds; timer services return without a five-second delay. Extra scans do not extend an active window.
Unknown / power failureUnknown scan gives no new opening command. Loss of power does not guarantee a physical locked or unlocked state.

Code

Install the libraries above and match the configuration and wiring before uploading.

smart-door-lock.ino
#include <Arduino.h>
#include <SPI.h>
#include <MFRC522.h>
#include <ESP32Servo.h>
constexpr uint8_t SS_PIN = 5, RST_PIN = 22, SERVO_PIN = 26;
constexpr bool PROVISIONED = false; // First record your own UID, then enable.
const byte ALLOWED_UID[] = {0x00, 0x00, 0x00, 0x00}; // Replace whole array.
constexpr int START_US = 1400, OPEN_US = 1600;
constexpr uint32_t OPEN_MS = 5000;
MFRC522 rfid(SS_PIN, RST_PIN);
Servo demoServo;
bool servoReady = false, openCommand = false;
uint32_t openedAt = 0;

bool allowedCard() {
  if (!PROVISIONED || rfid.uid.size != sizeof(ALLOWED_UID)) return false;
  for (byte i=0; i<rfid.uid.size; ++i)
    if (rfid.uid.uidByte[i] != ALLOWED_UID[i]) return false;
  return true;
}
void setup() {
  Serial.begin(115200);
  pinMode(SERVO_PIN, OUTPUT); digitalWrite(SERVO_PIN, LOW);
  SPI.begin(18, 19, 23, SS_PIN);
  rfid.PCD_Init();
  if (PROVISIONED) {
    demoServo.setPeriodHertz(50);
    demoServo.attach(SERVO_PIN, 1400, 1600);
    servoReady = demoServo.attached();
    if (servoReady) demoServo.writeMicroseconds(START_US);
  }
  Serial.println(PROVISIONED ? "Provisioned demo; inspect servo state" :
    "ENROLLMENT: scan card; servo pulses disabled");
  if (PROVISIONED && !servoReady) Serial.println("Servo attach failed; no opening commands");
}
void loop() {
  if (openCommand && millis()-openedAt >= OPEN_MS) {
    demoServo.writeMicroseconds(START_US); openCommand=false;
    Serial.println("Starting pulse restored; position not measured");
  }
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
  Serial.print("UID:");
  for (byte i=0; i<rfid.uid.size; ++i) Serial.printf(" 0x%02X", rfid.uid.uidByte[i]);
  Serial.println();
  if (allowedCard() && servoReady && !openCommand) {
    demoServo.writeMicroseconds(OPEN_US); openedAt=millis(); openCommand=true;
    Serial.println("Open pulse requested for 5 seconds");
  } else Serial.println("No new opening command (enrollment, unknown, busy or servo fault)");
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

How the Code Works

Explicit SPI pins remove reliance on board defaults. The sketch prints the UID before comparison, checks its full length and uses a millis-based five-second window. It never treats a key fob appearance as compatibility evidence. ESP32Servo writes commanded pulse widths, not measured angles. A failed attach suppresses opening commands; a stalled or disconnected servo is not detected.

Expected Output

Enrollment prints UID: followed by all hexadecimal bytes. With your array provisioned, an allowed scan prints Open pulse requested for 5 seconds; after the interval it prints Starting pulse restored; position not measured. An unknown card requests no opening. These expectations are not recorded hardware results, and the messages do not establish mechanical locking.

Troubleshooting

  • No UID prints Check selected 13.56 MHz card, 3.3 V reader supply and SCK18/MISO19/MOSI23/SS5/RST22. A 125 kHz tag will not work.
  • Every card denied Enrollment intentionally denies all. Replace the complete byte array and its length, set PROVISIONED=true and re-upload; check Serial.
  • Servo does not move Use exact positional FS90, external 5 V and shared ground. Enrollment has no pulses; inspect attach status. Never power it from GPIO or 3V3.
  • Resets or jams Disconnect servo power. Check supply response, wiring, GPIO5 reset loading and mechanical obstruction; do not force the horn or broaden pulse limits.

Checks to Perform on Your Build

  • Check enrollment prints the complete UID with servo power off.
  • Verify exact pins and separate servo positive supply.
  • With horn removed, compare allowed/unknown card messages and the five-second return command.
  • Confirm reset/power loss does not imply a secure mechanical state.

Practical Design Notes

This teaches card identification and timed motion only. UID comparison is cloneable, and the pinned library does not provide a secure door authentication system. No anti-tailgating, door-position feedback, actuator supervision, backup power or emergency egress design exists. GPIO5 is a classic ESP32 strapping pin: check the reader does not impose an incompatible reset level; if it does, review a different SS pin and update both table and sketch. Do not infer that a commanded pulse locks a real door.

Upgrade Ideas

  • Add a measured position indicator for learning; do not confuse it with certification.

FAQs

Can a solenoid replace this servo?

No. It needs a separately specified driver, supply, protection and control design; this sketch sends servo pulses only.

Can I secure a real door with this?

No. UID matching and commanded hobby-servo motion are educational; real access control needs secure authentication and a professionally reviewed mechanical/egress design.

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-10-26. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.

Educational level: Intermediate. Estimated completion time: 90-150 min. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.