Arduino code for fingerprint and buzzer alarm

anyone can help me with arduino code in intefaring the fingerprint and buzzer alarm when unidentified person scan it buzzer and when its identified no buzzer will alarm

#include <Adafruit_Fingerprint.h>
//For Arduino UNO use pin #D2 from sensor (GREEN wire)
// pin #D3 is OUT from arduino (WHITE wire)
//For Arduino MEGA use pin #D10 from sensor (GREEN wire)
// pin #D11 is OUT from arduino (WHITE wire)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//BUZZER pin
const int buzzer = 8;

void setup()
{
Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("\n\n Finger detect test");

// set the data rate for the sensor serial port
finger.begin(57600);

if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
//BUZZER
pinMode(buzzer, OUTPUT);
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
}

void loop() // run over and over again
{
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
//BUZZER
digitalWrite(buzzer, HIGH); //UNINDENTIFIED = LOCK
delay(3000);
digitalWrite(buzzer, LOW);
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}

// OK success!

p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}

// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}

// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);

return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;

p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;

p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;

// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}

If you want an answer here, you first have to specify what hardware you are using and how you envision it working. Have you already worked on a code? It's best if you describe the project in more detail, then I can help you better!

1 Like

hello sir good day i edit my post of the code it is okay or something wrong for that ?

1 Like
type or paste code here
#include <Adafruit_Fingerprint.h>
//For Arduino UNO use pin #D2 from sensor (GREEN wire)
// pin #D3 is OUT from arduino  (WHITE wire)
//For Arduino MEGA use pin #D10 from sensor (GREEN wire)
// pin #D11 is OUT from arduino  (WHITE wire)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//BUZZER pin
const int buzzer = 8;

void setup()  
{
  Serial.begin(9600);
  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);
  Serial.println("\n\n Finger detect test");

  // set the data rate for the sensor serial port
  finger.begin(57600);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }
  //BUZZER   
  pinMode(buzzer, OUTPUT);
  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

void loop()                     // run over and over again
{
  getFingerprintIDez();
  delay(50);            //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
    //BUZZER
     digitalWrite(buzzer, HIGH);    //UNINDENTIFIED = LOCK
     delay(3000);
     digitalWrite(buzzer, LOW);           
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 

  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}

I checked the code and found some possible errors. Here you are:

  • In the Getfingerprintid () function, a break statement is missing according to the Fingerprint_nofinger case. This could cause the code to pass into the next case and provide unexpected results.
  • The variable P is not initialized in the fingerprintez () function before it is used in the IF condition. This could lead to an undefined behavior.
  • The variable finger.fingerid is not returned in the fingerprintez () function if a fingerprint is found. This could cause the function to always return -1.

The improved code will follow soon.

I hope that will help you. If you have any further questions, let me know.

The easiest way is to simply try the code.

1 Like

I'm not sure if this code works. If not, unfortunately I can't help you.

#include <Adafruit_Fingerprint.h>

//For Arduino UNO use pin #D2 from sensor (GREEN wire)
// pin #D3 is OUT from arduino (WHITE wire)
//For Arduino MEGA use pin #D10 from sensor (GREEN wire)
// pin #D11 is OUT from arduino (WHITE wire)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

//BUZZER pin
const int buzzer = 8;

void setup() {
  Serial.begin(9600);
  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);
  
  Serial.println("\n\n Finger detect test");

  // set the data rate for the sensor serial port
  finger.begin(57600);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) {
      delay(1);
    }
  }
  
  //BUZZER
  pinMode(buzzer, OUTPUT);

  finger.getTemplateCount();
  Serial.print("Sensor contains ");
  Serial.print(finger.templateCount);
  Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

