Safe using arduino with keypad and finger print reader

I am building a safe using an arduino uno with a keypad and finger print reader. The safe always opens even though no finger is placed on the reader. Any idea how to fix this?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
#include <Servo.h>
#include <SoftwareSerial.h>

// Define the pins
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);

SoftwareSerial mySerial(11, 12); // RX, TX for SoftwareSerial
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

Servo safeServo;

// Define variables
const int correctPin = 1234; // Change this to your desired PIN
bool safeOpen = false;

// LED and buzzer pins
const int redLedPin = A1;
const int greenLedPin = A2;
const int buzzerPin = 13;

// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize fingerprint sensor
  finger.begin(57600); // Change the argument to match your Serial object
  finger.setSecurityLevel(5);

  // Initialize LCD
  lcd.begin(16, 2);
  lcd.print("Enter PIN:");

  // Attach servo
  safeServo.attach(10); // Use pin 10 for the servo

  // Set initial LED states
  digitalWrite(redLedPin, HIGH);
  digitalWrite(greenLedPin, LOW);
}

void loop() {
  int enteredPin = readKeypad();

  if (enteredPin == correctPin) {
    lcd.clear();
    lcd.print("Place finger...");
    delay(5000);

    if (validateFingerprint()) {
      openSafe();
      delay(5000);
      closeSafe();
    } else {
      buzzAndDelay();
    }
  } else {
    buzzAndDelay();
  }
}

int readKeypad() {
  lcd.clear();
  lcd.print("Enter PIN:");

  char enteredChars[5];
  int i = 0;

  while (true) {
    char key = keypad.getKey();
    if (key) {
      if (key == '#') {
        enteredChars[i] = '\0';
        lcd.clear();
        break;
      } else {
        lcd.setCursor(i, 1);
        lcd.print('*');
        enteredChars[i] = key;
        i++;
      }
    }
  }

  return atoi(enteredChars);
}

bool validateFingerprint() {
  lcd.clear();
  lcd.print("Place finger...");

  while (!finger.getImage());

  int fingerprintID = finger.fingerFastSearch();

  if (fingerprintID == -1 || finger.confidence < 50) {
    lcd.clear();
    lcd.print("Fingerprint");
    lcd.setCursor(0, 1);
    lcd.print("recognized");
    delay(2000);
    return true;
  } else {
    lcd.clear();
    lcd.print("Fingerprint not");
    lcd.setCursor(0, 1);
    lcd.print("recognized");
    delay(1000);
    return false;
  }
}


void openSafe() {
  lcd.clear();
  lcd.print("Safe open");
  safeServo.write(0); // Open the safe
  digitalWrite(redLedPin, LOW);
  digitalWrite(greenLedPin, HIGH);
  safeOpen = true;
}

void closeSafe() {
  if (safeOpen) {
    lcd.clear();
    lcd.print("Press * to close");
    while (true) {
      char key = keypad.getKey();
      if (key == '*') {
        lcd.clear();
        lcd.print("Safe closed");
        safeServo.write(90);
        digitalWrite(redLedPin, HIGH);
        digitalWrite(greenLedPin, LOW);
        safeOpen = false;
        break;
      }
    }
  }
}

void buzzAndDelay() {
  lcd.clear();
  lcd.print("Access denied");
  digitalWrite(buzzerPin, HIGH);
  delay(1000);
  digitalWrite(buzzerPin, LOW);
  delay(1000);
}

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
#include <Servo.h>
#include <SoftwareSerial.h>

// Define the pins
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);

SoftwareSerial mySerial(11, 12); // RX, TX for SoftwareSerial
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

Servo safeServo;

// Define variables
const int correctPin = 1234; // Change this to your desired PIN
bool safeOpen = false;

// LED and buzzer pins
const int redLedPin = A1;
const int greenLedPin = A2;
const int buzzerPin = 13;

// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize fingerprint sensor
  finger.begin(57600); // Change the argument to match your Serial object
  finger.setSecurityLevel(5);

  // Initialize LCD
  lcd.begin(16, 2);
  lcd.print("Enter PIN:");

  // Attach servo
  safeServo.attach(10); // Use pin 10 for the servo
  closeSafe();
}

void loop() {
  bool enteredPin = readKeypad();

  if (enteredPin && validateFingerprint())     openSafe();
  else     buzzAndDelay();
}

bool readKeypad() {
  lcd.clear();
  lcd.print("Enter PIN:");
  lcd.setCursor(2, 1);
  byte four = 4;
  int i = 0;

  while (four) {
    char key = keypad.getKey();
    if (key == '#') four = 0;
    else if (key >= '0' && key <= '9') {
      lcd.print('*');
      i = i * 10 + key - '0';
      four--;
      delay(100);
    }
  }
  delay(1000);
  return correctPin == i;
}

bool validateFingerprint() {
  lcd.clear();
  lcd.print("Place finger...");

  while (!finger.getImage());

  int fingerprintID = finger.fingerFastSearch();

  if (fingerprintID == FINGERPRINT_OK && finger.confidence > 50) {
    lcd.clear();
    lcd.print("Fingerprint");
    lcd.setCursor(0, 1);
    lcd.print("recognized");
    //delay(2000);
    return true;
  } else {
    lcd.clear();
    lcd.print("Fingerprint not");
    lcd.setCursor(0, 1);
    lcd.print("recognized");
    delay(1000);
    return false;
  }
}


void openSafe() {
  lcd.clear();
  lcd.print("Safe open");
  safeServo.write(0); // Open the safe
  digitalWrite(redLedPin, LOW);
  digitalWrite(greenLedPin, HIGH);
  safeOpen = true;
  delay(5000);
  closeSafe();
}

void closeSafe() {
  lcd.clear();
  lcd.print("Press * to close");
  char key = 0;
  do {
    key = keypad.getKey();
  } while (key != '*');
  safeServo.write(90);
  digitalWrite(redLedPin, HIGH);
  digitalWrite(greenLedPin, LOW);

  lcd.clear();
  lcd.print("Safe closed");
}

void buzzAndDelay() {
  lcd.clear();
  lcd.print("Access denied");
  digitalWrite(buzzerPin, HIGH);
  delay(1000);
  digitalWrite(buzzerPin, LOW);
  delay(5000);
}

Your correction is still opening the safe even without any finger placed. I think the main issue with the code is that the " bool validateFingerprint()" isn't correct. btw I have no coding experience.

do you have registered your fingerprints in fingerprint scanner?

upd. try now.

Yes, I have 4 different fingerprints registered.

Maybe you should test it one by one. If a fingerprint doesn't works, it means your code have problem.

wrong was this

if (fingerprintID == -1 || finger.confidence < 50) 

and it is not his code, probably ChatGPT

Wow! Thanks for your info.

Thank you very much everyone. I got one of my professor to help me out. It works now. I have also attached the working code if anyone is interested.

/***************************************************

 ****************************************************/


#include <Adafruit_Fingerprint.h>
#include <I2CKeyPad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include <SoftwareSerial.h>


#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
// Set up the serial port to use softwareserial..
SoftwareSerial mySerial(11, 12);

#else
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
// #0 is green wire, #1 is white
#define mySerial Serial1

#endif

// Define the pins
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);


Servo safeServo;

// Define variables
const int correctPin = 1234; // Change this to your desired PIN
bool safeOpen = false;

// LED and buzzer pins
const int redLedPin = A1;
const int greenLedPin = A2;
const int buzzerPin = 13;

// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

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

  lcd.begin(16, 2);
  lcd.print("Enter PIN:");

    // Attach servo
  safeServo.attach(10); // Use pin 10 for the servo

  // Set initial LED states
  digitalWrite(redLedPin, HIGH);
  digitalWrite(greenLedPin, LOW);

  // Set Buzzer as output
  pinMode(buzzerPin, OUTPUT);

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

  Serial.println(F("Reading sensor parameters"));
  finger.getParameters();
  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);

  finger.getTemplateCount();

  if (finger.templateCount == 0) {
    Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  }
  else {
    Serial.println("Waiting for valid finger...");
      Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  }
}

void loop()                     // run over and over again
{
  int enteredPin = readKeypad();
  Serial.println(enteredPin);
  Serial.println(correctPin);
  if (enteredPin == correctPin) {
    lcd.clear();
    lcd.print("Place finger...");
    delay(5000);
    int x = getFingerprintID();
    Serial.println("x:");Serial.println(x);
    delay(50);            //don't ned to run this at full speed.
    if (x == 1 || x == 3 || x == 4) {
      openSafe();
      delay(5000);
      closeSafe();
    } 
    else if (x == 2) {
      buzzAndDelay();
    }
  } else {
    buzzAndDelay();
  }

}

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");
      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.fingerSearch();
  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;
}

int readKeypad() {
  lcd.clear();
  lcd.print("Enter PIN:");

  char enteredChars[5];
  int i = 0;

  while (true) {
    char key = keypad.getKey();
    if (key) {
      if (key == '#') {
        enteredChars[i] = '\0';
        lcd.clear();
        break;
      } else {
        lcd.setCursor(i, 1);
        lcd.print('*');
        enteredChars[i] = key;
        i++;
      }
    }
  }
  Serial.println(enteredChars);
  return atoi(enteredChars);
}

void buzzAndDelay() {
  lcd.clear();
  lcd.print("Access denied");
  digitalWrite(buzzerPin, HIGH);
  delay(500);
  digitalWrite(buzzerPin, LOW);
  delay(500);
  digitalWrite(buzzerPin, HIGH);
  delay(500);
  digitalWrite(buzzerPin, LOW);
  delay(500);
  digitalWrite(buzzerPin, HIGH);
  delay(500);
  digitalWrite(buzzerPin, LOW);
  delay(500);
  digitalWrite(buzzerPin, HIGH);
  delay(500);
  digitalWrite(buzzerPin, LOW);
  delay(500);
}

void openSafe() {
  lcd.clear();
  lcd.print("Safe open");
  safeServo.write(0); // Open the safe
  digitalWrite(redLedPin, LOW);
  digitalWrite(greenLedPin, HIGH);
  safeOpen = true;
}

void closeSafe() {
  if (safeOpen) {
    lcd.clear();
    lcd.print("Press * to close");
    while (true) {
      char key = keypad.getKey();
      if (key == '*') {
        lcd.clear();
        lcd.print("Safe closed");
        safeServo.write(90);
        digitalWrite(redLedPin, HIGH);
        digitalWrite(greenLedPin, LOW);
        safeOpen = false;
        break;
      }
    }
  }
}

Hi, i was wondering if you could post the schematics/wiring of your work. Coincidentally I'm making a door security system for a project with the exact same components, your work is the closest I've found ( most of don't have a fingerprint sensor combined with all the other components). I would greatly appreciate it.

Here is my exact wiring.

1 Like

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