Logo

ESP32 Face Recognition-Based Door Unlock System

Introduction to Face Recognition Door Unlock with ESP32

In this project, we are going to create a smart door lock system that uses face recognition technology with the ESP32-CAM board. This system captures images using the onboard camera and processes them using built-in facial recognition features. If the face is recognized as a stored user, the system triggers a relay to unlock the door. This project is a practical example of combining AI and IoT for home automation and security.

Components Required

To build the ESP32-CAM-based face recognition system, you’ll need the ESP32-CAM module, a USB-to-Serial FTDI programmer for uploading code, a relay module to control door lock, and a power supply for the ESP32-CAM board.

  • Connect the ESP32-CAM with FTDI programmer for uploading the sketch.
  • Install the required board packages in Arduino IDE (ESP32 boards by Espressif).
  • Upload the face recognition web server code to the ESP32-CAM.
  • Access the camera interface via IP address and register new faces.
  • Connect a relay module to control the electronic door lock.
  • Test the system by showing a registered face — the door should unlock.
  • Unrecognized faces will not trigger the unlock function.

Formula: Unlock Logic: if (recognizedFace == true) { relay.setState(ON); delay(5000); relay.setState(OFF); }

Components List

  • 1 × ESP32-CAM Module
  • 1 × FTDI USB-to-Serial Programmer
  • 1 × Relay Module
  • 1 × Electronic Door Lock or Solenoid Lock
  • 1 × Power Supply (5V 2A for ESP32-CAM)
  • 1 × Breadboard and Jumper Wires
  • 1 × Wi-Fi Network

Pin Configuration

  • GPIO 0 (Flash Mode): Connect to GND during uploading the code via FTDI.
  • GPIO 12 (Relay Control): Used to control the relay module that unlocks the door.
  • U0R / U0T: TX/RX pins used for communication with FTDI during programming.
  • GND & 5V: Power supply and ground connections for ESP32-CAM and other modules.

Ensure that your ESP32-CAM module receives a stable 5V 2A power source to avoid boot or camera failures.

Wiring and Connections

  • -> U0R
  • -> U0T
  • -> GND
  • -> 5V
  • -> GND (Only while uploading)
  • -> GPIO 12
  • -> 5V & GND
  • ->

Code for ESP32 Face Recognition Door Unlock

1// Use the default ESP32-CAM example in Arduino IDE: File > Examples > ESP32 > Camera > CameraWebServer
2// After uploading, access IP address to register faces and control the door lock.
3
4#include "esp_camera.h"
5#include <WiFi.h>
6
7#define CAMERA_MODEL_AI_THINKER
8#include "camera_pins.h"
9
10const char* ssid = "Your_SSID";
11const char* password = "Your_PASSWORD";
12
13#define RELAY_PIN 12
14
15void startCameraServer();
16
17void setup() {
18  Serial.begin(115200);
19  pinMode(RELAY_PIN, OUTPUT);
20  digitalWrite(RELAY_PIN, LOW);
21
22  camera_config_t config;
23  config.ledc_channel = LEDC_CHANNEL_0;
24  config.ledc_timer = LEDC_TIMER_0;
25  config.pin_d0 = Y2_GPIO_NUM;
26  config.pin_d1 = Y3_GPIO_NUM;
27  config.pin_d2 = Y4_GPIO_NUM;
28  config.pin_d3 = Y5_GPIO_NUM;
29  config.pin_d4 = Y6_GPIO_NUM;
30  config.pin_d5 = Y7_GPIO_NUM;
31  config.pin_d6 = Y8_GPIO_NUM;
32  config.pin_d7 = Y9_GPIO_NUM;
33  config.pin_xclk = XCLK_GPIO_NUM;
34  config.pin_pclk = PCLK_GPIO_NUM;
35  config.pin_vsync = VSYNC_GPIO_NUM;
36  config.pin_href = HREF_GPIO_NUM;
37  config.pin_sscb_sda = SIOD_GPIO_NUM;
38  config.pin_sscb_scl = SIOC_GPIO_NUM;
39  config.pin_pwdn = PWDN_GPIO_NUM;
40  config.pin_reset = RESET_GPIO_NUM;
41  config.xclk_freq_hz = 20000000;
42  config.pixel_format = PIXFORMAT_JPEG;
43
44  if(psramFound()){
45    config.frame_size = FRAMESIZE_UXGA;
46    config.jpeg_quality = 10;
47    config.fb_count = 2;
48  } else {
49    config.frame_size = FRAMESIZE_SVGA;
50    config.jpeg_quality = 12;
51    config.fb_count = 1;
52  }
53
54  esp_err_t err = esp_camera_init(&config);
55  if (err != ESP_OK) {
56    Serial.printf("Camera init failed with error 0x%x", err);
57    return;
58  }
59
60  WiFi.begin(ssid, password);
61  while (WiFi.status() != WL_CONNECTED) {
62    delay(500);
63    Serial.print(".");
64  }
65  Serial.println("");
66  Serial.println("WiFi connected");
67  Serial.print("Camera Stream Ready! Go to: http://");
68  Serial.println(WiFi.localIP());
69
70  startCameraServer();
71}
72
73void loop() {
74  // Use web interface to manage face recognition
75}

Code Explanation

  • #include "esp_camera.h": Includes the ESP32 camera library required for image capturing.
  • camera_config_t config;: Creates a configuration structure to define the ESP32-CAM's pin mappings and settings.
  • WiFi.begin(ssid, password);: Connects the ESP32-CAM to your local Wi-Fi network.
  • startCameraServer();: Starts the built-in camera web server for streaming and face recognition.
  • pinMode(RELAY_PIN, OUTPUT);: Sets up the relay control pin to unlock the door when the face is recognized.

Applications

  • Smart door locks with facial authentication.
  • AI-based attendance systems for offices and schools.
  • Security access control for restricted areas.
  • DIY home automation projects involving biometrics.

Conclusion

This ESP32-CAM-based face recognition door lock project offers an affordable way to implement AI-powered access control. By using the ESP32-CAM's onboard camera and facial detection, the system provides enhanced home security and user convenience. The project is a great demonstration of IoT, computer vision, and real-world automation working together.