Voice-Controlled Security Door Lock using ESP32 + Face Detection

Voice-Controlled Security Door Lock using ESP32 + Face Detection

IoT Projects / ESP32 / AI + Automation / Smart Home

In today’s world, security is one of the most important needs for every home, office, shop, or private space. Traditional key-based door locks can be easily lost, duplicated, or even broken. To solve this problem, smart automation and AI-based technology bring a modern solution — a Smart Door Lock System using ESP32, Face Recognition Camera (ESP32-CAM), and IoT (Blynk App / IFTTT / Firebase / Google Assistant).

This project allows you to:

Unlock your door using your Face (AI Face Recognition)
Control the door from a mobile app (Blynk IoT / Google Assistant / Alexa)
Automatically notify the owner via mobile if an unknown person tries to access the door
Open/Close lock using keypad, RFID card, fingerprint, or voice command (optional)
Store entry logs and monitor door status in real-time on your phone

It is a perfect DIY project for students, engineers, IoT developers, security lovers, and smart home enthusiasts.


🎯 Project Goals

GoalDescription
✅ Smart & KeylessUnlock door without using any physical key
✅ AI-Powered SecurityESP32-CAM identifies the face and only allows authorized people
✅ Mobile App ControlLock/unlock via Blynk or Google Assistant from anywhere
✅ Real-Time AlertsSends push notifications if unauthorized access is detected
✅ Easy HardwareUses low-cost modules like ESP32, relay, servo/solenoid lock
✅ ExpandableCan add fingerprint, keypad, RFID, GSM module in future

🤖 How This System Works (Simple Explanation)

  1. Face Detection/Recognition:
    ESP32-CAM scans the face using the built-in OV2640 camera and compares it with saved authorized face data.
  2. ESP32 Wi-Fi Module:
    If the face is authenticated, the ESP32 sends a signal to the servo motor or electronic door lock.
  3. Mobile App (Blynk/IFTTT):
    You can also open or close the lock manually using your smartphone.
  4. Notification System:
    If someone unknown tries to open the door, the system captures their photo and sends it to your phone via email or Blynk alert.
  5. Door Lock Mechanism:
    A servo motor / solenoid lock is connected to the ESP32 which physically locks/unlocks the door.

📦 Applications of This Project

✔ Smart Homes
✔ Office & Private Cabins
✔ Hostel, PG, Apartment Security
✔ Lab/Server Room Access Control
✔ Hotels & AirBnB Smart Entry Systems
✔ School & College Entrance Gates


💡 Why Smart Door Lock is Better than Traditional Locks?

Traditional Door LockSmart Door Lock (This Project)
Uses key onlyKeyless – Face, App, Voice control
Keys can be lost or copiedEncrypted & secured access
No remote accessOpen door from anywhere via Wi-Fi
No alert systemSends alerts if unauthorized access
Manual operation onlyFully automatic and smart

⚙️ Part 2 – Components & Working of AI + IoT Smart Door Lock System

In this section, we will understand all the components used in this project, their purpose, and how they work together to make a fully smart security system. Every component has been selected based on performance, cost-effectiveness, and compatibility with IoT and AI features.


🧾 🔹 Required Components List

Component NameQuantityPurpose
ESP32 / ESP32 Dev Board1Main Wi-Fi microcontroller to control the system
ESP32-CAM (AI Camera Module)1For face detection and recognition
OV2640 CameraInbuiltCaptures face images
Servo Motor / Solenoid Lock1For physically locking/unlocking the door
Relay Module (5V/3.3V)1To control the door lock or electromagnetic latch
Power Supply (5V/2A or 12V with buck converter)1Provides stable power to ESP32, camera & lock
Push Button / Doorbell Switch (optional)1Manual door unlocking/doorbell feature
Breadboard / PCB / WiresFor circuit connections
Smartphone with Blynk App / IFTTT / Google Home1To control and monitor door remotely
Micro SD Card (optional)1To store captured images or logs
Jumper Wires (Male/Female)For connection between modules

🧠 🔹 How Each Component Works

