EXP : 7 INTERFACING KEYBOARD AND STEPPER MOTOR
AIM :
To simulate interfacing a keypad and stepper motor to create a password protected door lock
system.
APPARATUS REQUIRED:
1. ESP32 board
2. Keypad
3. 16×2 I2C LCD
4. Servo motor (acts as stepper substitute)
5. Jumper wires
6. Breadboard (for hardware)
7. Wokwi simulator (for online use)
PROCEDURE:
1. Open wokwi simulator online and create a new project by selecting "ESP32" as the
microcontroller.
2. From the components panel, add a 4x4 Keypad, 16x2 I2C LCD, and a Servo Motor to the
workspace.
3. Connect the keypad rows to ESP32 pins 23, 19, 18, 5 and columns to 17, 16, 4, 2.
4. Connect the LCD Display’s SDA to ESP32 pin 21 and SCL to pin 22 .
5. Connect the signal wire of the servo motor to pin 15 of the ESP32.
6. Ensure all components share a common GND and VCC appropriately .
7. Copy and paste the given program code into the code editor in Wokwi.
8. Click the green "Start Simulation" button to run the project.
9. Use the keypad to enter the password. The LCD shows the status, and the servo rotates on
correct input.
PROGRAM:
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Password.h>
#define servoPin 15
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
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] = {23, 19, 18, 5};
byte colPins[COLS] = {17, 16, 4, 2};
bool isDoorLocked = true;
Password password = Password("1234");
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
servo.attach(servoPin);
servo.write(10);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Door LOCK System");
delay(2000);
lcd.clear();
void loop() {
lcd.setCursor(3, 0);
isDoorLocked ? lcd.print("DOOR LOCKED") : lcd.print("DOOR OPEN");
char key = keypad.getKey();
Serial.println(key);
if (key != NO_KEY) {
delay(100);
if (key == '#') {
resetPassword();
} else if (key == '*') {
isDoorLocked = !isDoorLocked;
isDoorLocked ? doorLocked() : doorOpen();
} else {
processNumberKey(key);
void processNumberKey(char key) {
if (currentPasswordLength < maxPasswordLength) {
lcd.setCursor(currentPasswordLength + 5, 1);
lcd.print("*");
password.append(key);
currentPasswordLength++;
void doorLocked() {
if (password.evaluate()) {
servo.write(10);
displayMessage("Password Correct", "LOCKED");
} else {
displayMessage("Wrong Password", "Try Again");
resetPassword();
}
void doorOpen() {
if (password.evaluate()) {
servo.write(180);
displayMessage("Password Correct", "UNLOCKED");
} else {
displayMessage("Wrong Password", "Try Again");
resetPassword();
void resetPassword() {
password.reset();
currentPasswordLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
void displayMessage(const char *line1, const char *line2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(3, 1);
lcd.print(line2);
delay(3000);
lcd.clear();
}
DIAGRAM:
OUTPUT:
1) If password is correct door locking or unlocking happen ,
2) If password incorrect it can be viewed as wrong password Try Again ,
RESULT:
Thus the simulation of interfacing a keypad and stepper motor to create a password
protected door lock system was done successfully .