/*
Project : Arduino Keypad Password Lock System
Author : Moamen Abouhaty
Board : Arduino UNO
Year : 2026
Copyright : © 2026 Moamen Abouhaty
Description:
- Password-based access control system
- Uses Keypad for input
- LCD for user feedback
- Green LED for success
- Red LED for failure
- Buzzer for sound effects
*/
#include <Arduino.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// ================= LCD Configuration =================
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address 0x27, 16 columns, 2 rows
// ================= Keypad Configuration =================
const byte ROWS = 4;
const byte COLS = 4;
// Keypad button layout
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Arduino pins connected to keypad rows and columns
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Create keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ================= Hardware Pins =================
#define BUZZER 10
#define GREEN_LED 11
#define RED_LED 12
// ================= Password Settings =================
String password = "1234"; // Correct password
String input = ""; // User input buffer
// ================= Function Prototypes =================
void checkPassword();
void resetInput();
void resetAll();
void playMario();
void playError();
// ================= Setup =================
void setup() {
pinMode(BUZZER, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
}
// ================= Main Loop =================
void loop() {
char key = keypad.getKey(); // Read pressed key
if (key) {
tone(BUZZER, 1000, 50); // Beep sound on key press
if (key == '#') {
// Confirm password
checkPassword();
}
else if (key == '*') {
// Clear input
resetInput();
}
else {
// Add key to input and show * on LCD
input += key;
lcd.print("*");
}
}
}
// ================= Password Check =================
void checkPassword() {
lcd.clear();
if (input == password) {
lcd.print("Access Granted");
digitalWrite(GREEN_LED, HIGH);
playMario();
} else {
lcd.print("Access Denied");
digitalWrite(RED_LED, HIGH);
playError();
}
delay(2000);
resetAll();
}
// ================= Reset Functions =================
void resetInput() {
input = "";
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0, 1);
}
void resetAll() {
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
resetInput();
}
// ================= Sound Effects =================
void playError() {
tone(BUZZER, 200, 300);
delay(300);
tone(BUZZER, 100, 500);
delay(500);
noTone(BUZZER);
}
void playMario() {
int melody[] = {660, 660, 0, 660, 0, 510, 660, 0, 770};
int duration[] = {150, 150, 150, 150, 150, 150, 150, 150, 300};
for (int i = 0; i < 9; i++) {
if (melody[i] > 0) {
tone(BUZZER, melody[i], duration[i]);
}
delay(duration[i] * 1.3);
}
noTone(BUZZER);
}