Mod edit, see also:
Description:
This is a multi-layered smart security lock that unlocks doors using:
- Face Recognition (via Python + OpenCV)
- RFID card authentication
- Remote unlock feature via IoT (Blynk or custom app)
- Failsafe backup via password on keypad
It’s built using Arduino + Raspberry Pi (or PC) and integrates hardware + AI + IoT, making it an ideal final-year project or flagship portfolio piece.
Advanced Features:
- Face detection using Python + OpenCV (runs on Raspberry Pi or PC).
- Dual authentication: face + RFID.
- Optional keypad to unlock with password.
- Mobile control using Blynk.
- Sends alert to phone on unauthorized access attempt.
- Logs entry time + person ID in a CSV file or cloud database.
Components List:
| Component | Quantity |
|---|---|
| Arduino Uno | 1 |
| Raspberry Pi (or Laptop/PC) | 1 |
| USB Camera (for face detection) | 1 |
| MFRC522 RFID Module | 1 |
| Servo Motor (for lock mechanism) | 1 |
| 4x4 Matrix Keypad (Optional) | 1 |
| ESP8266 (or NodeMCU for IoT) | 1 |
| Jumper Wires, Breadboard | - |
| 12V Adapter or Power Supply | 1 |
Working Logic Flow:
- Camera captures the user's face.
- Python script checks face using OpenCV + trained model.
- If face is recognized → Arduino checks for RFID tag.
- If RFID tag matches → Servo unlocks the door.
- Logs access (with time, name) to file/cloud.
- Can be overridden using:
- Keypad password
- IoT (via Blynk mobile app)
Code Structure
Python (Face Recognition Part — runs on PC/RPi)
import cv2
import serial
ser = serial.Serial('COM3', 9600) # Replace with your Arduino port
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml') # Pretrained model
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
id_, conf = recognizer.predict(gray[y:y+h, x:x+w])
if conf < 60:
ser.write(b'U') # Send unlock signal to Arduino
else:
ser.write(b'L') # Send lock signal
cv2.imshow('Face', frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Arduino (Reads RFID + Actuates Lock)
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo lockServo;
char validUID[] = "A1B2C3D4"; // Example UID
char incomingChar;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
lockServo.attach(8);
lockServo.write(0); // Locked
}
void loop() {
if (Serial.available()) {
incomingChar = Serial.read();
if (incomingChar == 'U') {
checkRFID();
}
}
}
void checkRFID() {
if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;
String uidStr = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
uidStr += String(mfrc522.uid.uidByte[i], HEX);
}
if (uidStr == validUID) {
lockServo.write(90); // Unlock
delay(5000);
lockServo.write(0); // Re-lock
}
}
IoT Extension (Blynk):
- Create buttons in the Blynk app: Unlock / Lock
- ESP8266 receives commands and controls Arduino via serial or relay
- Optional: Add notification on failed face/RFID attempts
Use Case Summary:
| Situation | Outcome |
|---|---|
| Face matched + RFID match | Door unlocks |
| Unknown face or card | Door stays locked, alert sent |
| IoT unlock button pressed | Door unlocks via ESP |
| Keypad backup entered | Door unlocks manually |
Future Upgrades:
- Add biometric fingerprint sensor.
- Save logs to Firebase or Google Sheets.
- Add camera capture on failed attempts.
- Integrate with Google Assistant or Alexa.
Circuit Diagram Explanation (Component-Wise)
1. MFRC522 RFID Module (SPI Connection with Arduino)
This module uses SPI protocol, which means it connects to specific pins on the Arduino Uno.
| RFID Pin | Arduino Uno Pin |
|---|---|
| SDA | D10 |
| SCK | D13 |
| MOSI | D11 |
| MISO | D12 |
| RST | D9 |
| 3.3V | 3.3V |
| GND | GND |
Important Note: RFID MFRC522 works on 3.3V, not 5V.
2. Servo Motor (Door Lock Actuator)
Used to physically open/close the latch of a door when access is granted.
| Servo Wire | Arduino Uno Pin |
|---|---|
| Signal | D8 |
| VCC | 5V |
| GND | GND |
Servo rotates:
0°→ Locked90°→ Unlocked
Tip: Use an external power source for servo if it gets weak with Arduino power.
3. Serial Communication Between Arduino & Raspberry Pi/PC
- Arduino connects to Raspberry Pi / PC via USB Cable.
- Python (OpenCV) sends a character (e.g.,
Ufor unlock,Lfor lock) via Serial. - Arduino reads this signal with
Serial.read()and takes action.
No extra wires needed — just USB.
4. Optional: 4x4 Keypad for Backup Manual Password
If RFID or face detection fails, user can enter a pin.
| Keypad Rows (R1–R4) | Connect to Digital Pins (e.g., D2–D5) |
|---|---|
| Columns (C1–C4) | Connect to D6–D9 (or available pins) |
Use the Keypad.h library to handle password input.
5. Optional: ESP8266 (NodeMCU or ESP-01) for IoT Unlock
- Add a NodeMCU to receive Wi-Fi commands from Blynk or custom app.
- Use serial communication between ESP8266 and Arduino (via SoftwareSerial) OR
- ESP controls the relay/servo directly.
| ESP8266 Pin | Connection |
|---|---|
| TX | Arduino RX (via level shifter or voltage divider) |
| RX | Arduino TX |
| VCC | 3.3V (important!) |
| GND | GND |
Level shifting required if using Arduino Uno's 5V logic.
Overall Circuit Flow
(Python + OpenCV)
[Face Detection on PC/RPi]
|
v
Sends Serial 'U' (Unlock) or 'L' (Lock)
|
----------------------
| Arduino Uno (Brain) |
----------------------
|
------------------------------
| |
[RFID Reader] [Optional Keypad]
| |
Reads UID Accepts password fallback
|
Valid UID?
|
Yes -> [Servo Motor Unlocks Door]
|
[Relay/IoT Control Optional]
Recommended Power Setup:
- Arduino: via USB or 9V adapter
- Servo: Powered via external 5V 2A adapter
- RFID: 3.3V from Arduino
- ESP8266: Use dedicated 3.3V regulator like AMS1117