ESP32-CAM
Face Recognition Door Lock
AI-powered smart door lock using ESP32-CAM face recognition, a 1-channel relay module, and 12V solenoid lock. Contactless, automatic, and secure — no keycard or PIN needed.
📖 Read the full project on MakeMindz — includes face enrollment guide and advanced features
🔗 View Full Project on MakeMindzProject Overview
A smart security system that uses AI to recognize faces and automatically unlock a door
The ESP32-CAM Face Recognition Door Lock combines computer vision, IoT hardware, and embedded systems into a complete smart access control solution. The ESP32-CAM's built-in AI model continuously scans for faces — when it detects a registered face, it triggers GPIO4 to activate the relay module, which switches 12V power to the solenoid lock and opens the door automatically.
AI Face Recognition
ESP32-CAM runs an on-chip face detection and recognition model — no cloud needed
Auto Door Unlock
Recognized face → GPIO4 HIGH → relay closes → solenoid lock opens for 5 seconds
Relay Switching
1-channel relay safely switches 12V solenoid current from a 3.3V GPIO signal
Unknown = Locked
Unrecognized faces leave the relay OFF and door securely locked
Contactless Access
No physical key, PIN, or RFID card — face is the only credential needed
Low-Cost Build
Entire system costs under $15 in components — accessible for students and makers
Components Required
Everything you need to build the face recognition door lock
ESP32-CAM Module
Main controller with built-in OV2640 camera — runs face detection and recognition AI, controls GPIO4 relay signal
FTDI Programmer (USB-to-Serial)
Required for uploading the Arduino sketch — ESP32-CAM has no USB port of its own. Use 3.3V logic level.
1-Channel Relay Module (5V)
Electrically isolates the ESP32-CAM from the 12V solenoid circuit. Activates when IN receives HIGH from GPIO4.
12V Solenoid Door Lock
Electromechanical lock — plunger retracts when 12V is applied, allowing door to open. Returns to locked state when power is removed.
12V Battery / DC Power Supply
Dedicated 12V supply for the solenoid lock. Do not power the solenoid from the ESP32-CAM's power rails.
Jumper Wires
Male-to-female and male-to-male wires for connecting modules. Use red for power, black for ground, other colours for signals.
Breadboard (optional)
Useful for prototyping the relay and power connections before building a permanent installation
Circuit Connections
Full wiring reference for all three circuit sections
📐 Circuit Connection Map
| From | To | Wire / Note |
|---|---|---|
| RELAY ↔ ESP32-CAM | ||
| Relay VCC | 5V | Red wire |
| Relay GND | GND | Black wire |
| Relay IN | GPIO4 | Signal wire — HIGH = relay ON |
| SOLENOID ↔ RELAY ↔ 12V SUPPLY | ||
| Relay COM | 12V Battery (−) | Black wire |
| Relay NO | Solenoid Lock (−) | Closes circuit on activation |
| Solenoid Lock (+) | 12V Battery (+) | Red wire — always connected |
Arduino Code
Upload to ESP32-CAM using FTDI programmer and Arduino IDE
/* * ESP32-CAM Face Recognition Door Lock * Controls a 1-Channel Relay module connected to IO4. * When a registered face is detected, the relay activates * for 5 seconds, powering the 12V solenoid lock to unlock. * * Hardware: ESP32-CAM → Relay IN → GPIO4 * Relay NO → Solenoid Lock (−) * 12V supply → Solenoid Lock (+) and Relay COM */ const int relayPin = 4; // Relay signal pin → GPIO4 (IO4) void setup() { Serial.begin(115200); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); // Relay OFF at startup → door LOCKED Serial.println("Security system initialized. Door locked."); } void loop() { // ───────────────────────────────────────────────────── // Replace this placeholder with your ESP32-CAM face // recognition result. In the full project, the CAM // module sends a HIGH signal on GPIO4 when a known // face is detected by the on-chip recognition model. // ───────────────────────────────────────────────────── bool faceRecognized = false; // ← set true when face matches if (faceRecognized) { Serial.println("✅ Face recognized — unlocking door..."); digitalWrite(relayPin, HIGH); // Activate relay → solenoid opens delay(5000); // Door stays unlocked 5 seconds digitalWrite(relayPin, LOW); // Deactivate relay → door locks Serial.println("🔒 Door locked again."); } else { Serial.println("❌ Face not recognized — door remains locked."); } delay(100); // Poll every 100ms — prevents rapid toggling }
How It Works
Step-by-step working principle of the face recognition door lock
Camera Continuously Scans
The ESP32-CAM's OV2640 camera captures frames continuously. The on-chip face detection model scans every frame looking for human faces in the field of view.
Face Compared to Database
When a face is detected, the recognition model compares facial feature vectors against the stored database of enrolled (authorized) faces. Enrollment is done once via the web interface.
Match Found → GPIO4 Goes HIGH
If the detected face matches an enrolled face (above confidence threshold), the ESP32-CAM sets GPIO4 HIGH, sending a trigger signal to the relay module's IN pin.
Relay Activates → Circuit Closes
The relay coil energizes, pulling the NO (Normally Open) contact closed. This completes the 12V circuit between the battery positive terminal and the solenoid lock negative terminal.
Solenoid Lock Opens for 5 Seconds
12V flows through the solenoid coil, generating a magnetic field that retracts the plunger — the door unlocks. After 5 seconds, GPIO4 goes LOW, the relay opens, power cuts, and the plunger returns — door locks again.
No Match → Relay Stays OFF
If the detected face is not in the authorized database, GPIO4 remains LOW, the relay stays open, no current flows to the solenoid, and the door stays securely locked.
Key Concepts Explained
What makes this project work — the technology behind it
On-Device AI Vision
The ESP32-CAM uses the MTMN (MobileNet-based) face detection and recognition model running entirely on the ESP32 chip — no internet or cloud service required. The model extracts a facial feature vector from each detected face and compares it against enrolled vectors using cosine similarity. A match above the confidence threshold triggers the unlock signal.
Electrical Isolation via Relay
A relay is an electromechanical switch. The ESP32-CAM's 3.3V GPIO can only source ~12mA — far too little to power a solenoid. The relay takes this small signal to energize a coil, which physically moves a metal contact to switch a completely separate high-power circuit. This electrical isolation protects the microcontroller from voltage spikes and high current.
Fail-Safe Lock Design
Using the NO terminal means the solenoid circuit is broken by default — the door is locked even if power fails. If you used NC, a power outage would unlock the door, creating a security vulnerability. Always use NO for security applications where "fail safe = fail locked."
Electromagnetic Plunger Lock
A solenoid lock contains a coil of wire wound around a ferromagnetic plunger. When 12V DC flows through the coil, it creates a magnetic field that pulls the plunger inward, retracting the bolt and releasing the door. When current stops, a return spring pushes the plunger back, re-engaging the lock mechanically.
Digital Output Signal Timing
The 5-second delay(5000) after digitalWrite(relayPin, HIGH) controls how long the door stays unlocked. This is a simple but effective timed access pattern — long enough for someone to open the door, short enough to relock before a second person can tailgate through. In production systems, this would use a non-blocking timer.
Real-World Applications
Where this face recognition door lock can be deployed
Smart Home Security
Replace traditional key locks with contactless face-based entry for front or back doors
Office Access Control
Restrict server rooms, HR offices, or executive areas to only authorized personnel
School Lab Entry
Secure computer labs, science labs, or storage areas — only enrolled students get access
Hostel / Dormitory
Keyless entry for student accommodation — no lost keys, no lock-outs
IoT Smart Door Lock
Foundation for a full smart lock system with Wi-Fi logging and remote access
Warehouse Security
Control access to restricted storage areas in factories or logistics facilities
Comments
Post a Comment