void loop()  // run over and over again
{
  getFingerprintIDez();
  delay(50);            //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      digitalWrite(buzzer, HIGH); //UNINDENTIFIED = LOCK
      delay(3000);
      digitalWrite(buzzer, LOW);
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
  }

  if (p != FINGERPRINT_OK) {
    return p;
  }
  
  p = finger.image2Tz();
  
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      break;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      break;
    default:
      Serial.println("Unknown error");
      break;
  }

  if (p != FINGERPRINT_OK) {
    return p;
  }

  p = finger.fingerFastSearch();
  
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }

  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  // Unlock the door
  digitalWrite(buzzer, HIGH);
  delay(2000);
  digitalWrite(buzzer, LOW);

  // Return the finger ID
  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;

  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  // Unlock the door
  digitalWrite(buzzer, HIGH);
  delay(2000);
  digitalWrite(buzzer, LOW);

  // Return the finger ID
  return finger.fingerID;
}

1 Like

Thank you for the likes!

1 Like

thanks a lot sir for your time and answer it may not be work but i appreciate your good will

I tried the code that u give me its working but not what i mean the code you give me was when i open or unlock the door it will buzzer or alarm. what i mean is when i open the door but the finger is an authorize or unidentified finger the buzzer alarm will make a sound if the finger is not detected.

can u help me with this ? please.

To modify the code to add a buzzer alarm sound when an unauthorized finger is detected, you can add an additional case in the getFingerprintID() function for FINGERPRINT_NOTFOUND, which will trigger the buzzer to sound:

#include <Adafruit_Fingerprint.h>
//For Arduino UNO use pin #D2 from sensor (GREEN wire)
// pin #D3 is OUT from arduino (WHITE wire)
//For Arduino MEGA use pin #D10 from sensor (GREEN wire)
// pin #D11 is OUT from arduino (WHITE wire)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

//BUZZER pin
const int buzzer = 8;

void setup() {
  Serial.begin(9600);
  while (!Serial); // For Yun/Leo/Micro/Zero/...
  delay(100);
  Serial.println("\n\n Finger detect test");

  // set the data rate for the sensor serial port
  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }

  //BUZZER
  pinMode(buzzer, OUTPUT);

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  Serial.println("Waiting for valid finger...");
}

void loop() // run over and over again
{
  getFingerprintIDez();
  delay(50); //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      digitalWrite(buzzer, HIGH); //UNINDENTIFIED = LOCK
      delay(3000);
      digitalWrite(buzzer, LOW);
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
  }

  if (p != FINGERPRINT_OK) {
    return p;
  }

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      break;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      break;
    default:
      Serial.println("Unknown error");
      break;
  }

  if (p != FINGERPRINT_OK) {
    return p;
  }

  p = finger.fingerFastSearch();
  
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
    // Unlock the door
    digitalWrite(buzzer, LOW); // turn off buzzer
  } else {
    Serial.println("Did not find a match");
    // Sound the alarm
    digitalWrite(buzzer, HIGH); // turn on buzzer
  }
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  
  // Return the finger ID
  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK) return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK) return -1;

  p = finger.fingerFastSearch();
  
   if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
    // Unlock the door
    digitalWrite(buzzer, LOW); // turn off buzzer
   } else {
     Serial.println("Did not find a match");
     // Sound the alarm
     digitalWrite(buzzer, HIGH); // turn on buzzer
   }
  
   // found a match!
   Serial.print("Found ID #"); Serial.print(finger.fingerID); 
   Serial.print(" with confidence of "); Serial.println(finger.confidence);

  
   // Return the finger ID
   return finger.fingerID; 
}

I'm not sure if this code will work as I don't have the contents of the "SoftwareSerial.h". But you don't have to post them either. Also, unfortunately I don't know how long the buzzer is then. But you can usually set that higher with the "Delay", which is set to 50. I hope it works now

1 Like

thanks a lot sir i appreciate your kindness on me i hope you will help in other time again thank you so much

Always my pleasure! That's what the forum is for. If you have any further questions, you just have to get in touch!

1 Like

Sir just a little bit ,messy but its almost done can you help about it it's okay if unauthorized fingerprint it will on the buzzer but no delay the buzzer keep going on alarming but when i put an authorized fingerprint it will off i put delay(1000) on digitalWrite(buzzer, HIGH) to turn off automatically but its keep going on how should i fix it ?

