ESP32-CAM Face Recognition Door Lock

 1. Introduction

Security systems are becoming smarter with the integration of Artificial Intelligence and IoT. In this project, we build a real-world smart door lock system using the powerful ESP32-CAM.

Unlike basic projects that use servo motors, this system uses a solenoid lock, which is widely used in real access control systems. Additionally, an SD card is used to store captured images, ensuring data is not lost after power reset.

When a face is detected and verified, the system unlocks the door using the solenoid lock. All captured images are stored in the SD card, making it a practical and reliable security system.


2. Components Required

  • ESP32-CAM Module
  • FTDI Programmer
  • Solenoid Door Lock (12V, Fail-Secure recommended)
  • Relay Module (5V)
  • Micro SD Card (8GB recommended)
  • 12V Power Supply
  • Jumper Wires
  • Breadboard
  • Flyback Diode (1N4007 recommended)

3. Circuit and Connections

ESP32-Camwiring code upload

Pinout Diagram of Single Channel Relay Module
Pinout Diagram Relay Module

Typical Solenoid Lock
Pinout Diagram of a Typical Solenoid Lock


🔌 ESP32-CAM Programming Connection

  • 5V → 5V
  • GND → GND
  • U0R → TX
  • U0T → RX
  • GPIO0 → GND (upload mode)

🔌 Relay + Solenoid Connection

  • GPIO 12 → Relay IN
  • Relay COM → 12V (+)
  • Relay NO → Solenoid (+)
  • Solenoid (–) → GND

⚠️ Flyback Diode (IMPORTANT)

  • Connect diode across solenoid:
    • Cathode → +
    • Anode → –

✔ Protects circuit from voltage spikes


💾 SD Card

✔ Already inside ESP32-CAM
✔ No wiring needed


4. Detailed Step-by-Step Circuit Working

  1. ESP32-CAM initializes camera and SD card
  2. System starts face detection
  3. When a face appears:
    • Image is captured
    • Stored in SD card
  4. If face is recognized:
    • Relay activates
    • Solenoid unlocks door
  5. After delay:
    • Lock automatically engages

5. Code

#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"

#define LOCK_PIN 12

void setup() {
Serial.begin(115200);

pinMode(LOCK_PIN, OUTPUT);
digitalWrite(LOCK_PIN, LOW);

// Initialize SD Card
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}

if(SD_MMC.cardType() == CARD_NONE){
Serial.println("No SD Card attached");
return;
}

Serial.println("SD Card Ready");

// Camera configuration
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = 5;
config.pin_d1 = 18;
config.pin_d2 = 19;
config.pin_d3 = 21;
config.pin_d4 = 36;
config.pin_d5 = 39;
config.pin_d6 = 34;
config.pin_d7 = 35;
config.pin_xclk = 0;
config.pin_pclk = 22;
config.pin_vsync = 25;
config.pin_href = 23;
config.pin_sscb_sda = 26;
config.pin_sscb_scl = 27;
config.pin_pwdn = 32;
config.pin_reset = -1;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;

esp_camera_init(&config);
}

void loop() {

camera_fb_t * fb = esp_camera_fb_get();

if(!fb){
Serial.println("Camera capture failed");
return;
}

// Save image to SD Card
String path = "/img_" + String(millis()) + ".jpg";
File file = SD_MMC.open(path, FILE_WRITE);

if(file){
file.write(fb->buf, fb->len);
Serial.println("Saved: " + path);
} else {
Serial.println("File write failed");
}

file.close();
esp_camera_fb_return(fb);

// Simulated face match (replace with real detection)
bool faceMatched = true;

if(faceMatched){
Serial.println("Access Granted");

digitalWrite(LOCK_PIN, HIGH); // Unlock
delay(3000);
digitalWrite(LOCK_PIN, LOW); // Lock
}

delay(5000);
}

6. Detailed Step-by-Step Code Working

  • esp_camera.h → Captures images
  • SD_MMC.h → Stores images
  • LOCK_PIN → Controls relay

Setup:

✔ Initializes SD card
✔ Configures camera
✔ Sets lock pin

Loop:

✔ Captures image
✔ Saves to SD card
✔ Checks face match
✔ Activates solenoid


7. Tips

✔ Use dedicated 12V power supply
✔ Do not power solenoid from ESP32
✔ Ensure proper lighting
✔ Use heat sink if running continuously


8. Uses

  • Smart Door Lock Systems
  • Office Access Control
  • Home Automation
  • Security Surveillance

9. Limitations

  • Basic face matching (ESP32 limitation)
  • Needs backup authentication
  • Sensitive to lighting conditions
⚠️ This system is designed as a practical smart lock prototype. For high-security applications, combine it with RFID, keypad,
or cloud-based face recognition.


10. Conclusion

This project demonstrates a real smart door lock system using ESP32-CAM with solenoid locking and SD card storage. Compared to servo-based systems, it offers better security and reliability.

Although ESP32 has limitations, this system is a strong foundation for building advanced IoT-based security solutions.


Note: For Beginners Look at the Following Link

https://simplebasicelectronics.blogspot.com/2026/03/arduino-fingerprint-door-lock-system-r307.html

https://simplebasicelectronics.blogspot.com/2026/03/arduino-rfid-door-lock-system-rc522.html

à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ി ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

Empowering students in Kerala with hands-on technical skills.