Face Recognition Security System using Arduino Nano and Husky Lens

Overview

In recent years, home security is becoming more automated with the utilization of robotics and smart devices. This innovation enhances security and convenience at the same time for better experience. This can range from simple to complex automated systems, similar to water dispensing, lighting and ventilation control, or gate and door security system. 

In this tutorial, we will explore HuskyLens, a machine learning ready vision sensor from DFROBOT which can easily be interfaced to most microcontroller boards similar to Arduino, Raspberry Pi, Micro:bit, etc. We will utilize HuskyLens using the popular Arduino platform as a means to create a DIY face recognition door lock system.

 

Hardware Used

 

Software Used

  • Arduino IDE

 

Application Discussion

  • Gravity: HuskyLens 

HuskyLens is a vision sensor created by DFROBOT. This can be used easily and is already machine ready. With several built-in function including face recognition, object tracking, object recognition, line tracking, color recognition, and tag (QR code) recognition. It can be easily interfaced with microcontrollers through the use of UART or I2C port. Moreover, HuskyLens has a built-in LCD screen allowing for a 2.0 inches display and it also has buttons and knob. With this, algorithms can be changed easily without the need of a microcontroller board.

 

  • Relay 1-channel module

An electrical switch that is activated by an electromagnet is a relay module. A different low-power signal, in this example from the Arduino Nano microcontroller, activates the electromagnet. The electromagnet pulls when it is turned on, allowing a circuit to be opened or closed.

 

  • Solenoid Electromagnetic Lock - LP Security

Solenoid Electric Control Lock 12V - leetechbd

This type of electromagnetic lock has a varied working mechanism compared to usual electromagnetic locks. Most electromagnetic locks need electricity for it to function or to hold the lock in place. However, the type used in this project only requires electricity when it is ought to be unlocked. Setting the lock doesn't require electricity as merely pushing the metal inclusion through the lock feed binds it manually. The working requirement of this lock is a 12V, 2A power supply. It can also be manually unlocked by pressing the solenoid actuator down. This is handy in cases of power outages. 

 

Hardware set-up

Pin connection: HuskyLens 

In order to interface the HuskyLens to an Arduino Nano through I2C:

HuskyLens Pin 1 (Blue)  =  Nano Serial Clock line (A4)

HuskyLens Pin 2 (Green) = Nano Serial Data line (A5)

HuskyLens Pin 3 (Black) = Nano Ground

HuskyLens Pin 4 (Red) = Nano 5V or VCC

 

Pin connection: Single Channel 5V relay module

To keep a safe connection in the relay, the 12V power supply ground was connected directly to the Solenoid EM lock. The Hot wire (red wire from the 12V power supply) is connected to the normally open node of the relay and the VCC of the solenoid is connected to the common node of the relay. The relay input pin is connected to the Arduino D5 pin. 

 

Software set-up

HuskyLens Library

The HuskyLens library should be installed on your host computer before working with it in Arduino IDE. To download the HuskyLens library, please proceed to this link: GitHub - HuskyLens/HUSKYLENSArduino. The only requirement is to download the compressed folder, extract the folder, and paste the HuskyLens library from your download folder to the Arduino library folder of the desired device. You should be able to see the HuskyLens library, along with the examples, in the Arduino IDE after following the steps indicated above.

 

Code

#include "HUSKYLENS.h"
#include "Wire.h"

#define RELAY_PIN 5
#define KNOWN_FACE_ID 1

HUSKYLENS huskylens;
//HUSKYLENS green line >> SDA; blue line >> SCL
void printResult(HUSKYLENSResult result);

void setup() {
  Serial.begin(115200);
  Wire.begin();
  while (!huskylens.begin(Wire)) {
    Serial.println(F("Begin failed!"));
    Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>I2C)"));
    Serial.println(F("2.Please recheck the connection."));
    delay(100);
  }

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  // Initialize the relay to be off
}