i fix it already sir thank you so much again sir for helping me in anyway i'll approach again to you sir if i have any concern thank u so much again

1 Like

hello sir cloud can you help me with this code ? i want the relay to open using my fingerprint sensor and automatically close within 10 seconds and also the buzzer again i have new code for it because there's a lot of device that i put on it what should i do on it i want again the buzzer will alarm if theres unidentify person scanning on fingerprint sensor i want to solve this but i cant do it anymore i need your help sir

heres the code sir

#include <SoftwareSerial.h>

#include <Wire.h>

#include <SD.h>

#include <LiquidCrystal_I2C.h>

#include <DS3231.h>

#include <Adafruit_Fingerprint.h>

/*  Webpage: https://www.electronoobs.com/eng_arduino_tut41.php
 *  Schematic: https://www.electronoobs.com/eng_arduino_tut40_sch1.php
 *  Code: https://www.electronoobs.com/eng_arduino_tut40_code3.php
 *  Youtube Channel: https://www.youtube.com/c/Electronoobs */

////////////////////////////////////INCLUDE ALL LIBRARIES/////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.
//Fingerprint libraries
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
int getFingerprintIDez();
SoftwareSerial mySerial(10, 11);  // pin #2 is IN from sensor (GREEN wire)/ pin #3 is OUT from arduino (WHITE wire)
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//Real time clock libraries
#include <DS3231.h>
DS3231  rtc(SDA, SCL);// Init the DS3231 using the hardware interface
//SD card read/write libraries
#include <SPI.h>
#include <SD.h>
File myFile;
//servo library
#include <Servo.h>
Servo miServo;


/////////////////////////////////////Input and outputs////////////////////////////////////////////////
int scan_pin = 13;      //Pin for the scan push button
int add_id_pin = 12;    //Pin for the add new ID push button  
int open_door = 9;     //Pin to open the door button
int RELAY_PIN = 6;        //Pin for the Servo PWM signal
const int buzzer = 8;  // Sound the alarm

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////Editable variables//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main_user_ID = 1;                 //Change this value if you want a different main user
int door_open_delay =  0.2;
int door_close_delay = 10000;
String file_name_to_save = "blackBox.txt";
//////////////////////////////////////////////////////////////////////////////////////////////////////

//Other used variables in the code. Do not change!
bool scanning = false;
int counter = 0;
int id_ad_counter = 0;
bool id_ad = false;
uint8_t num = 1;
bool id_selected = false;
uint8_t id;
bool first_read = false;
bool main_user = false;
bool add_new_id = false;
bool door_locked = true;



