ESP32 - Keypad Door Lock

This tutorial will guide you on how to create a door lock system with a password using a keypad and solenoid lock. The system will unlock the door when the correct password is entered and will keep the door unlocked for 20 seconds before automatically relocking it. The ESP32 code is also capable of supporting multiple passwords.

※ NOTE THAT:

In addition to this tutorial, we also have other tutorials on door lock systems available:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Keypad 3x4
1×Relay
1×Solenoid Lock
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Keypad and Solenoid Lock

We have separate tutorials that focus specifically on keypads and solenoid locks. Each tutorial provides detailed information and step-by-step instructions on hardware pinout, working principle, wiring connections to ESP32, ESP32 code, and more. For more information on these tutorials, please refer to the following links:

Wiring Diagram

  • ESP32 - Door lock with keypad, solenoid lock
ESP32, keypad, solenoid lock wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code - Door lock system with password using keypad, solenoid lock

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-keypad-door-lock */ #include <Keypad.h> #define RELAY_PIN 19 // ESP32 pin GPIO19 connected to the relay #define ROW_NUM 4 // keypad four rows #define COLUMN_NUM 3 // keypad three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; byte pin_rows[ROW_NUM] = {12, 14, 27, 26}; // ESP32 pin: GPIO12, GPIO14, GPIO27, GPIO26 connected to the row pins byte pin_column[COLUMN_NUM] = {25, 33, 32}; // ESP32 pin: GPIO25, GPIO33, GPIO32 connected to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234567"; // change your password here const String password_2 = "987654"; // change your password here const String password_3 = "55555"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 33, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // lock the door } void loop() { char key = keypad.getKey(); if (key){ Serial.println(key); if(key == '*') { input_password = ""; // reset the input password } else if(key == '#') { if(input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct, unlocking the door in 20 seconds"); digitalWrite(RELAY_PIN, HIGH); // unlock the door for 20 seconds delay(20000); digitalWrite(RELAY_PIN, LOW); // lock the door } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Quick Instructions

  • Connect the ESP32 to your PC using a USB cable.
  • Open the Arduino IDE, select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for the “keypad” library, then find the keypad library created by Mark Stanley and Alexander Brevig.
  • Click the Install button to install the keypad library.
ESP32 keypad library
  • Copy the provided code and paste it into the Arduino IDE.
  • Compile and upload the code to the ESP32 board by clicking the Upload button in the Arduino IDE.
  • Open the Serial Monitor in the Arduino IDE.
  • Press the keys 12345 and then press #
  • Press the keys 1234567 and then press #
  • Observe the state of the lock tongue for 20 seconds.
  • Observe the output on the Serial Monitor.
COM6
Send
The password is incorrect, try again The password is correct, unlocking the door in 20 seconds
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

In this system, valid passwords are pre-programmed into the ESP32 code. An input string is used to store the password entered by users. The keypad has two special keys, * and #, that are used for clearing and terminating the input password. The system operates as follows:

  • When a key other than the special keys is pressed, it is added to the input string.
  • When the * key is pressed, the input string is cleared and the password input process can be restarted.
  • When the # key is pressed:
    • The input string is compared to the pre-defined passwords. If it matches one of the pre-defined passwords, the relay is activated to unlock the door.
    • Regardless of whether the password is correct or not, the input string is cleared for the next input.

    ※ NOTE THAT:

    • In the above code, To make it simple, we used the delay function. It is better to use millis() instead of delay(). See How to use millis() instead of delay()
    • You can add a piezo buzzer to make the beep sound each time keypad is pressed.
    • In the above codes, the door is locked again after 20 seconds. You can replace it by a door sensor. The door is locked when the door sensor detect the door is closed by user.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES