Arduino Nano 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 Arduino Nano ESP32 controls electromagnetic lock to unlock the door. The tutorials also optionally adds an LCD for more convenience. The Arduino Nano ESP32 code aslo supports multiple passwords.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Keypad 3x4
1×Relay
1×Electromagnetic Lock
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×(Optional) LCD I2C
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of 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, Arduino Nano 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 Arduino Nano ESP32 compares the inputed string with predefined passwords.
  • If the inputed password is vaild, unlock the door.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and door lock system

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code - Door lock system with password using keypad, electromagnetic lock

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-door-lock-system-using-password */ #include <Keypad.h> #define RELAY_PIN D2 // The Arduino Nano ESP32 pin 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] = {D10, D9, D8, D7}; // The Arduino Nano ESP32 pin connected to the row pins byte pin_column[COLUMN_NUM] = {D6, D5, D4}; // The Arduino Nano ESP32 pin connected to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234"; // change your password here const String password_2 = "4444"; // 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 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 } } }

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.* Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type “keypad” on the search box, then look for the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
Arduino Nano ESP32 keypad library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano 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 1111 keys and press #
  • Check the attraction force between electromagnet and armature plate.
  • Press 1234 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  

Arduino Nano 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 Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-door-lock-system-using-password */ #include <LiquidCrystal_I2C.h> #include <Keypad.h> #define RELAY_PIN D2 // The Arduino Nano ESP32 pin 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] = {D10, D9, D8, D7}; // The Arduino Nano ESP32 pin connected to the row pins byte pin_column[COLUMN_NUM] = {D6, D5, D4}; // The Arduino Nano ESP32 pin connected to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234"; // change your password here const String password_2 = "4444"; // change your password here const String password_3 = "55555"; // change your password here String input_password; LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows 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 } } }

※ NOTE THAT:

The LCD I2C address can be different from each manufacturer. In the code, we used address of 0x27 that is specified by DIYables manufacturer

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!