✅ 1. ESP32 Development Board

  • Acts as the brain of the system.
  • Connects to Wi-Fi and Blynk/MQTT/IFTTT cloud.
  • Controls relay/servo motor based on face detection or mobile app command.

✅ 2. ESP32-CAM (Face Recognition Module)

  • Comes with OV2640 camera sensor which captures live face images.
  • Uses inbuilt AI-based face detection algorithm.
  • Can store registered faces and compare them in real-time.

✅ 3. Servo Motor / Electronic Lock

  • This is the physical lock/unlock mechanism.
  • When an authorized face is detected or unlock command is received, the servo rotates and opens the latch.
  • Compact and consumes very low power.

✅ 4. Relay Module

  • Works like an electronic switch.
  • Used when controlling a magnetic/solenoid lock instead of servo.
  • Can handle high voltage locks safely using ESP32 signals.

✅ 5. Power Supply

  • ESP32 and servo/lock require a stable 5V or 12V supply.
  • For safety, use a 5V 2A adapter or 12V adapter + buck converter (to 5V).

✅ 6. Mobile App (Blynk / IFTTT / Google Assistant)

  • Allows door control from anywhere in the world via the internet.
  • You receive real-time notifications or emails when someone tries to unlock the door.
  • Can integrate voice commands like:
    “Hey Google, unlock the door”

🔄 🔹 Working Principle (Step-by-Step Explanation)

  1. Person stands in front of door → Camera scans face
  2. ESP32-CAM detects if the face is known or unknown
  3. ✅ If Authorized Face → Lock opens automatically
  4. ❌ If Unknown Person → Alert sent to owner (photo + notification)
  5. You can also:
    • Unlock door manually from Blynk App / Google Assistant
    • Use push button or RFID/Fingerprint (optional)
  6. Door automatically locks after a delay for safety.

📊 🔹 System Flowchart Overview

[Start]
   ↓
Camera Detects Face
   ↓
Is Face Recognized?
   ↓ Yes                    ↓ No
Unlock Door          Send Alert + Capture Photo
   ↓                        ↓
Send Data to App      Wait for Owner's Response
   ↓
Auto Lock After Delay

🔌 Part 3 – Circuit Diagram, Wiring & Hardware Connections

In this part, we will understand how to connect all the components of the AI + IoT Smart Door Lock System properly. सही wiring करना बहुत जरूरी है क्योंकि गलत connections से ESP32 खराब हो सकता है या camera और relay काम नहीं करेंगे।


🖇️ 🔹 Hardware Connection Overview

We are using:

  • ESP32-CAM (for Face Recognition)
  • ESP32 / NodeMCU ESP32 Dev Board (optional if using Blynk or Google Assistant control)
  • Servo Motor / Relay + Solenoid Lock (for locking system)
  • 5V Power Supply (for stable operation)

🧠 1. ESP32-CAM Power & Serial Wiring

ESP32-CAM PinConnection
5V / VCC5V Power Supply
GNDPower Supply GND
U0R (RX)ESP32 / USB to TTL TX pin
U0T (TX)ESP32 / USB to TTL RX pin
GPIO 0GND (only while uploading code)
3.3V OutputCan be used to power small sensors (optional)

✔ Upload करते समय GPIO0 को GND से connect करें
✔ Code upload होने के बाद GPIO0 को हटाएँ और reset करें


🔐 2. Servo Motor / Solenoid Lock Wiring

📌 If using Servo Motor (SG90 / MG995):

Servo WireConnect To
Red5V Power Supply
Brown / Black (GND)GND of ESP32
Orange / Yellow (Signal)GPIO 12 (or 13/15) of ESP32-CAM

📌 If using Relay + Electronic Lock:

Relay Module PinConnect To
VCC5V
GNDGND
INGPIO 14 (or any digital pin on ESP32-CAM)
NO (Normally Open)One wire of lock
COM (Common)Power source for lock (12V/5V depending on lock)

📡 3. ESP32 / Blynk / IoT Control Board (Optional but Recommended)

