Smart Biometric Door Lock using NodeMCU and Fingerprint Sensor

 Intro: This circuit is an IoT-based Smart Biometric Door Lock that uses a NodeMCU (ESP8266) microcontroller and a fingerprint sensor (specifically the GT511C3 module) to provide secure keyless access control.

Here is a quick breakdown of how it works:

  • Biometric Authentication: The fingerprint sensor scans and identifies the user's fingerprint.

  • Access Control: If a match is found (e.g., predefined IDs like Ashish, Manoj, or Aswinth), the NodeMCU triggers a relay module to unlock a electronic door lock (DC motor/solenoid) and sounds a buzzer for confirmation.

  • IoT Logging & Alerts:

    • Successful Entries: It uses Wi-Fi to connect to the Adafruit MQTT broker, where it publishes a real-time message stating who entered.

    • Unrecognized Attempts: If an unauthorized finger tries to gain access, the circuit activates a persistent warning buzzer and triggers an external webhook via IFTTT (maker.ifttt.com) to send an alert or log the security event online.

It acts as a modern, cloud-connected security solution ideal for homes, private offices, or restricted labs.

Components: To build this Smart Biometric Door Lock, you will need the following hardware components:

Core Microcontroller & Sensors

  • NodeMCU (ESP8266): The brain of the circuit. It processes the biometric data and uses its built-in Wi-Fi module to communicate with the cloud (Adafruit MQTT and IFTTT).

  • Fingerprint Sensor Module: Typically a module like the GT511C3, used to scan, capture, and identify authorized fingerprints.

Actuators & Indicators

  • Relay Module: Acts as an electronic switch. The NodeMCU cannot directly power heavy loads, so the relay safely switches the main power to the door locking mechanism.

  • DC Motor / Solenoid Lock: The actual physical locking mechanism that moves to lock or unlock the door when triggered by the relay.

  • Buzzer (5mm): Provides audible feedback (e.g., a short beep for access granted or a continuous pulsing alert for an unauthorized attempt).

Miscellaneous

  • Jumper Wires: For making connections between the modules and the NodeMCU.

  • Power Supply: A suitable power source to power both the NodeMCU (typically 5V via Micro-USB) and the electronic lock/motor (depending on its voltage rating, usually 9V or 12V).


Arduino Code: Here is the complete, production-ready Arduino code for the Smart Biometric Door Lock. This code is configured for the NodeMCU (ESP8266) using the GT511C3 fingerprint sensor, Adafruit IO (MQTT), and IFTTT webhooks.

Prerequisites & Setup

Before uploading, make sure you install the following libraries in your Arduino IDE:

  1. FPS_GT511C3

  2. Adafruit MQTT Library

C++
#include "FPS_GT511C3.h"
#include "SoftwareSerial.h"
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// --- WiFi Configuration ---
const char *ssid = "Your_WiFi_Name";     // Enter your WiFi Name here
const char *pass = "Your_WiFi_Password"; // Enter your WiFi Password here

// --- Adafruit MQTT Configuration ---
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "Your_Adafruit_Username" // Enter your Adafruit IO username
#define MQTT_PASS "Your_Adafruit_AIO_Key"   // Enter your Adafruit IO AIO Key

// --- IFTTT Webhook Configuration ---
const char *host = "maker.ifttt.com";
const char *privateKey = "Your_IFTTT_Private_Key"; // Enter your IFTTT Webhook key

// --- Pin Definitions ---
#define relay D1  // Relay control module pin
#define buzzer D2 // Alert buzzer pin

// Define software serial pins for the FPS (D6 = NodeMCU RX connected to FPS TX, D5 = NodeMCU TX connected to FPS RX)
FPS_GT511C3 fps(D6, D5);

// Global variables for cloud messages
String msg;
char msg1[20];

// Initialize WiFi and MQTT clients
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, ...); // Handled internally
Adafruit_MQTT_Publish Fingerprint = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/Fingerprint");

// Forward declaration
void send_event(const char *event);
void MQTT_connect();
void Buzzer();