void setup() {
  Serial.begin(57600);        //Start serial communication for fingerprint TX RX data.
  rtc.begin();                //Start the real time clock (remember to set time before this code)
  SD.begin(53);               //Start SD card module with CS pin connected to D53 of the Arduino MEGA. The other pins are SPI pins

  /*Now we open the new created file, write the data, and close it back*/
  myFile = SD.open(file_name_to_save, FILE_WRITE);   //Create a new file on the SD card named before
  myFile.print("Door lock system started at ");
  myFile.print(rtc.getTimeStr()); myFile.print(" and day ");  myFile.print(rtc.getDateStr());
  myFile.println(" ");myFile.println(" ");
  myFile.close(); 
   
  lcd.init();                     //Init the LCD with i2c communication and print text
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("   Press SCAN   ");
  lcd.setCursor(0,1);
  lcd.print(" -door  locked- ");

  //Define pins as outputs or inputs
  //BUZZER   
  pinMode(buzzer, OUTPUT);
  pinMode(scan_pin,INPUT);  
  pinMode(add_id_pin,INPUT);  
  pinMode(open_door,INPUT);  
  pinMode(RELAY_PIN, OUTPUT);
  miServo.write(door_close_delay);  //Door close
  digitalWrite(RELAY_PIN,HIGH);
  delay(10000);                   // Door open         
  digitalWrite(RELAY_PIN,LOW);    // Door close     
  finger.begin(57600);                // set the data rate for the sensor serial port
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////VOID LOOP////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


void loop() {
/////////////////////////////////////////DOOR OPEN BUTTON PRESSED///////////////////////////////////
  if(digitalRead(open_door))
  {
    door_locked = true;
    miServo.write(door_close_delay);  //Door close
    digitalWrite(RELAY_PIN,HIGH);       
    digitalWrite(RELAY_PIN,LOW);       
    lcd.setCursor(0,0);
    lcd.print("  Door closed!  ");
    lcd.setCursor(0,1);
    lcd.print("                "); 
    delay(2000);     
    lcd.setCursor(0,0);
    lcd.print("   Press SCAN   ");
    lcd.setCursor(0,1);
    lcd.print(" -door  locked- ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Door was closed by a user");
    myFile.close(); 
  }


////////////////////////////////Scan button pressed/////////////////////////////////////////////////
 if(digitalRead(scan_pin) && !id_ad)
 {
  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.println(" -- Attempt of openning door");
  myFile.close(); 
  scanning = true;
  lcd.setCursor(0,0);
  lcd.print("  Place finger  ");
  lcd.setCursor(0,1);
  lcd.print("SCANNING--------");
 }
  
 while(scanning && counter <= 60)
 {
  getFingerprintID();
  delay(100); 
  counter = counter + 1;
  if(counter == 10)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING  ------");
  }

  if(counter == 20)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING    ----");
  }

  if(counter == 40)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING      --");
  }

  if(counter == 50)
  {
    lcd.setCursor(0,0);
    lcd.print("  Place finger  ");
    lcd.setCursor(0,1);
    lcd.print("SCANNING        ");
  }
  if(counter == 59)
  {
    lcd.setCursor(0,0);
    lcd.print("    Timeout!    ");
    lcd.setCursor(0,1);
    lcd.print("   Try again!   ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Scanning timeout!");
    myFile.close(); 
    delay(2000);
    if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
   }
   
 }
 scanning = false;
 counter = 0;
///////////////////////////////END WITH SCANNING PART



//////////////////////////////////////////Add new button pressed///////////////////////////////////////////
if(digitalRead(add_id_pin) && !id_ad)
 {
  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.println(" -- Add new user attempt. Require identification!");
  myFile.close(); 

  add_new_id = true;

  lcd.setCursor(0,0);
  lcd.print(" Scan main user ");
  lcd.setCursor(0,1);
  lcd.print("  finger first! ");  
  
  while (id_ad_counter < 40 && !main_user)
  {
    getFingerprintID();
    delay(100);  
    id_ad_counter = id_ad_counter+1;
    if(!add_new_id)
    {
      id_ad_counter = 40;
    }
  }
  id_ad_counter = 0;
  add_new_id = false;

  
  if(main_user)
  {
    lcd.setCursor(0,0);
    lcd.print(" Add new ID# to ");
    lcd.setCursor(0,1);
    lcd.print("  the database  ");  
    delay(1500);
    print_num(num);  
    id_ad = true;
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- Add new user permission granted");
    myFile.close(); 
  }
  else
  {
    lcd.setCursor(0,0);
    lcd.print("ERROR! Only main");
    lcd.setCursor(0,1);
    lcd.print("user can add IDs");  
    delay(1500); 
    if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
    id_ad = false;
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.println(" -- The user does not have permission to add new user");
    myFile.close(); 
  }
 }

if(digitalRead(scan_pin) && id_ad)
 {  
  
  id=num;
  while (!  getFingerprintEnroll() );
  id_ad = false;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  New ID saved  ");
  lcd.setCursor(4,1);
  lcd.print("as ID #"); 
  lcd.setCursor(11,1);
  lcd.print(id);  
  delay(3000);
  if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
  add_new_id = false;
  main_user = false;
  id_ad = false;
 
  
 }