If you want Google Assistant, Alexa, Mobile App control, then add an ESP32 Dev Board separately.

ESP32 Dev BoardConnection
VIN (5V)5V Adapter
GNDCommon Ground
TX / RXFor communication with ESP32-CAM (optional for inter-board UART)
GPIO Pin (D5/D12)Connected to Relay IN or Servo signal
3.3VUsed to power logic devices or sensors

4. Power Supply Guide (Very Important)

ComponentVoltageCurrent Required
ESP32-CAM5V500mA
ESP32 Dev Board5V300mA
Servo Motor5V500mA to 1A
Solenoid Lock5V or 12V1A to 2A
Relay Module5V70mA

⚠️ Important Note:

  • ESP32 का 5V pin directly high-load devices जैसे servo & lock को power न दे
  • Use separate 5V 2A adapter or 12V + Buck Converter
  • All grounds (GND) must be connected together (common ground)

🖼️ Circuit Diagram (Explanation)

You can create this using tools like Fritzing / Proteus / Tinkercad, but the connections are clearly explained above.

Circuit Highlights:
✅ ESP32-CAM → Camera + Wi-Fi + AI Face Detection
✅ Servo/Relay → Locking Mechanism Control
✅ Power Supply → Stable 5V/12V Source
✅ ESP32 Dev Board → IoT + App + Voice Assistant Control

Part 4 (Continue) – Detailed Code Explanation Line-by-Line

अब हम ऊपर दिए गए ESP32 Main Board कोड को आसान भाषा में step-by-step समझेंगे ताकि आप या आपका viewer इसे कॉपी ही नहीं बल्कि properly समझकर implement भी कर सके।


📌 1. Header Files & Template Configuration

#define BLYNK_TEMPLATE_ID "Your_Template_ID"
#define BLYNK_AUTH_TOKEN "Your_Blynk_Token"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <WebServer.h>

🔹 What this means?

  • BLYNK_TEMPLATE_ID → Blynk App में बनाया गया Template ID
  • BLYNK_AUTH_TOKEN → Blynk से Email पर भेजा गया token
  • WiFi.h और WiFiClient.h → ESP32 को Wi-Fi से connect करने के लिए
  • ESP32Servo.h → Door Lock Servo को control करने के लिए
  • WebServer.h → ESP32 को web server बनाने के लिए (face recognition से unlock request receive करने के लिए)

📌 2. Wi-Fi Credentials & Hardware Pins

char ssid[] = "Your_WiFi_Name";
char pass[] = "Your_WiFi_Password";

Servo lockServo;
int servoPin = 12;  
int relayPin = 14;  
WebServer server(80);

ssid और pass = Wi-Fi connection के लिए
servoPin 12 = Motor जो लॉक को घुमाएगा
relayPin 14 = अगर आप solenoid lock या magnetic lock use कर रहे हैं
server(80) → ESP32 को HTTP requests सुनने के लिए port 80 पर ready रखता है


📌 3. Setup Function – Start Everything Here

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nConnected to Wi-Fi!");
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  lockServo.attach(servoPin);
  pinMode(relayPin, OUTPUT);
  lockServo.write(0); // Default Locked Position

  server.on("/unlock", []() {
    unlockDoor();
    server.send(200, "text/plain", "Door Unlocked");
  });

  server.begin();
}

🔹 Explaination:

FunctionPurpose
Serial.begin()Debugging messages
WiFi.begin()Connect ESP32 to internet
Blynk.begin()Connect ESP32 with Blynk Cloud
lockServo.attach()Initialize Servo Motor
server.on("/unlock")जब भी browser या ESP32-CAM इस URL को hit करेगा → Door unlock होगा

Example:
If you visit → http://192.168.1.50/unlock → Door will open automatically.


📌 4. Unlock Function

void unlockDoor() {
  Serial.println("Unlocking Door...");
  digitalWrite(relayPin, HIGH);
  lockServo.write(90);
  delay(3000);
  digitalWrite(relayPin, LOW);
  lockServo.write(0); // Lock again
}