void setup() {
  Serial.begin(9600); // Initialize hardware serial debugging
  delay(100);
  
  fps.Open();      // Send serial command to initialize fingerprint scanner
  fps.SetLED(true); // Turn on optical sensor LED
  
  pinMode(relay, OUTPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(relay, LOW); // Keep the solenoid locked initially

  // Connect to local WiFi network
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("."); // Print progress bar until connected
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  // Ensure we maintain a secure connection to Adafruit IO
  MQTT_connect();

  // Check if a finger is touching the scanner plate
  if (fps.IsPressFinger()) {
    fps.CaptureFinger(false); // Capture high-quality fingerprint image
    int id = fps.Identify1_N(); // Compare print against stored database

    // --- CASE 1: Ashish Authorized (ID 7) ---
    if (id == 7) {
      Serial.print("Ashish Entered");
      Buzzer();                      // Play brief confirmation beep
      digitalWrite(relay, HIGH);     // Unlock door mechanism
      delay(6000);                   // Keep door unlocked for 6 seconds
      digitalWrite(relay, LOW);      // Securely relock door
      
      msg = "Ashish Entered";
      msg.toCharArray(msg1, 20);
      Fingerprint.publish(msg1);     // Post entry log to Adafruit cloud
    }
    // --- CASE 2: Manoj Authorized (ID 8) ---
    else if (id == 8) {
      Serial.print("Manoj Entered");
      Buzzer();
      digitalWrite(relay, HIGH);
      delay(6000);
      digitalWrite(relay, LOW);
      
      msg = "Manoj Entered";
      msg.toCharArray(msg1, 20);
      Fingerprint.publish(msg1);
    }
    // --- CASE 3: Aswinth Authorized (ID 9) ---
    else if (id == 9) {
      Serial.print("Aswinth Entered");
      Buzzer();
      digitalWrite(relay, HIGH);
      delay(6000);
      digitalWrite(relay, LOW);
      
      msg = "Aswinth Entered";
      msg.toCharArray(msg1, 20);
      Fingerprint.publish(msg1);
    }
    // --- CASE 4: Intruder / Unauthorized Print ---
    else {
      Serial.println("Finger not found");
      send_event("login_event"); // Instantly fire IFTTT security webhook alert
      
      // Pulse the hardware buzzer 7 times rapidly as a warning indicator
      for (int i = 7; i > 0; i--) {
        digitalWrite(buzzer, HIGH);
        delay(100);
        digitalWrite(buzzer, LOW);
        delay(100);
      }
    }
  } 
  else {
    Serial.println("Please press finger");
  }
  delay(100); // Small standard polling delay
}

// --- Trigger HTTP Webhook to IFTTT ---
void send_event(const char *event) {
  Serial.print("Connecting to ");
  Serial.println(host);

  WiFiClient secureClient;
  const int httpPort = 80;
  if (!secureClient.connect(host, httpPort)) {
    Serial.println("Connection failed");
    return;
  }

  // Structure the specific endpoint URL query string
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += privateKey;

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // Construct standard HTTP raw GET request header string
  secureClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
                     "Host: " + host + "\r\n" +
                     "Connection: close\r\n\r\n");
                     
  while (secureClient.connected()) {
    if (secureClient.available()) {
      String line = secureClient.readStringUntil('\r');
      Serial.print(line);
    } else {
      delay(50);
    }
  }
  Serial.println("\nClosing connection");
  secureClient.stop();
}

// --- Robust Self-Healing MQTT Broker Handler ---
void MQTT_connect() {
  int8_t ret;
  if (mqtt.connected()) return; // Exit quickly if online

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // Returns 0 if communication succeeds
    mqtt.disconnect();
    delay(5000); // Wait 5 seconds before attempting reconfiguration loop
    retries--;
    if (retries == 0) {
      while (1); // Trap process until hardware watchdog resets the NodeMCU
    }
  }
}

// --- Clean 1-Second Access Notification ---
void Buzzer() {
  digitalWrite(buzzer, HIGH);
  delay(1000);
  digitalWrite(buzzer, LOW);
}