if(digitalRead(add_id_pin) && id_ad)
 {
  num = num + 1;
  if(num > 16)
  {
    num=1;
  }
  print_num(num);  
 }







}//end of void


//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////


/*This function will print the ID numbers when adding new ID*/
void print_num(uint8_t)
{
  if (num == 1)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">1  2   3   4   ");  
    delay(500);
  }
  if (num == 2)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1 >2   3   4   ");  
    delay(500);
  }
  if (num == 3)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1  2  >3   4   ");  
    delay(500);
  }
  if (num == 4)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 1  2   3  >4   ");  
    delay(500);
  }
  if (num == 5)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">5  6   7   8   ");  
    delay(500);
  }
  if (num == 6)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5 >6   7   8   ");  
    delay(500);
  }
  if (num == 7)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5  6  >7   8   ");   
    delay(500);
  }
  if (num == 8)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 5  6   7  >8   ");   
    delay(500);
  }
  if (num == 9)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">9  10  11  12  ");  
    delay(500);
  }
  if (num == 10)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9 >10  11  12  ");  
    delay(500);
  }
  if (num == 11)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9  10 >11  12  ");  
    delay(500);
  }
  if (num == 12)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 9  10  11 >12  ");  
    delay(500);
  }
  if (num == 13)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(">13  14  15  16 ");  
    delay(500);
  }
  if (num == 14)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13 >14  15  16 ");  
    delay(500);
  }
  if (num == 15)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13  14 >15  16 ");  
    delay(500);
  }
  if (num == 16)
  {
    lcd.setCursor(0,0);
    lcd.print("Select ID number");
    lcd.setCursor(0,1);
    lcd.print(" 13  14  15 >16 ");  
    delay(500);
  }
}









/*This function will read the fingerprint placed on the sensor*/
uint8_t getFingerprintID()
{
  uint8_t p = finger.getImage();
  switch (p)
  {
    case FINGERPRINT_OK:
    break;
    case FINGERPRINT_NOFINGER: return p;
    case FINGERPRINT_PACKETRECIEVEERR: return p;
    case FINGERPRINT_IMAGEFAIL: return p;
    default: return p; 
  }
// OK success!

  p = finger.image2Tz();
  switch (p) 
  {
    case FINGERPRINT_OK: break;    
    case FINGERPRINT_IMAGEMESS: return p;    
    case FINGERPRINT_PACKETRECIEVEERR: return p;  
    case FINGERPRINT_FEATUREFAIL: return p;  
    case FINGERPRINT_INVALIDIMAGE: return p;    
    default: return p;
  }
// OK converted!

p = finger.fingerFastSearch();

if (p == FINGERPRINT_OK)
{
  scanning = false;
  counter = 0;
  if(add_new_id)
  {
    if(finger.fingerID == main_user_ID)
    {
      main_user = true;
      id_ad = false;
    }
    else
    {
      add_new_id = false;
      main_user = false;
      id_ad = false;
    }
    
  }
  else
  {
  miServo.write(door_open_delay); //delay so door is open
  digitalWrite(RELAY_PIN,HIGH);       //
  digitalWrite(RELAY_PIN,LOW);    //
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("   User match   ");
  
  lcd.setCursor(0,1);
  lcd.print(" ID: #");
  
  lcd.setCursor(6,1);
  lcd.print(finger.fingerID);

  lcd.setCursor(9,1);
  lcd.print("%: ");

  lcd.setCursor(12,1);
  lcd.print(finger.confidence);

  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.print(" -- User match for ID# "); myFile.print(finger.fingerID);
  myFile.print(" with confidence: ");   myFile.print(finger.confidence); myFile.println(" - door open");
  myFile.close(); 
  door_locked = false;
  delay(4000);
  lcd.setCursor(0,0);
  lcd.print("   Press SCAN   ");
  lcd.setCursor(0,1);
  lcd.print("  -door  open-  ");
  delay(50);
  }
}//end finger OK

else if(p == FINGERPRINT_NOTFOUND)
{
  scanning = false;
  id_ad = false;
  counter = 0;
  lcd.setCursor(0,0);
  lcd.print("    No match    ");
  lcd.setCursor(0,1);
  lcd.print("   Try again!   ");
  add_new_id = false;
  main_user = false;  

  myFile = SD.open(file_name_to_save, FILE_WRITE);
  myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
  myFile.print(" -- No match for any ID. Door status still the same"); 
  myFile.close(); 
  delay(2000);
  if(door_locked)
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print(" -door  locked- ");   
    }
    else
    {
      lcd.setCursor(0,0);
      lcd.print("   Press SCAN   ");
      lcd.setCursor(0,1);
      lcd.print("  -door  open-  ");  
    }
  delay(2);
  return p;
}//end finger error
}// returns -1 if failed, otherwise returns ID #


