#include <Keypad.h>
#include <ESP32Servo.h> // Use ESP32 Servo library for compatibility
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Ensure this is the correct I2C address
// 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'}
};
// Define the row and column pin configuration for the ESP32-S3
byte rowPins[ROWS] = {47, 48, 45, 0}; // Use GPIO pins available on ESP32-S3
byte colPins[COLS] = {35, 36, 37, 38}; // Use GPIO pins available on ESP32-S3
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo
Servo myServo;
const int servoPin = 13; // GPIO for the servo motor
// PIR Sensors and LED
const int pirPin = 12; // GPIO for the first PIR sensor
const int pirPin1 = 39; // GPIO for the second PIR sensor
const int ledPin = 19; // GPIO for the LED
void setup() {
// Initialize LCD
Wire.begin(21,20);
lcd.init(); // Correct initialization
lcd.backlight(); // Ensure backlight is on
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Initialize Servo
myServo.attach(servoPin);
myServo.write(0); // Locked position
// Initialize PIR Sensor and LED
pinMode(pirPin, INPUT);
pinMode(pirPin1, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Initially turn off the LED
// Initialize Serial Monitor
Serial.begin(115200); // ESP32 works at higher baud rate
}
void loop() {
static String inputPassword = "";
const String correctPassword = "21082004A"; // Set your desired password
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Submit password
if (inputPassword == correctPassword) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
myServo.write(90); // Unlock position
delay(5000);
myServo.write(0); // Lock position
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
delay(2000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
inputPassword = ""; // Clear the input
} else if (key == '*') {
// Clear input
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second row
} else {
// Add key to input
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
// PIR Sensor detection for LED
if (digitalRead(pirPin) == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED if motion detected
} else {
digitalWrite(ledPin, LOW); // Turn off LED if no motion detected
}
// PIR Sensor detection for Servo
if (digitalRead(pirPin1) == HIGH) {
myServo.write(90); // Unlock servo if motion detected by second PIR sensor
} else {
myServo.write(0); // Lock servo if no motion
}
}