Configuration Instructions:

  • Replace "Your_WiFi_Name" and "Your_WiFi_Password" with your local credentials.

  • Ensure your usernames, keys, and specific user IDs correspond cleanly to what you set up inside your Adafruit IO and IFTTT profile interfaces.

How It Works: The system functions through a continuous loop of fingerprint detection, biometric validation, and automated physical/cloud responses:

  • Initialization: When booted up, the NodeMCU initializes communication with the fingerprint sensor and activates its optical scanning LED. It then establishes a persistent connection to the local Wi-Fi network and checks for a valid session with the Adafruit MQTT broker.

  • Fingerprint Detection & Matching: The microcontroller continuously polls the fingerprint module. When a finger is physically pressed against the scanner plate, the module captures a high-resolution biometric image and runs the Identify1_N method to compare it against all locally registered fingerprint templates stored in its database.

  • Access Granted: If the fingerprint matches an authorized ID (such as the predefined profiles for Ashish, Manoj, or Aswinth):

    • The system sounds a brief 1-second confirmation beep via the buzzer.

    • It pulls the relay pin HIGH, sending power to open the solenoid door lock.

    • The door remains unlocked for 6 seconds to allow entry before pulling the relay LOW to re-secure the latch.

    • A real-time status update confirming who entered is formatted and published straight to the Adafruit MQTT cloud feed.

  • Access Denied: If an unrecognized fingerprint is scanned:

    • The code triggers a rapid loop that pulses the buzzer 7 times as an audible warning indicator.

    • It invokes the send_event function, establishing a direct TCP connection to the IFTTT webhook service (maker.ifttt.com) via port 80. It then dispatches an HTTP GET request containing a private API key to instantly fire external notifications or log the security alert online.

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Here is the complete, compiled Arduino IDE code for this Smart Biometric Door Lock.

Complete Code Listing

C++
#include "FPS_GT511C3.h"
#include "SoftwareSerial.h"
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// --- WiFi Configuration ---
const char *ssid = "Galaxy-M20"; // Enter your WiFi Name
const char *pass = "ac312124";   // Enter your WiFi Password

// --- Adafruit MQTT Configuration ---
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "choudharyas"                      // Enter your Adafruit IO username
#define MQTT_PASS "988c4e045ef64c1b9bc8b5bb7ef5f2d9" // Enter your Adafruit IO AIO Key

// --- IFTTT Webhook Configuration ---
const char *host = "maker.ifttt.com";
const char *privateKey = "hUAAAz0AVvc6-NW1UmqWXXv6VQWmpiGFxx3sV5rnaM9"; // Enter your IFTTT Webhook key

// --- Pin Definitions ---
#define relay D1  // Relay control module pin
#define buzzer D2 // Alert buzzer pin

String msg;
char msg1[20];

// Initialize WiFi and MQTT clients
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS); //
Adafruit_MQTT_Publish Fingerprint = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/Fingerprint"); //

// Define software serial pins for the FPS (D6 = NodeMCU RX, D5 = NodeMCU TX)
FPS_GT511C3 fps(D6, D5);

// Forward declarations
void send_event(const char *event);
void MQTT_connect();
void Buzzer();

