/*
ESP32-Based Smart Door Lock System
Purpose:
This project implements a smart door lock system using an ESP32 microcontroller. The system consists of:
- A 16x2 I2C LCD for displaying instructions and feedback.
- A 4x4 matrix keypad for entering the password.
- A servo motor to physically lock and unlock the door.
- A predefined password that must be entered correctly to unlock the door.
- A feature to hide the entered password using '*' characters on the LCD.
How it works:
- The user enters a 6-digit password using the keypad.
- The LCD displays '*' for each key press to hide the actual password.
- If the correct password is entered, the servo rotates to unlock the door.
- After a few seconds, the door locks automatically.
- If an incorrect password is entered, an error message is displayed, and the system resets.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Library for the LCD
#include <Keypad.h> // Library for handling keypad input
#include <ESP32Servo.h> // Library for controlling the servo on ESP32
// LCD Configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo Configuration
Servo myServo;
int servoPin = 13; // ESP32 GPIO pin for the servo (Ensure this is a PWM-capable pin)
int closedPos = 0; // Position when the door is locked
int openPos = 90; // Position when the door is unlocked
// Keypad Configuration
const uint8_t ROWS = 4; // Number of rows in the keypad
const uint8_t COLS = 4; // Number of columns in the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
uint8_t rowPins[ROWS] = {19, 18, 5, 17}; // GPIO pins connected to the rows
uint8_t colPins[COLS] = {16, 4, 0, 2}; // GPIO pins connected to the columns
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Password Configuration
String password = "123456"; // This is the password
String inputP = ""; // This will store user input
void setup() {
myServo.attach(servoPin);
myServo.write(closedPos);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
inputP += key;
lcd.setCursor(0, 1);
// Display '*' for each character entered (hiding the actual password)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
for (int i = 0; i < inputP.length(); i++) {
lcd.print("*");
}
if (inputP.length() == 6) {
if (inputP == password) { // If the entered password is correct
lcd.clear();
lcd.print("Unlocked"); // Display unlocked message
myServo.write(openPos); // Move servo to unlock the door
delay(3000); // Keep the door open for 3 seconds
lcd.clear();
lcd.print("Enter Password:"); // Reset display
myServo.write(closedPos); // Lock the door again
} else {
lcd.clear();
lcd.print("Wrong Password"); // Display incorrect password message
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.print("Enter Password:"); // Reset display
}
inputP = ""; // Reset input for next attempt
}
}
}