ESP32-CAM Face Recognition Door Lock Using Relay and 12V Solenoid Lock

ESP32-CAM Face Recognition Door Lock with Relay & Solenoid Lock | MakeMindz
🔐 Intermediate · ESP32-CAM · Arduino · IoT

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.

· By MakeMindz

ESP32-CAM 1-Ch Relay 12V Solenoid Face Recognition AI Arduino IDE

📖 Read the full project on MakeMindz — includes face enrollment guide and advanced features

🔗 View Full Project on MakeMindz
// 01

Project 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

// 02

Components Required

Everything you need to build the face recognition door lock

01

ESP32-CAM Module

Main controller with built-in OV2640 camera — runs face detection and recognition AI, controls GPIO4 relay signal

02

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.

03

1-Channel Relay Module (5V)

Electrically isolates the ESP32-CAM from the 12V solenoid circuit. Activates when IN receives HIGH from GPIO4.

04

12V Solenoid Door Lock

Electromechanical lock — plunger retracts when 12V is applied, allowing door to open. Returns to locked state when power is removed.

05

12V Battery / DC Power Supply

Dedicated 12V supply for the solenoid lock. Do not power the solenoid from the ESP32-CAM's power rails.

06

Jumper Wires

Male-to-female and male-to-male wires for connecting modules. Use red for power, black for ground, other colours for signals.

07

Breadboard (optional)

Useful for prototyping the relay and power connections before building a permanent installation

// 03

Circuit Connections

Full wiring reference for all three circuit sections

📐 Circuit Connection Map

Relay Module → ESP32-CAM
Relay VCC 5V (ESP32-CAM)
Relay GND GND
Relay IN (signal) GPIO4 (IO4)
Solenoid Lock → Relay
Relay COM 12V Battery Negative (−)
Relay NO Solenoid Lock Negative (−)
Power Supply
Solenoid Lock Positive (+) 12V Battery Positive (+)
FromToWire / Note
RELAY ↔ ESP32-CAM
Relay VCC5VRed wire
Relay GNDGNDBlack wire
Relay INGPIO4Signal wire — HIGH = relay ON
SOLENOID ↔ RELAY ↔ 12V SUPPLY
Relay COM12V Battery (−)Black wire
Relay NOSolenoid Lock (−)Closes circuit on activation
Solenoid Lock (+)12V Battery (+)Red wire — always connected
⚠️ Never power the solenoid from ESP32-CAM pins The 12V solenoid draws 500mA–1A. Always use a dedicated 12V power supply switched via the relay. The ESP32-CAM GPIO can only safely source ~12mA.
💡 NO vs NC on the Relay Use the NO (Normally Open) terminal — this keeps the solenoid circuit OPEN (locked) by default. When the relay activates, NO closes and the solenoid gets power. Using NC would keep the door unlocked by default, which is a security risk.
// 05

Arduino Code

Upload to ESP32-CAM using FTDI programmer and Arduino IDE

sketch.ino — face recognition door lock
/*
 * 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
}
💡 Uploading to ESP32-CAM with FTDI Connect FTDI TX→ESP32 RX, FTDI RX→ESP32 TX, GND→GND, 3.3V→3.3V. Hold the GPIO0 button to GND before pressing reset to enter flash mode. Select AI Thinker ESP32-CAM as the board in Arduino IDE.
💡 Enabling Full Face Recognition For the complete face enrollment and recognition system, use the ESP32 CameraWebServer example from Arduino IDE (File → Examples → ESP32 → Camera → CameraWebServer). Set board to AI Thinker, upload, open the IP in your browser, enable Face Recognition, and enroll your face.
// 06

How It Works

Step-by-step working principle of the face recognition door lock

1

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.

2

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.

3

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.

4

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.

5

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.

6

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.

// 07

Key Concepts Explained

What makes this project work — the technology behind it

A — ESP32-CAM Face Recognition

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.

B — Relay Module Operation

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.

C — Normally Open (NO) vs Normally Closed (NC)

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."

D — Solenoid Lock Mechanics

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.

E — GPIO Control Logic

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.

// 08

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

✔ Why This Project Stands Out This system combines AI computer vision, IoT hardware, and real-world actuators in a single low-cost build. It demonstrates how embedded AI can replace mechanical security systems — making it an ideal final-year project, science fair entry, or smart home prototype.
🔐 ESP32-CAM · Face Recognition · Smart Door Lock · IoT
ESP32-CAM Face Recognition Door Lock · MakeMindz

Comments

Product Cards
Buddy Bot eBook
⭐ New 2026 Release
Build Your
Own Robot!
3D design, wiring &
Arduino coding.
Young inventors love it!
🖨️
3D Print
All parts
Wire it
Circuit guide
💻
Code it
Arduino IDE
🤖
Watch it
Walk & react
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Website Offer
₹499 300
🌍 International: $5 USD
One-time · Instant digital delivery
🔒 Secured by Razorpay · Your data is safe
📄 Download Free Sample Copy
🔒 Secured by Razorpay · Your data is safe
🍓
Raspberry Pi Pico Mastery
21 Projects
⚡ Launch Price — 80% OFF
Learn Pico
Build 21 Projects!
MicroPython · Wokwi
IoT · Certificate
Perfect for beginners!
🖥️
Wokwi
No hardware
🐍
MicroPy
From zero
🔨
21 Projects
IoT + sensors
📄
Certificate
Verified cert
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Launch Offer
₹999 200 80% OFF
🌍 International: $5 USD
One-time · Lifetime access · No subscription
🔒 Secured by Razorpay · UPI · Cards · NetBanking
🎉

You're in!

Payment successful! Your Buddy Bot eBook is ready. Time to build!

📖 Access Your eBook Now
🎉

Enrolled!

Payment successful! Lifetime access to all 21 Pico Projects is yours!

🍓 Go to My Course