void setup() {
  Serial.begin(9600); // Set up hardware serial UART
  delay(100);
  
  fps.Open();      // Send serial command to initialize fingerprint scanner
  fps.SetLED(true); // Turn on LED so fingerprint scanner can see the print
  
  pinMode(relay, OUTPUT); //
  pinMode(buzzer, OUTPUT); //
  digitalWrite(relay, LOW); // Keep motor/solenoid off initially

  // Connect to local WiFi network
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass); //
  
  while (WiFi.status() != WL_CONNECTED) { //
    delay(500);
    Serial.print("."); // Print progress trailing dots
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  // Ensure connection to the Adafruit MQTT broker is open
  MQTT_connect();

  // Check if a finger is touching the scanner plate
  if (fps.IsPressFinger()) { //
    fps.CaptureFinger(false); // Capture high-quality fingerprint image
    int id = fps.Identify1_N(); // Search database for matching template ID

    // --- CASE 1: Ashish Authorized (ID 7) ---
    if (id == 7) { //
      Serial.print("Ashish Entered"); //
      Buzzer();                      // Play brief confirmation beep
      digitalWrite(relay, HIGH);     // Unlock door mechanism
      delay(6000);                   // Keep door unlocked for 6 seconds
      digitalWrite(relay, LOW);      // Securely relock door
      
      msg = "Ashish Entered"; //
      msg.toCharArray(msg1, 20); //
      Fingerprint.publish(msg1); // Post entry log to Adafruit cloud
    }
    // --- CASE 2: Manoj Authorized (ID 8) ---
    else if (id == 8) { //
      Serial.print("Manoj Entered"); //
      Buzzer();
      digitalWrite(relay, HIGH);
      delay(6000);
      digitalWrite(relay, LOW);
      
      msg = "Manoj Entered"; //
      msg.toCharArray(msg1, 20);
      Fingerprint.publish(msg1);
    }
    // --- CASE 3: Aswinth Authorized (ID 9) ---
    else if (id == 9) { //
      Serial.print("Aswinth Entered"); //
      Buzzer();
      digitalWrite(relay, HIGH);
      delay(6000);
      digitalWrite(relay, LOW);
      
      msg = "Aswinth Entered"; //
      msg.toCharArray(msg1, 20);
      Fingerprint.publish(msg1);
    }
    // --- CASE 4: Intruder / Unauthorized Print ---
    else {
      send_event("login_event"); // Instantly fire IFTTT security webhook alert
      Serial.println("Finger not found"); //
      
      // Pulse the hardware buzzer 7 times rapidly as a warning indicator
      for (int i = 7; i > 0; i--) { //
        digitalWrite(buzzer, HIGH); //
        delay(100); //
        digitalWrite(buzzer, LOW); //
        delay(100); //
      }
    }
  } 
  else {
    Serial.println("Please press finger"); //
  }
  delay(100); // Standard polling interval
}

// --- Trigger HTTP Webhook to IFTTT ---
void send_event(const char *event) { //
  Serial.print("Connecting to ");
  Serial.println(host); //

  WiFiClient secureClient; // Create direct TCP connection client
  const int httpPort = 80; //
  if (!secureClient.connect(host, httpPort)) { //
    Serial.println("Connection failed"); //
    return;
  }

  // Structure the specific endpoint URL query string
  String url = "/trigger/"; //
  url += event; //
  url += "/with/key/"; //
  url += privateKey; //

  Serial.print("Requesting URL: ");
  Serial.println(url); //

  // Construct and send standard HTTP raw GET request header string
  secureClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
                     "Host: " + host + "\r\n" +
                     "Connection: close\r\n\r\n"); //
                     
  while (secureClient.connected()) { //
    if (secureClient.available()) { //
      String line = secureClient.readStringUntil('\r'); //
      Serial.print(line); //
    } else {
      delay(50); //
    }
  }
  Serial.println();
  Serial.println("closing connection"); //
  secureClient.stop(); //
}

// --- Self-Healing MQTT Broker Handler ---
void MQTT_connect() { //
  int8_t ret;
  if (mqtt.connected()) return; // Exit quickly if online

  uint8_t retries = 3; //
  while ((ret = mqtt.connect()) != 0) { // Connect will return 0 for successful handshake
    mqtt.disconnect(); //
    delay(5000);       // Wait 5 seconds before retrying
    retries--;
    if (retries == 0) {
      while (1);       // Standby and let Watchdog Timer (WDT) reset the NodeMCU
    }
  }
}

// --- Clean 1-Second Access Notification ---
void Buzzer() { //
  digitalWrite(buzzer, HIGH); //
  delay(1000);                // Sound for 1 second
  digitalWrite(buzzer, LOW);  //
}

Post a Comment

Previous Post Next Post