Security is the number one concern of everyone today, whether it’s data security or the security of one’s own home. With the advancement of technology and the increasing popularity of the Internet of Things, digital door locks have become very common these days. The digital lock does not require any physical key, but it uses RFID, fingerprint, Face ID, pin, password, etc. to control the door lock. We have developed many digital door lock applications in the past using these different technologies. In this tutorial, we built a Face ID controlled digital door lock system using ESP32-CAM.

required components

ESP32 Cam

FTDI Board of Directors

Relay module

Electromagnetic lock


Electromagnetic lock

Electromagnetic locks work on electromechanical locking mechanisms. This type of lock has a molding with an angled cutout and a good mounting bracket. When energized, the direct current creates a magnetic field that moves the plug inside and keeps the door in the unlocked position. The warhead will hold its position until power is removed. When the power goes out, the slug moves outside and locks the door. It does not use any power in the locked state. To drive the electromagnetic lock, you need a power supply that can provide 12V @ 500mA.

We previously used an electromagnetic lock to build an Arduino based RFID door lock.

circuit schematic

The circuit diagram of ESP32-CAM face recognition door lock system is as follows:

The circuit above is combined with an FTDI board, a relay module and an electromagnetic lock. The FTDI board is used to flash code into the ESP32-CAM since it doesn’t have a USB connector, and the relay module is used to turn the solenoid lock on or off. The VCC and GND pins of the FTDI board and Relay module are connected to the Vcc and GND pins of the ESP32-CAM. The TX and RX of the FTDI board are connected to the RX and TX of the ESP32, and the IN pin of the relay module is connected to the IO4 of the ESP32-CAM.

Note: Ground IO0 before uploading code. IO0 determines whether the ESP32 is in blink mode. When GPIO 0 is connected to GND, the ESP32 is in blink mode.

After connecting the hardware according to the circuit diagram, it should look like this:

Install ESP32 Board on Arduino IDE

Here we use the Arduino IDE to program the ESP32-CAM. To do this, first, install the ESP32 plugin on the Arduino IDE.

To install the ESP32 board in your Arduino IDE, go to File > Preferences.

Now copy the link below and paste it into the “AddiTIonal Board Manager URLs” field as shown in the image below. Then, click the OK button:

Now go to Tools > Board > Board Manager

In the Board Manager, search for ESP32 and install “ESP32 by Espressif Systems”.

code description

We explained using the ESP32 for face recognition in the previous article; here we will modify the same code to control the electromagnetic door lock. The complete code is divided into four parts. One is the main code for the camera and relay module, the ESP32 locks or unlocks the door based on face recognition, and the other three codes are the web page, the camera index, and the camera pin. The complete code is given at the end of this page. Here we will explain some important parts of the code.

Start the program by including all library files.

#include “esp_camera.h”#include#include “camera_pins.h”

On the next line, uncomment the camera module you are using with the ESP32. In the code, five different camera models are defined. In this case, we are using the AI-THINKER model.


After that, insert your network credentials into the following variables:

const char* ssid = "Wi-Fi name";const char* password = "Wi-Fi password";

Then define the pins that connect the relay module. After unlocking within a defined time interval, we will use the millis() function to lock the door, here 5 seconds.

#define relay 4 long prevMillis = 0; integer interval = 5000;

In the setup() function, initialize the serial monitor at a baud rate of 115200 for debugging. Then in the next few lines, define the pin mode for the relay module and initially set the relay to low.

void set() { serialNumber.start(115200); pinMode(relay, output); digitalWrite(relay, low);

In the loop() function, check if the face matches the registered face. If yes, unlock the door for 5 seconds, then lock the door again after 5 seconds.

void loop() { if (matchFace == true && activeRelay == false) { activeRelay = true; digitalWrite(relay, high); prev Millis = millis(); } if(activeRelay == true && millis ()- prevMillis > interval) { active_relay = false; match_face = false; digital_write(relay, low); }

Test ESP32-CAM face recognition door lock system

Finally upload the code, connect the FDTI board to your laptop and select “ESP32 Wrover Module” as your board. Also, change other settings as shown in the image below:

Don’t forget to connect the IO0 pin to GND before uploading the code and press the ESP32 reset button and then click the upload button.

NOTE: If you get an error uploading the code, please check that IO0 is connected to GND and that you have selected the correct setting in the Tools menu.

After uploading the code, remove the IO0 and GND pins. Then open the serial monitor and change the baud rate to 115200. Then, press the ESP32 reset button and it will print the ESP IP address and port number. on the serial monitor.

Now navigate to a browser and enter the ESP IP address copied from the serial monitor to access the camera stream. It will take you to the streaming page. To start streaming, click the “Start Streaming” button at the bottom of the page.

To recognize faces using ESP32-CAM, first, we have to register faces. To do this, turn on Face Recognition and Detection from the settings and click the “Register Faces” button. It takes multiple attempts to save face. After saving the face, it detects the face as subject 0, where zero is the face number.

After registering a face, if a face is recognized in the video, the ESP32 will set the relay module high to unlock the door.

#include “esp_camera.h”
// warn! ! ! Make sure you have the ESP32 Wrover module selected,
// or another PSRAM enabled board
// select camera model
#include “camera_pins.h”
const char* ssid = “Galaxy-M20”;
const char* password = “ac312124”;
#define LED_BUILTIN 4
#define relay 4
#define buzzer 2
boolean matchface = false;
boolean acTIveRelay = false;
long prevMillis = 0;
integer interval = 5000;
void startCameraServer();
void set() {
serial number.println();
pinMode(relay, output);
pinMode(buzzer, output);
pinMode(LED_BUILTIN, output);
digitalWrite(LED_BUILTIN, low);
digitalWrite(relay, low);
digitalWrite(buzzer, low);
camera_config_t configuration;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
// initialize with high spec to preallocate larger buffer
if (psramFound()) {
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
#if defined (CAMERA_MODEL_ESP_EYE)
pinMode(14, INPUT_PULLUP);
#just in case
// camera initialization
esp_err_t err = esp_camera_init(&config);
if (error != ESP_OK) {
Serial.printf(“Camera initialization failed with error 0x%x”, err);
sensor_t * s = esp_camera_sensor_get();
// The initial sensor is flipped vertically and the color is a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1);//Flip back
s->set_brightness(s, 1);//Slightly increase the brightness
s->set_saturation(s, -2);//Reduce saturation
// reduce the frame size for a higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#just in case
WiFi.begin(ssid, password);
而(WiFi.status()!= WL_CONNECTED){
Serial number.println(“”);
Serial.println(“WiFi connection”);
Serial.print(“Camera is ready! Use ‘http://”);
Serial.println(“‘To connect”);
void loop() {
if (matchFace == true && activeRelay == false){
active_relay = true;
digitalWrite(relay, high);
digitalWrite(buzzer, high);
digitalWrite(buzzer, low);
Previous Millis = millis();
if(activeRelay == true && millis()- prevMillis > interval){
active_relay = false;
matchface = false;
digitalWrite(relay, low);

Leave a Reply

Your email address will not be published.