int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
return finger.fingerID;
}









//////////////////////////////////////////










/*This function will add new ID to the database*/
uint8_t getFingerprintEnroll() {

  int p = -1;
  if(!first_read)
  {
  lcd.setCursor(0,0);
  lcd.print("Add new as ID#  ");
  lcd.setCursor(14,0);
  lcd.print(id);
  lcd.setCursor(0,1);
  lcd.print("  Place finger  ");  
  }
  
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("  Image taken!  ");
      lcd.setCursor(0,1);
      lcd.print("                ");  
      delay(100);
      first_read = true;
      break;
    case FINGERPRINT_NOFINGER:
      lcd.setCursor(0,0);
      lcd.print("Add new as ID#  ");
      lcd.setCursor(14,0);
      lcd.print(id);
      lcd.setCursor(0,1);
      lcd.print("  Place finger  "); 
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    case FINGERPRINT_IMAGEFAIL:
      lcd.setCursor(0,0);
      lcd.print("     -Image     ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("Image converted!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0,0);
      lcd.print("Image too messy!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }
  
  lcd.setCursor(0,0);
  lcd.print(" Remove finger! ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  lcd.setCursor(0,1);
  lcd.print("ID# ");
  lcd.setCursor(4,1);
  lcd.print(id);
  p = -1;
  lcd.setCursor(0,0);
  lcd.print("Place again the ");
  lcd.setCursor(0,1);
  lcd.print("same finger     ");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("  Image taken!  ");
      lcd.setCursor(0,1);
      lcd.print("                "); 
      break;
    case FINGERPRINT_NOFINGER:
      lcd.setCursor(0,0);
      lcd.print("Place again the ");
      lcd.setCursor(0,1);
      lcd.print("same finger     ");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    case FINGERPRINT_IMAGEFAIL:
      lcd.setCursor(0,0);
      lcd.print("     -Image     ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      lcd.setCursor(0,0);
      lcd.print("Image converted!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      break;
    case FINGERPRINT_IMAGEMESS:
      lcd.setCursor(0,0);
      lcd.print("Image too messy!");
      lcd.setCursor(0,1);
      lcd.print("                ");
      delay(1000);
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    case FINGERPRINT_FEATUREFAIL:
      lcd.setCursor(0,0);
      lcd.print(" No fingerprint ");
      lcd.setCursor(0,1);
      lcd.print("features found  ");
      delay(1000);
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      lcd.setCursor(0,0);
      lcd.print("  Comunication  ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
    default:
      lcd.setCursor(0,0);
      lcd.print("    -Unknown    ");
      lcd.setCursor(0,1);
      lcd.print("     ERROR!     ");
      delay(1000);
      return p;
  }
  
  // OK converted!
 lcd.setCursor(0,0);
 lcd.print(" Creating model ");
 lcd.setCursor(0,1);
 lcd.print("for ID# ");
 lcd.setCursor(8,1);
 lcd.print(id);
  
  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0,0);
    lcd.print(" Print matched! ");
    lcd.setCursor(0,1);
    lcd.print("                ");
    delay(1000);
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0,0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    lcd.setCursor(0,0);
    lcd.print("Fingerprint did ");
    lcd.setCursor(0,1);
    lcd.print("not match       ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0,0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  }   
  
  lcd.setCursor(0,1);
  lcd.print("ID# ");
  lcd.setCursor(4,1);
  lcd.print(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    lcd.setCursor(0,0);
    lcd.print("     Stored     ");
    lcd.setCursor(0,1);
    lcd.print("                ");
    myFile = SD.open(file_name_to_save, FILE_WRITE);
    myFile.print(rtc.getDateStr()); myFile.print(" -- "); myFile.print(rtc.getTimeStr());
    myFile.print(" -- New fingerprint stored for ID# "); myFile.println(id);
    myFile.close(); 
    delay(1000);
    first_read = false;
    id_ad = false;
    
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    lcd.setCursor(0,0);
    lcd.print("  Comunication  ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    lcd.setCursor(0,0);
    lcd.print("Could not store ");
    lcd.setCursor(0,1);
    lcd.print("in that location");
    delay(1000);
    return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    lcd.setCursor(0,0);
    lcd.print("Error writing to");
    lcd.setCursor(0,1);
    lcd.print("flash           ");
    delay(1000);
    return p;
  } else {
    lcd.setCursor(0,0);
    lcd.print("    -Unknown    ");
    lcd.setCursor(0,1);
    lcd.print("     ERROR!     ");    
    delay(1000);
    return p;
  }   
}




#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

// Pin assignments
const int FINGERPRINT_RX = 10;
const int FINGERPRINT_TX = 11;
const int RELAY_PIN = 6;
const int BUZZER_PIN = 8;

SoftwareSerial fingerprintSerial(FINGERPRINT_RX, FINGERPRINT_TX);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&fingerprintSerial);

// Variables to control the relay
bool relayOn = false;
unsigned long relayStartTime = 0;

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

  // Initialize fingerprint sensor
  finger.begin(57600);
  if (!finger.verifyPassword()) {
    Serial.println("Fingerprint sensor not found");
    while (true);
  }
  finger.getTemplateCount();
  Serial.print("Sensor contains ");
  Serial.print(finger.templateCount);
  Serial.println(" templates");

  // Initialize relay pin
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  // Initialize buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  Serial.println("Ready to scan fingerprint");
}

void loop() {
  if (getFingerprintIDez() != -1) {
    // Fingerprint match, unlock the door
    digitalWrite(RELAY_PIN, HIGH);
    relayOn = true;
    relayStartTime = millis();
    Serial.println("Door unlocked");

    // Turn off buzzer
    digitalWrite(BUZZER_PIN, LOW);
  }

  // Turn off relay after 10 seconds
  if (relayOn && (millis() - relayStartTime) > 10000) {
    digitalWrite(RELAY_PIN, LOW);
    relayOn = false;
    Serial.println("Door locked");
  }
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK) {
    Serial.println("Error getting fingerprint image");
    return -1;
  }

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK) {
    Serial.println("Error converting fingerprint image");
    return -1;
  }

  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Fingerprint match found");
    return finger.fingerID;
  } else {
    Serial.println("Fingerprint match not found");
    digitalWrite(BUZZER_PIN, HIGH);
    delay(1000);
    digitalWrite(BUZZER_PIN, LOW);
    return -1;
  }
}

Since English is not my mother tongue, it's difficult to understand what you want when you always write sentences without periods. I hope I can help you though. I understand you want the lock to close automatically after 10 seconds? I've updated the above code as the bottom one is a bit long... I'll definitely take a look at the bottom one as well. But I can't guarantee that the code will work because I can't test it myself.

I hope I was able to help you anyway.
PS. If you have another question, you can put it in a new post so other people can help too; maybe there are better answers. It would also be better if it was always a new post, because then you can mark the answers as "solved" again and again. But this is only a recommendation.

1 Like

wait mark cleo i want to inform me how to make the physical circuit

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.