void loop() {
  if (!huskylens.request()) Serial.println(F("Fail to request data from HUSKYLENS, recheck the connection!"));
  else if (!huskylens.isLearned()) Serial.println(F("Nothing learned, press learn button on HUSKYLENS to learn one!"));
  else if (!huskylens.available()) Serial.println(F("No block or arrow appears on the screen!"));
  else {
    Serial.println(F("###########"));
    while (huskylens.available()) {
      HUSKYLENSResult result = huskylens.read();
      printResult(result);
      
      // Check if the detected face ID matches the known face ID
      if (result.command == COMMAND_RETURN_BLOCK && result.ID == KNOWN_FACE_ID) {
        digitalWrite(RELAY_PIN, HIGH);  // Turn on the relay for known faces
        delay (5000);
        digitalWrite(RELAY_PIN, LOW); // Turn off the relay after certain delay
      } else {
        digitalWrite(RELAY_PIN, LOW);   // Turn off the relay for unknown faces
      }
    }
  }
}

void printResult(HUSKYLENSResult result) {
  if (result.command == COMMAND_RETURN_BLOCK) {
    Serial.println(String() + F("Block:xCenter=") + result.xCenter + F(",yCenter=") + result.yCenter + F(",width=") + result.width + F(",height=") + result.height + F(",ID=") + result.ID);
  } else if (result.command == COMMAND_RETURN_ARROW) {
    Serial.println(String() + F("Arrow:xOrigin=") + result.xOrigin + F(",yOrigin=") + result.yOrigin + F(",xTarget=") + result.xTarget + F(",yTarget=") + result.yTarget + F(",ID=") + result.ID);
  } else {
    Serial.println("Object unknown!");
  }
}

Code breakdown

The final code in this project is a slightly modified version of an existing example code in the HuskyLens library. Pre-established function in the introduction example for the HuskyLens are kept since it does not impact the functionality of the face recognition door lock system. On the contrary, keeping the original code and modifying it only by adding new lines can help reduce confusion.

Code block before setup()

  • Required variables such as the relay pin and face ID are defined to be used in the succeeding lines of codes. It is important to specify the known face ID in order for the system to work. If this variable is not set, the system might be unlocked as it detects a face even if the face was not recognized.
  • The line "void printResult(HUSKYLENSResult result);" is included in the example. This prints out the position of the detected object, it's coordinates, and if it's recognized or not. 

setup()

  • In this part, the HuskyLens was initialized. Precautions are also written here in case the HuskyLens is not detected.

loop()

  • In this section, the detected face is analyzed if it matches the face that was identified to be FaceID 1. If the detected face has an ID value 0, the system will not be unlocked. As the 0 value signifies that the detected face was not recognized. If the ID value is greater than 1, the lock will still not be unlocked since this is the ID value set in the setup. 
  • If the ID value is equal to 1, the relay will be turned on. Resulting to the system to be on unlocked state. As mentioned beforehand, the Solenoid EM lock doesn't need electricity to hold the lock in place. With this, a delay was indicated in the code to reset the lock. 

 

Demonstration

 

Conclusion

This tutorial demonstrated a face recognition door lock system using HuskyLens and Arduino that can be easily deployed to homes. When the HuskyLens detects a face, the face ID will be shown in the Arduino's serial monitor. If the face ID value does not match the value defined in the code, it leaves the system locked. Once the face ID value matches face ID value defined in the code, the solenoid EM lock will have electricity resulting in unlocking the system. A delay was set in order for the relay to turn on once again, cutting the electricity across the solenoid EM lock. This resets the system, locking the solenoid pin once again.  

 

References

Gravity: HUSKYLENS AI Machine Vision Sensor - DFRobot Wiki

Arduino Nano V3 Compatible ATmega328 mini USB, CH340G – CreateLabz Store

Face recognitionFace recognition security systemHome securityHuskylensSecurity system

Leave a comment

All comments are moderated before being published