✔ Servo rotates 0° → 90° to open lock
✔ Relay gives power to solenoid lock
✔ 3 seconds delay = Enough to open the door
✔ Then automatically locks again for safety


📌 5. Blynk Voice/Manual Control Button

BLYNK_WRITE(V0) {
  unlockDoor();
}

✔ जब भी आप Blynk App में बटन दबाएंगे → ये function call होगा
✔ Google Assistant + Blynk भी इसी pin V0 को trigger करेगा


📌 6. Main Loop Function

void loop() {
  Blynk.run();
  server.handleClient();
}

Blynk.run() = Real-time connection to mobile app
server.handleClient() = Handle face recognition unlock request from ESP32-CAM


✅ Part 4 में अब तक क्या पूरा हुआ?

✔ ESP32-CAM + ESP32 कोड पूरा
✔ WebServer + IoT + Servo Lock + Relay Control
✔ Line-by-line explanation
✔ 100% working concept for YouTube/Blog/Project

📍 Part 5: Complete ESP32 Code + Sensor Logic + Blynk Integration


🛠️ 5.1 Required Libraries (Add in Arduino IDE):

Arduino IDE में यह Libraries जरूर Install करें:

Library NameInstall From
Blynk by Volodymyr ShymanskyyLibrary Manager
ESP32 Board PackageBoard Manager
SimpleTimer (auto included with Blynk)Comes with Blynk

5.2 Complete Arduino Code (With Comments)

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_DEVICE_NAME "Water Level System"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define TRIG_PIN  13  // D13 → Trig Pin
#define ECHO_PIN  12  // D12 → Echo Pin
#define RELAY_PIN 14  // Motor Control (Optional)

char auth[] = "YOUR_AUTH_TOKEN";   // Blynk Auth Token
char ssid[] = "YOUR_WIFI_NAME";    // WiFi Name
char pass[] = "YOUR_WIFI_PASSWORD"; // WiFi Password

long duration;
int distance;
int tankHeight = 30;  // Tank Height in cm
int waterLevel;

BlynkTimer timer;

// Function to get distance from ultrasonic sensor
int getWaterLevel() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;  // Convert to cm

  waterLevel = map(distance, 0, tankHeight, 100, 0);
  if (waterLevel > 100) waterLevel = 100;
  if (waterLevel < 0)   waterLevel = 0;

  return waterLevel;
}

// Send data to Blynk app
void sendData() {
  int level = getWaterLevel();
  Blynk.virtualWrite(V1, level);  // Gauge
  Blynk.virtualWrite(V4, level);  // Graph

  if (level >= 90) {
    Blynk.virtualWrite(V2, "Tank Full");
    Blynk.virtualWrite(V3, 255); // Red LED
    digitalWrite(RELAY_PIN, LOW);  // Auto Motor Off
  }
  else if (level <= 10) {
    Blynk.virtualWrite(V2, "Tank Empty");
    Blynk.virtualWrite(V3, 255);
  }
  else {
    Blynk.virtualWrite(V2, "Normal");
    Blynk.virtualWrite(V3, 0);
  }
}

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

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH); // Motor initially Off

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(2000L, sendData); // Send data every 2 seconds
}

void loop() {
  Blynk.run();
  timer.run();
}

5.3 Code कैसे काम करता है? (Simple Explanation)

StepDescription
1Trig Pin से Sound Wave भेजी जाती है
2Echo Pin से Reflection Time Measure होता है
3Formula से Distance निकाला जाता है (cm में)
4Distance को Tank Height के हिसाब से Water % में Convert किया जाता है
5Blynk App → Gauge, Label, Graph पर Data भेजते हैं
6अगर पानी 90%+ है → Motor को Auto OFF & Alert
7अगर पानी 10% से कम है → Low Level Alert

5.4 Output क्या दिखेगा?

✔ Mobile App पर Real-Time Percentage
✔ Status Text → “Tank Full / Normal / Empty”
✔ LED Widget रंग बदलेगा Alert के समय
✔ Graph पर Live Data Update
✔ (Optional) Relay से Motor ON/OFF Auto Control


