#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <Keypad.h>
// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo motor
Servo myservo;
const int servoPin = 4; // GPIO pin to control servo
// Keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 12, 13, 14}; // Row pins (R1-R4)
byte colPins[COLS] = {16, 17, 18, 19}; // Column pins (C1-C4)
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Relay module
const int relayPin = 15; // GPIO pin to control relay
// Buzzer
const int buzzerPin = 2; // GPIO pin to control buzzer
String inputPassword = "";
const String correctPassword = "1234";
void setup() {
Wire.begin();
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Attach servo to pin
myservo.attach(servoPin);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is initially off
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Ensure buzzer is initially off
Serial.begin(115200);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
// Check password
if (inputPassword == correctPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
myservo.write(90); // Move servo to 90 degrees
digitalWrite(relayPin, HIGH); // Turn on relay
// Turn off buzzer
delay(5000); // Keep relay on for 5 seconds
myservo.write(0); // Return servo to 0 degrees
digitalWrite(relayPin, LOW); // Turn off relay
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
delay(1000); // Keep buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Turn off buzzer
delay(2000); // Display "Access Denied" for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
inputPassword = ""; // Reset input password
} else if (key == '*') {
// Clear input
inputPassword = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
} else {
// Append key to password
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
}