ESP8266 - Keypad Door Lock

This tutorial instructs you how to build a password door lock system by using ESP8266, keypad, and solenoid lock or electromagnetic lock. The below is how the door lock system will work:

ESP8266 NodeMCU, keypad, solenoid lock

To make it easy, the tutorial is divided into multiple steps, from easy to difficult:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Keypad 3x4 and 4x4 Kit
1×Relay
1×Solenoid Lock
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Electromagnetic Lock
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Keypad, Solenoid Lock, and Electromagnetic Lock

Both the solenoid lock and electromagnetic lock are used to lock/unlock the door. They primarily differ from each other in term of machanical design. Their wiring to ESP8266 are similar. The ESP8266 code for controlling them are the same.

If you are unfamiliar with keypad, solenoid lock, electromagnetic lock (pinout, how it works, how to program ...), the following tutorials can help you learn:

Wiring Diagram

  • Wiring Diagram with ESP8266, keypad and solenoid lock
The wiring diagram between ESP8266 NodeMCU and, keypad, solenoid lock

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • Wiring Diagram with ESP8266, keypad and electromagnetic lock
The wiring diagram between ESP8266 NodeMCU and, keypad, electromagnetic lock

This image is created using Fritzing. Click to enlarge image

ESP8266 Code - Keypad Door Lock

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-keypad-door-lock */ #include <Keypad.h> #define RELAY_PIN D0 // The ESP8266 pin connected to the IN pin of relay #define ROW_NUM 4 // 4 rows #define COLUMN_NUM 3 // 3 columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {D1, D2, D3, D4}; // The ESP8266 pins connect to the row pins byte pin_column[COLUMN_NUM] = {D5, D6, D7}; // The ESP8266 pins connect to the column pins Keypad keypad = Keypad( makeKeymap(key_layout), 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 } } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect a USB cable between the ESP8266 and the PC.
  • Open the Arduino IDE, select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “keypad” and locate the keypad library created by Mark Stanley and Alexander Brevig.
  • Then, press the Install button to complete the installation of the keypad library.
ESP8266 NodeMCU keypad library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to send the code to the ESP8266.
  • Open the Serial Monitor.
  • Press the 12345 keys followed by the # symbol.
  • Then press the 1234ABC keys followed by the # symbol.
  • Check out the lock tongue's state for 20 seconds.
  • Check out the result on the Serial Monitor.
COM6
Send
The incorrect password! try again The correct password! Unlocking the door in 20 seconds
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

The valid passwords are pre-defined in the ESP8266 code. A string is used to store the input password from users, referred to as input string. In keypad, two keys (* and #) are utilized for special purposes: clear password and terminate password. The system operates as follows:

  • Apart from two special keys, if another key is pressed, it is added to the input string
  • If * is pressed, input string is cleared. You can use it to initiate or restart inputing the password
  • If # is pressed:
    • The ESP8266 compares input string with the pre-defined passwords. If it matches with one of the pre-defined passwords, the ESP8266 controls the relay to unlock the door.
    • Regardless of whether the password is correct or not, The ESP8266 clears the input string for the next input.

ESP8266 Code - Multiple Keys

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-keypad-door-lock */ #include <Keypad.h> #define RELAY_PIN D0 // The ESP8266 pin connected to the IN pin of relay #define ROW_NUM 4 // 4 rows #define COLUMN_NUM 3 // 3 columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {D1, D2, D3, D4}; // The ESP8266 pins connect to the row pins byte pin_column[COLUMN_NUM] = {D5, D6, D7}; // The ESP8266 pins connect to the column pins Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234ABC"; // change your password here const String password_2 = "5642CD"; // change your password here const String password_3 = "4545B"; // 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 correct password! 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 incorrect password! try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Adding the beep sound when the keypad is pressed.

We can make the door lock look responsive by adding a piezo buzzer to generate a short beep each time a key on keypad is pressed.

It is quite simple to add it. So, we're leaving this part up to your creativity. You can check out the ESP8266 - Keypad Beep tutorial for more guidance.

Adding a door sensor to the keypad door lock

In the previously mentioned code, the ESP8266 locks the door after a timeout since unlocking. However, in practical applications, a door sensor is usually added to the system. If the ESP8266 detects that the door is closed, it locks the door immediately instead of waiting for the timeout.

To avoid overwhelming you, we didn't include the door sensor in the wiring diagram and code. Instead, we're leaving this part up to your creativity. You can check out the ESP8266 - Door Sensor and ESP8266 - Door Sensor control Relay tutorials for more guidance.

Managing and storing the valid passwords to EEPROM

The above code has valid passwords (UID) that are hardcoded into the ESP8266 code. This means that if you want to add or remove passwords, you have to make changes to the code and upload it to the ESP8266 again, which is inconvenient.

In real-life applications, it's necessary to manage passwords without having to modify and upload the code every time. To achieve this, the passwords can be stored in EEPROM instead of being hardcoded. Consequently, a method is needed to easily manage the passwords from the EEPROM.

Two methods are available for managing passwords in EEPROM:

  • Use master passwords
    • By utilizing a password as an ADD master password, new passwords can be added to the system. Once the ESP8266 detects the ADD master password, it switches between the ADD mode and OPERATION mode.
    • During the ADD mode, the ESP8266 adds any new passwords it detects to the EEPROM.
    • A similar approach is used for DELETE master password and DELETE mode.
  • Use a ADD/DELETE commands via Serial/Bluetooth/IR
    • Commands are transmitted via Serial/Bluetooth/IR, utilizing tools such as the Serial Monitor, Bluetooth app, or IR controller.
    • The commands consist of a directive (ADD/DELETE) and the password.

    In order to utilize any of two above methods, a considerable amount of ESP8266 code must be added. For individuals who are new to programming, this can be a challenging task. As a result, this tutorial aims to provide a basic understanding of the door lock system without overwhelming beginners with complex code. If you wish to implement this system for practical purposes, please contact us through our paid programming service

Storing the access history to SD Card

To keep track of access history, it might be necessary to save access status (GRANTED/DENIED), and date and time. Since EEPROM has insufficient capacity to store the entire history, an SD card can be used instead. You can find guidance on this ESP8266 - Log Data with Timestamp to SD Card tutorial.

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!