// Include the library to control Servo motors with ESP32
#include <ESP32Servo.h>
// Include the library to use a keypad
#include <Keypad.h>
// Include the library to control an I2C LCD display
#include <LiquidCrystal_I2C.h>
// Define Blynk credentials for the project
#define BLYNK_TEMPLATE_ID "*********************"
#define BLYNK_TEMPLATE_NAME "IoT Project -Password protect door lock using ESP32 "
#define BLYNK_AUTH_TOKEN "*************************"
// Include libraries required for Blynk and Wi-Fi connectivity
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
// WiFi credentials
char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXX"; // SSID of the Wi-Fi network
char pass[] = "XXXXXXXXXXXXXXXX"; // Wi-Fi password (empty for Wokwi guest)
// Define ultrasonic sensor pins
#define PIN_TRIG 34 // Trigger pin of ultrasonic sensor
#define PIN_ECHO 35 // Echo pin of ultrasonic sensor
#define timeDelay 5000 // Delay in milliseconds after successful unlock
// Define keypad configuration
const uint8_t ROWS = 4; // Number of rows in keypad
const uint8_t COLS = 3; // Number of columns in keypad
// Key mapping on the keypad
char keys[ROWS][COLS] = {
{ '1', '2', '3',},
{ '4', '5', '6',},
{ '7', '8', '9',},
{ '*', '0', '#',}
};
// Pin numbers for keypad rows and columns
uint8_t rowPins[ROWS] = { 23, 19, 18, 15 }; // Connect to R1, R2, R3, R4
uint8_t colPins[COLS] = { 5, 4, 2 }; // Connect to C1, C2, C3
// Create Servo, LCD, and Keypad objects
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns x 4 rows LCD
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define default password
String password = "********";
String enterPass = ""; // To store user-entered password
bool passEntered; // Flag to check if password prompt was shown
void setup() {
Serial.begin(9600); // Start serial communication
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk server using credentials
myservo.attach(11); // Attach servo to GPIO 11
lcd.init(); // Initialize the LCD
pinMode(PIN_TRIG, OUTPUT); // Set TRIG pin as output
pinMode(PIN_ECHO, INPUT); // Set ECHO pin as input
}
void loop() {
// Send a 10 microsecond pulse to trigger the ultrasonic sensor
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
Blynk.run(); // Keep the Blynk connection alive
// Read distance from ultrasonic sensor
int duration = pulseIn(PIN_ECHO, HIGH); // Time of echo pulse
int distance = duration / 58; // Convert to centimeters
if(distance < 150) { // If an object (e.g., person) is within 150cm
char key = keypad.getKey(); // Read key press from keypad
lcd.backlight(); // Turn on LCD backlight
if (passEntered == false) { // If password prompt not shown yet
lcd.setCursor(1, 1);
lcd.print("ENTER PASSWORD :");
passEntered = true; // Mark prompt as shown
}
if (key != NO_KEY) { // If a key was pressed
if (key == '#') { // If user presses '#' to submit password
if (password == enterPass) { // If password is correct
beep(); // Sound buzzer
myservo.write(0); // Open the door (servo moves to 0°)
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("PASSWORD CORRECT");
lcd.setCursor(1, 1);
lcd.print("DOOR OPENED");
delay(timeDelay); // Wait before closing again
lcd.clear();
reset(); // Reset input and flags
}
else { // If password is incorrect
myservo.write(90); // Keep door closed (servo at 90°)
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("PASSWORD INCORRECT");
reset(); // Reset input and flags
}
}
else { // If key is a digit (not '#')
enterPass += key; // Append digit to entered password
lcd.setCursor(1, 3);
lcd.print(enterPass); // Show entered password on LCD
}
}
} else { // No one is near the sensor
myservo.write(90); // Keep door closed (servo at 90°)
lcd.clear();
lcd.noBacklight(); // Turn off LCD backlight to save power
reset(); // Reset input and flags
}
}
// Function to sound a buzzer
void beep() {
tone(8, 1000, 50); // Play tone on pin 8 at 1kHz for 50ms
}
// Function to reset password input
void reset() {
passEntered = false; // Reset flag
enterPass = ""; // Clear entered password
}