Smart Face Recognition Door Lock System

by dmcits in Circuits > Microcontrollers

7 Views, 1 Favorites, 0 Comments

Smart Face Recognition Door Lock System

Screenshot 2025-06-12 at 5.26.06 PM.png

A Raspberry Pi + Arduino project that uses facial recognition to unlock a door (servo motor) with visual and sound feedback! Based on: Core Electronics Guide

Supplies

Raspberry Pi 4 or 5 recommended

PiCamera or USB webcam

Arduino Uno

Servo motor - SG90

Common cathode RGB LED

Piezo buzzer

Jumper wires

Resistors: 220Ω

Breadboard

USB cable (to connect Pi ↔ Arduino)

Install Raspberry Pi OS

Using Raspberry Pi Imager, install the latest Bookworm OS on your Pi

Set Up Your Raspberry Pi for Face Recognition

Based on: Core Electronics Guide

1. Create a Virtual Environment

python3 -m venv --system-site-packages face_rec
source face_rec/bin/activate
💡 You’ll see (face_rec) in your terminal prompt—this means it’s active.

2. Install Dependencies

sudo apt update
sudo apt full-upgrade
pip install opencv-python
pip install imutils
sudo apt install cmake
pip install face-recognition

Train Your Face Recognition Model

1. Capture Training Images

Create a dataset/ folder with subfolders named after each person (e.g., john, alice). Capture photos using:

python3 image_capture.py
📂 Save each person’s photos in their respective folder. (Take multiple photos so the its more accurate recognizing faces)

2. Encode Faces

Generate the face encodings:

python3 model_training.py

This creates a encodings.pickle file used for recognition.

Build the Arduino Circuit

Door Unlock System (1).png
20250617_181422.jpg

Servo Motor - D2

RGB LED - Red → D3 Green → D5 Blue → D6

Piezo Buzzer - D7

#include <Servo.h>

const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
const int buzzerPin = 7;
const int servoPin = 2;

Servo doorServo;

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

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);

doorServo.attach(servoPin);
doorServo.write(0); // Start in locked position
}

void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');

if (command == "pass") {
handlePass();
}
else if (command == "fail") {
handleFail();
}
}
}

void handlePass() {
setColor(0, 255, 0); // Green LED
doorServo.write(90); // Unlock
playPositiveChime();
delay(5000);
doorServo.write(0); // Lock again
setColor(0, 0, 0); // LED off
}

void handleFail() {
setColor(255, 0, 0); // Red LED
doorServo.write(0); // Ensure it's locked
playNegativeChime();
delay(3000);
setColor(0, 0, 0); // LED off
}

void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}

void playPositiveChime() {
tone(buzzerPin, 880, 200); // A5
delay(250);
tone(buzzerPin, 988, 200); // B5
delay(250);
tone(buzzerPin, 1047, 300); // C6
delay(350);
noTone(buzzerPin);
}

void playNegativeChime() {
tone(buzzerPin, 300, 300); // Low buzz
delay(350);
tone(buzzerPin, 200, 300); // Lower buzz
delay(350);
noTone(buzzerPin);
}

Run Real-Time Face Detection

Use this modified script: facial_recognition_hardware.py

Key Customization:

  1. Sends "pass\n" or "fail\n" to the Arduino over serial (/dev/ttyACM0)
  2. Compares faces against authorized names list
  3. Draws bounding boxes and labels on live camera feed

I modified facial_recognition_hardware.py so that the Raspberry Pi communicates to the Arduino

To run:

python3 facial_recognition_hardware_Arduino.py

Make sure the authorized_names array has the names people from your dataset

authorized_names = ["john", "alice", "bob"]

Test the System:

  1. Connect the Pi to the Arduino
  2. Watch the Arduino react:
  3. Green LED + Servo turns → ✅ Authorized face
  4. Red LED + Buzz → ❌ Unknown face