Arduino Nano - Keypad Door Lock

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

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

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B 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) LCD I2C
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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, Electromagnetic Lock, and LCD display

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 Arduino Nano are similar. The Arduino Nano code for controlling them are the same.

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

Wiring Diagram

  • Wiring Diagram with Arduino Nano, keypad and solenoid lock
The wiring diagram between Arduino Nano and, keypad, solenoid lock

This image is created using Fritzing. Click to enlarge image

  • Wiring Diagram with Arduino Nano, keypad and electromagnetic lock
The wiring diagram between Arduino Nano and, keypad, electromagnetic lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Keypad Door Lock

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-door-lock */ #include <Keypad.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //four columns const int RELAY_PIN = 2; // The Arduino Nano pin connected to the IN pin of relay module const byte pin_rows[ROW_NUM] = {10, 9, 8, 7}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {6, 5, 4}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); const String password = "1234"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 32, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an 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) { Serial.println("The correct password! Unlocking the door in 20 seconds"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 20 seconds delay(20000); digitalWrite(RELAY_PIN, HIGH); // 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 } } }

※ NOTE THAT:

The address of the LCD may differ depending on the manufacturer. In our code, we used 0x27 as specified by DIYables.

Detailed Instructions

  • Connect a USB cable to the Arduino Nano and PC.
  • Open the Arduino IDE and select the appropriate board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “keypad”, then locate the keypad library created by Mark Stanley and Alexander Brevig.
  • Press the Install button to install the keypad library.
Arduino Nano keypad library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button to upload the code to the Arduino Nano.
  • Open the Serial Monitor.
  • Press the 12345 keys followed by the # symbol.
  • Then press the 1234 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 Arduino Nano code. A string is utilized to store the password entered by users, referred to as input string. On the keypad, two keys (* and #) are used for special purposes: to clear the password and to terminate the password. The system works as follows:

  • Except for the two special keys, if any other key is pressed, it is added to the input string.
  • If * is pressed, the input string is cleared. This can be used to start or restart the input of the password.
  • If # is pressed:
    • Arduino Nano compares the input string with the pre-defined passwords. If it matches one of the pre-defined passwords, Arduino Nano controls the relay to unlock the door.
    • Regardless of whether the password is correct or not, Arduino Nano clears the input string for the next input.

Arduino Nano Code - Multiple Keys

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-door-lock */ #include <Keypad.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //four columns const int RELAY_PIN = 2; // The Arduino Nano pin connected to the IN pin of relay module const byte pin_rows[ROW_NUM] = {10, 9, 8, 7}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {6, 5, 4}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); const String password_1 = "1234"; // change your password here const String password_2 = "5642"; // change your password here const String password_3 = "12345"; // 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 LCD Display to the RFID Door Lock

We can optionally add a LCD display to show the status (e.g. GRANTED/DENIED) to users.

Wiring diagram - Door lock system using keypad, solenoid lock or electromagnetic lock, and LCD display

The wiring diagram between Arduino Nano and door lock system lcd

This image is created using Fritzing. Click to enlarge image

Please note that, in the above wiring diagram, both relay and LCD may not works or work unstable due to insufficient power. We strongly advise to use an external power source for both the relay and LCD. Please refer to the following wiring diagram for guidance:

The wiring diagram between Arduino Nano and door lock system lcd external power supply

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Keypad Door Lock

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-door-lock */ #include <Keypad.h> #include <LiquidCrystal_I2C.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //four columns const int RELAY_PIN = 2; // The Arduino Nano pin connected to the IN pin of relay module const byte pin_rows[ROW_NUM] = {10, 9, 8, 7}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {6, 5, 4}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows const String password_1 = "1234"; // change your password here const String password_2 = "5642"; // change your password here const String password_3 = "12345"; // 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, HIGH); // lock the door lcd.init(); // Initialize the LCD I2C display 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("password is correct"); 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("password is incorrect, 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 address of the LCD can differ depending on the manufacturer. We used 0x27 in our code, as specified by DIYables.

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 Arduino Nano - Keypad Beep tutorial for more guidance.

Adding a door sensor to the keypad door lock

In the previously mentioned code, the Arduino Nano locks the door after a timeout since unlocking. However, in practical applications, a door sensor is usually added to the system. If the Arduino Nano 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 Arduino Nano - Door Sensor and Arduino Nano - 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 Arduino Nano 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 Arduino Nano 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 Arduino Nano detects the ADD master password, it switches between the ADD mode and OPERATION mode.
    • During the ADD mode, the Arduino Nano 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 Arduino Nano 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 Arduino Nano - Log Data with Timestamp to SD Card tutorial.

Video Tutorial