🎯 Part 5 Complete!

अब अगले Part 6 में हम Blynk App Dashboard, Widgets Setup (Gauge, Graph, LED) properly डिज़ाइन करेंगे।

Part 6: Blynk IoT App Setup & Dashboard Design

In this section, we will set up the Blynk IoT mobile app and web dashboard to monitor the water level, view notifications, and control the water pump remotely.


📱 6.1 Create a New Project in Blynk IoT (Mobile App)

  1. Open the Blynk IoT App (Android / iOS).
  2. Tap on “+ New Device” → “Create Device”
  3. Select:
    • Template Name: Smart Water Level Monitoring
    • Hardware: ESP32
    • Connection Type: Wi-Fi
  4. Blynk will automatically generate:
    Template ID
    Device Name
    Auth Token → Copy and paste it into your ESP32 code.

📊 6.2 Add Widgets in Blynk App

Widget NamePurposeVirtual Pin
GaugeDisplays % water levelV1
LabelShows status (Full/Low/Normal)V2
LEDAlert IndicatorV3
SuperChart (Graph)Real-time water level graphV4
Button (Optional)Manual pump on/off controlV5
NotificationSends alert pop-upsAuto

🛠 How to Configure Widgets:

1. Gauge Widget

  • Name: Water Level
  • Pin: V1
  • Range: 0 to 100
  • Unit: %

2. Label Widget

  • Name: Status
  • Pin: V2
  • Value Display: Tank Full / Empty / Normal

3. LED Widget

  • Name: Alert LED
  • Pin: V3
  • Color Mode: Auto

4. SuperChart (Graph Widget)

  • Add a Data Stream:
    • Name: WaterLevel
    • Pin: V4
    • Type: Percentage (%)
    • Update Interval: 1s or 5s

5. Button Widget (Optional for Motor Control)

  • Name: Motor Switch
  • Mode: Switch
  • Pin: V5
  • If using relay with ESP32: handle in code with BLYNK_WRITE(V5)

🌐 6.3 Blynk Web Dashboard Setup (Optional but Professional)

  1. Visit: https://blynk.cloud
  2. Log in using the same account
  3. Go to: My Devices → Your Device → Dashboard
  4. Add widgets (Gauge, Chart, Switch, Indicators)
  5. Link same Virtual Pins: V1, V2, V3, V4, V5
  6. Save and view real-time water level on browser

📩 6.4 Enable Alerts & Notification System

✅ For Tank Full or Empty Alerts:

  1. Go to Event → Add New Event
    • Name: Tank_Full
    • Condition: When V2 = “Tank Full”
  2. Enable → Send Push Notification or Send Email

6.5 Final Output of Blynk App

✔ Real-time water percentage on gauge
✔ Tank full / empty alerts on phone
✔ Live water-level graph over time
✔ Pump control from mobile or automatically
✔ Notification when water is low or overflows

📞 Contact YaranaIoT Guru Empowering IoT Innovation | ESP32 | Home Automation | Smart Solutions | 50K+ Community

We’d love to hear from you! Whether you have questions about IoT projects, collaboration opportunities, technical support, custom PCB design, bulk kit orders, corporate training, college workshops, or freelance developmentwe’re just one message away.


✉️ Email (Official)

For all inquiries, project support, business collaboration, sponsorships, or documentation requests: 📩 contact@yaranaiotguru.in 📧 Alternate: support@yaranaiotguru.inResponse Time: Within 24 hours (Mon–Sat) 💡 Use email for detailed queries with attachments (code, schematics, etc.)


📱 Phone / WhatsApp (24×7 Support)

Instant live technical help, project consultation, troubleshooting, or order status: 📞 +91 70527 22734 💬 WhatsApp: Chat NowCall Timing: Mon–Sat, 10 AM – 7 PM IST 🚀 Emergency Support: WhatsApp anytime (reply within 1 hour)


🌐 Official Website

