ESP32 - Door Lock System using Password

This tutorial instructs you how to use ESP32, Keypad and electromagnetic lock to make door lock system. Users will be ask for password via keypad. It the password is valid, the ESP32 controls electromagnetic lock to unlock the door. The tutorials also optionally adds an LCD for more convenience. The ESP32 code aslo supports multiple passwords.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Keypad 4x4
1×Relay
1×12V Power Adapter
1×Electromagnetic Lock
n×Jumper Wires
1×LCD I2C (optional)
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 Electromagnetic Lock and LCD

We have specific tutorials about kypad, electromagnetic lock and LCD. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:

How check the password

  • A special key * is used to reset the password
  • A special key # is used to terminate the password input.
  • Every inputed key, except for * and # is appended in to a string.
  • When # key is pressed, the password input is complete. The ESP32 compares the inputed string with predefined passwords.
  • If the inputed password is vaild, unlock the door.

Wiring Diagram

wiring diagram between esp32 door lock system

This image is created using Fritzing. Click to enlarge image

ESP32 Code - Door lock system with password using keypad, electromagnetic 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-door-lock-system-using-password */ #include <Keypad.h> #define RELAY_PIN 14 // ESP32 pin GIOP14 connected to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte pin_rows[ROW_NUM] = {19, 18, 5, 17}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "0156CAB"; // change your password here const String password_2 = "5642CD"; // change your password here const String password_3 = "545423"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 32 pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, HIGH); // 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("Valid Password => unlock the door"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 20 seconds delay(20000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.println("Invalid Password => Try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • On Arduino IDE, Navigate to Tools Manage Libraries
  • ESP32 add library
  • Type “keypad” on the search box, then look for the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
  • ESP32 keypad library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • To simulate the electromagnet lock installed on the door, place the electromagnet near the armature plate.
  • Press 987657 keys and press #
  • Check the attraction force between electromagnet and armature plate.
  • Press 0156CAB keys and press #
  • Check the attraction force between electromagnet and armature plate.
  • See the result on Serial Monitor
  • COM6
    Send
    Invalid Password => Try again Valid Password => unlock the door
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

ESP32 Code - Door lock system with password using keypad, electromagnetic lock and LCD

The below code added LCD I2C to display the access status. For wiring with LCD, you can see in this tutorial Esp32 - LCD I2C tutorial

/* * 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-door-lock-system-using-password */ #include <LiquidCrystal_I2C.h> #include <Keypad.h> #define RELAY_PIN 14 // ESP32 pin GIOP14 connected to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte pin_rows[ROW_NUM] = {19, 18, 5, 17}; // GIOP19, GIOP18, GIOP5, GIOP17 connected to the row pins byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GIOP16, GIOP4, GIOP0, GIOP2 connected to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows const String password_1 = "0156CAB"; // change your password here const String password_2 = "5642CD"; // change your password here const String password_3 = "545423"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 32 pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, HIGH); lcd.init(); // initialize the lcd lcd.backlight(); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password lcd.clear(); } else if (key == '#') { lcd.clear(); if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("Valid Password => unlock the door"); lcd.setCursor(0, 0); lcd.print("CORRECT!"); lcd.setCursor(0, 1); lcd.print("DOOR UNLOCKED!"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 20 seconds delay(20000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.println("Invalid Password => Try again"); lcd.setCursor(0, 0); lcd.print("INCORRECT!"); lcd.setCursor(0, 1); lcd.print("ACCESS DENIED!"); } input_password = ""; // reset the input password } else { if (input_password.length() == 0) { lcd.clear(); } input_password += key; // append new character to input password string lcd.setCursor(input_password.length(), 0); // move cursor to new position lcd.print('*'); // print * key as hiden character } } }

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.

※ NOTICES