// a Door lock system using
// Arduino, keypad, lcd 16*2 display, servo motor, buzzer
// door lock password '1234'. '*' to enter. '#' to clear screen.
// Helal Uzzaman Hasib
// 19 May 2023
#include <Arduino.h>
#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <ezBuzzer.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo myservo;
int pos = 0; // variable to store the servo position
// Define a password for unlocking the door
const String password = "1234";
// Define the buzzer pin
const int buzzerPin = 12; // Connect buzzer to this Arduino pin
ezBuzzer buzzer(buzzerPin); // create ezBuzzer object that attach to a pin;a
void showInfo() {
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
}
void setup() {
myservo.attach(11);
myservo.write(0);
Serial.begin(9600);
// Init
lcd.init();
lcd.backlight();
showInfo();
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
digitalWrite(buzzerPin, LOW); // Turn off the buzzer initially
buzzer.beep(50); // generates a 50ms beep
}
void openDoor() {
lcd.setCursor(0, 1);
lcd.print("Opening Door");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(0); // waits 15ms for the servo to reach the position
}
}
void closeDoor() {
lcd.setCursor(0, 1);
lcd.print("Closing Door");
for (pos = 180; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(0); // waits 15ms for the servo to reach the position
}
}
void activateBuzzer() {
digitalWrite(buzzerPin, HIGH);
}
void deactivateBuzzer() {
digitalWrite(buzzerPin, LOW);
}
void checkPassword(char key) {
static String enteredPassword = ""; // Store the entered password
static int attempts = 0; // Number of incorrect attempts
if (key == '*') {
// Check if the entered password matches the predefined password
if (enteredPassword == password) {
lcd.clear();
lcd.print("Access Granted!");
openDoor(); // Unlock the door
delay(2000);
closeDoor(); // Lock the door again
lcd.clear();
lcd.setCursor(0, 0);
showInfo();
enteredPassword = ""; // Reset the entered password
attempts = 0; // Reset the number of attempts
} else {
attempts++;
lcd.clear();
lcd.print("Incorrect ");
lcd.print(attempts);
lcd.setCursor(0, 1);
lcd.print(" Press # ");
if (attempts >= 3) {
lcd.clear();
lcd.print("Security Alert!");
activateBuzzer(); // Activate the buzzer for 10 seconds
delay(10000);
deactivateBuzzer();
lcd.clear();
lcd.setCursor(0, 0);
showInfo();
enteredPassword = ""; // Reset the entered password
attempts = 0; // Reset the number of attempts
}
}
} else if (key == '#') {
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0);
showInfo();
enteredPassword = "";
}
else {
enteredPassword += key; // Append the entered key to the password string
}
}
//================================loop==================================
void loop() {
buzzer.loop();
char key = keypad.getKey(); // Read the keypad input
if (key) {
// Check if the entered key is a digit or asterisk
if (isdigit(key) || key == '*' || key == '#') {
lcd.print('*'); // Display asterisks instead of the actual key
checkPassword(key); // Check the entered password
buzzer.beep(50); // generates a 50ms beep
};
}
}