Full project tutorials, downloadable code, PDF guides, schematics, blogs, free tools, and online store: 🔗 www.yaranaiotguru.in 🛒 Shop: yaranaiotguru.in/shop (ESP32 DevKits, Sensors, Relays, Custom PCBs, Project Kits)


▶️ YouTube Channel

Step-by-step IoT tutorials, live coding, project builds, ESP32 programming, Blynk, Node-RED, MQTT, Home Assistant, and real-time demos: 🔗 Yarana IoT Guru 📺 1,200+ Videos | 52K+ Subscribers | 5.5M+ Views | 4.8★ Rating 🎥 New Video Every Week 🔔 Subscribe & Turn On Notifications


🛠 GitHub (100% Open Source)

All source codes, Arduino IDE sketches, PlatformIO projects, Node-RED flows, MQTT configs, and full documentation: 🔗 github.com/YaranaIotGuru50+ Repositories | 10K+ Stars & Forks 📂 Top Projects:

  • ESP32 WebSocket Real-Time Dashboard
  • Smart Home with Blynk & Alexa
  • IoT Irrigation System with Soil Moisture
  • MQTT + Node-RED + MySQL Logging
  • OLED Weather Station with API

📸 Instagram

Daily IoT reels, quick tips, project teasers, live builds, student showcases, and giveaways: 🔗 @YaranaIoTGuru 📱 10K+ Followers | Reels | Stories | Live Sessions


💼 LinkedIn (Professional Network)

For B2B collaborations, IoT consulting, corporate training, startup partnerships, hiring, and industry insights: 🔗 Yarana IoT Guru 🤝 Services Offered:

  • Custom IoT Product Development
  • Embedded Systems Training
  • College Workshops & FDPs
  • PCB Design & Prototyping

🐦 Twitter / X

Real-time updates, tech news, poll-based Q&A, project launches, and community engagement: 🔗 @YaranaIoTGuru 📢 Follow for instant alerts


🛠 Hackster.io (Project Showcases)

In-depth project write-ups, circuit diagrams, bill of materials (BOM), code explanations, and user ratings: 🔗 hackster.io/yaranaiotguru 🏆 50+ Published Projects | 100K+ Views | Top 5% Creator


📝 Instructables (DIY Guides)

Beginner-friendly step-by-step guides, printable templates, tool lists, and troubleshooting sections: 🔗 instructables.com/member/YaranaIoTGuru 🛠 Featured Instructables:

  • Automatic Plant Watering System
  • Voice-Controlled Home Appliances
  • WiFi-enabled Temperature Monitor

📚 Medium Blog

Technical articles, IoT trends, tutorials, case studies, and career tips in embedded systems: 🔗 medium.com/@yaranaiotguru ✍️ 50+ Articles | 15K+ Readers


🛒 Online Store & Kits

Ready-to-use IoT project kits, custom PCBs, sensors, modules, and merch: 🔗 yaranaiotguru.in/shop 📦 Free Shipping in India above ₹999 💳 Payment: UPI, Card, Wallet, COD


🌟 Community Platforms

PlatformLinkPurpose
Telegram Channelt.me/YaranaIoTGuruProject files, PDFs, updates
Telegram Groupt.me/YaranaIoTCommunityPeer support, doubts
Discord Serverdiscord.gg/yarana-iotLive voice help, coding rooms
WhatsApp CommunityJoin HereAnnouncements & polls

🏢 Office & Studio Address

Yarana Studio & Software (Yarana IoT Guru HQ) 📍 Near Rookh Baba Mandir Umariya Badal Urf Gainda, Allahabad (Prayagraj), Uttar Pradesh – 212507, IndiaGoogle Rating: 5.0 ★ (Based on 100+ Reviews) 🕒 Opening Hours:

  • Monday – Saturday: 10:00 AM – 5:00 PM
  • Sunday: Closed 🌐 Associated Website: yaranawebtech.in🗺️ View on Google Maps(Search: “Yarana Studio & Software”)📌 Walk-ins welcome | Appointment recommended for project discussions

Leave a Reply

Your email address will not be published. Required fields are marked *