Many times we need to set up a room in our home or office (possibly a secret dexter lab) so that no one can enter the room without our permission and to ensure that our important accessories and assets are prevented from being stolen or lost. There are many types of security systems out there today, but behind the scenes, they all rely on fingerprints, retina scanners, iris scanners, facial IDs, tongue scanners, RFID readers, passwords, ciphers, patterns, etc. for authentication. A low-cost solution that closes all is to use a password or password-based system. So, in this project, I've built an Arduino keypad door lock that can be installed on any of your existing doors to protect them with a digital password.

Before building our combination door lock project, first, we need to gather the required components and then proceed with the step-by-step build process.

component list

Arduino Uno/Pro/Mini or custom board using an Atmega 328p microcontroller

16 x 2 LCD (Liquid Crystal Display)

4 x 3 or 4 x 4 matrix keyboard for Arduino

servo motor

3D printed door locker/custom door locker

Add-on for 1 amp 5 volt mobile charger power supply

4'' / 6'' plastic box, jumper wires, nuts and bolts, plastic case, etc.​

Arduino keyboard door lock circuit diagram

The complete circuit diagram of our Arduino based digital keypad door lock project is shown below.

First, we start with the brain of this project, the Arduino UNO board. The Arduino board is connected to the LCD and servo motors. Servo motors are used to push (lock) or pull (unlock) the latch on the door. The Arduino needs a 16 x 2 LCD to display messages, 16 x 2 means it has 16 columns and 2 rows.

Here I am using a 5v Towerpro SG90 servo motor to make our custom door lock. It is a basic level servo motor that can be used with Arduino without any driver circuit or external modules. Also, the cost of this servo motor is very low, so you can easily buy it.

In this project, I used a 4 x 4 matrix keyboard (but the 4 x 4 keyboard part cannot be used in fritzing to make this graphical representation), but don't worry, because a 4 x 3 matrix keyboard also works for my coding. We need a keypad for entering a passcode and manually locking our custom door lock. It consists of 16 keys (soft switches), 4 keys in row (R1, R2, R3, R4) and 4 keys in column (C1, C2, C3, C4) When a key is pressed, it is in the corresponding Create connections between rows and columns. The table below shows how to connect your Arduino to the keyboard.

Arduino Code for Numpad Door Lock

The complete Arduino door lock code can be found at the bottom of this page. You can upload the code directly, but it is recommended to read the following paragraphs to understand how the code works. Also, it is important to make sure that you have added the following keyboard libraries to your Arduino IDE to successfully compile the code. To do so, simply open the link below and download the ZIP file. Then on your Arduino IDE navigate to Sketch -> Include Library -> Add.ZIP Library and browse for the file you just downloaded.

After inserting all header and library files, assign all pins for LCD and define password length and set initial position of servo to 0. After that, take the "char" data type to declare the numbers that can hold it, including the null character.

//#include#include#include servo myservo; lcd (A0, A1, A2, A3, A4, A5); #define Password_Lenght 7 // provide enough space for six characters + NULL character integer position = 0; // variable to store servo position character data[password_lenght]; // 6 is the number of characters it can hold + null characters = 7char Master[Password_Lenght] = "123456"; servo.h>

With this code (char Master[Password_Lenght] = "123456";) — under Char Master, I declare the password for the door lock, then assign the number of rows and columns of the keyboard, also declare keyMaps and connect with the rows and columns . Under void setup, initialize the servo signal pin D9, turn off the servo status, and print the project/device/company name with a delay of 3 seconds when the LCD is turned on.

void set() { myservo.attach(9); servo off(); lcd.begin(16, 2); lcd.print("Arduino gate"); lcd.setCursor(0, 1); lcd.print(" --see item--"); delay(3000); lcd.clear();}

Below the loop function, there is a simple if-else condition. According to the status (automatic locking), print "Door is closed" after a delay of 3 seconds, the servo rotates to the closed position, the door data counts as 1, otherwise the door lock remains open, the data counts as 0, and the servo is turned on and rotates to 0 degrees to 180 degrees, turn it off from 180 to 0. The servo on and servo off functions are shown below.

void servo on() { for (pos = 180; pos >= 0; pos -= 5) { // go from 0 degrees to 180 degrees // in 1 degree units myservo.write(pos); // tell the servo servo goes to position delay(15) in variable 'pos'; // wait 15ms for servo to reach position }} void servo off() { for (pos = 0; pos <= 180;="" pos="" +="5)" {="" from ="" 180="" degrees to ="" 0="" degrees="" myservo.write(pos);="" tells the server to go to variable="" " position in 'pos'=""="" delay(15); //="" wait="" 15ms="" let servos reach position="" }="">=>

Write the servo position with a delay of 15 seconds to reach the servo position. Under the void open function, print "enter password" on the LCD, then the condition specifies that the entered password should be the same as the custom key, in this body the data counts and stores the char into the data array, if the data length (key number) and enter a password that matches the data master (predefined key exists). Then as an action, the LCD is cleared, the servo drives, "Door is open" is printed on the LCD, and the data counter is cleared.

If the entered key does not match the Data Master, as an action on the LCD, clear print "Wrong Password" on the LCD to delay 1 second notification and remain in its locked position, the data counter is set to 1, and this continues in a loop process.

if (data_count == Password_Lenght - 1) // if array index equals expected number of characters, compare data to master { if (!strcmp(Data, Master)) // equals (strcmp(Data, Master) == 0 ) { lcd.clear(); servo on(); lcd.print("Door is open"); door = 0; } else { lcd.clear(); lcd.print("Wrong password"); delay( 1000); gate = 1; } clearData(); }

Arduino Keypad Door Lock Assembly and Test

Now pack everything in a 4"/6" plastic case and power it with a mobile charger, lining everything up nicely with the case. Ideally I could 3D print to assemble my lock, but after designing my files, I found 3D printing to be very expensive, so I just fixed the servos first and then used a metal plate to attach the normal sliding lock to the My servo is attached and covering it is made of fiberglass, and while it works fine, it's not very secure.

If you want more security, then you have to print a 3D model of the door lock that will work with this servo system inside. If you have access to a 3D printer, you can download the required STL file from the link below and 3D print it.

Design files are also shown in the image below.

At the beginning of the project, the moment we start it for the first time, we need to give the project a name (you can also show the company name) so that it looks smart and unique like a commercial device (as you are in the image below).

After a delay of 3 seconds, lock the door quickly, and directly check the door lock status on the display, as shown in the figure below.

When you manually lock the door by pressing the "#" key, it will first display that the door is closed for 1 second, and then display enter the password. On the other hand, when the correct password is entered to unlock the door, Door is Open is displayed.

It will show if the door remains unlocked – as long as you manually lock the door according to the code, the door will open. I have given the code, you can customize the settings as needed by changing the code display parameters.



Servo myservo;

Liquid crystal liquid crystal (A0, A1, A2, A3, A4, A5);

#define Password_Lenght 7 // provide enough space for six characters + NULL char

int pos = 0; // variable to store servo position

char Data[Password_Lenght]; // 6 is the number of characters it can hold + null char = 7

char Master[Password_Lenght] = “123456”;

bytes data_count = 0, master_count = 0;

boolean Pass_is_good;

Character custom key;

const byte ROWS = 4;

constant bytes COLS = 3;

character keys[ROWS][COLS] = {

{‘1’, ‘2’, ‘3’},

{‘4’, ‘5’, ‘6’},

{‘7’, ‘

boolean = true;

bytes rowPins[ROWS] = {1, 2, 3, 4}; // connect to keyboard

bytes colPins[COLS] = row pins of {5, 6, 7}; // column pins connected to keyboard

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //Initialize an instance of class NewKeypad

void setup()




lcd.begin(16, 2);

lcd.print(“Arduino 门”);

lcd.setCursor(0, 1);

lcd.print("–Look at the project–");




void loop()


if (door == 0)


customKey = customKeypad.getKey();

if (customKey == ‘#’)




lcd.print("Door is closed");


gate = 1;



else open();


void clearData()


while (data_count != 0)

{ // This can be used for any array size,

Data[data_count–] = 0; //Clear the array of new data




void ServoOpen()


for (pos = 180; pos >= 0; pos -= 5) { // from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell the server to go to the position in the variable 'pos'

delay(15); // wait 15ms for the servo to reach the position



void ServoClose()


for (pos = 0; pos <= 180;=”” pos=”” +=”5)” {=”” 从=”” 180=”” 度转到=”” 0=””>=>

myservo.write(pos) ;

// tell the servo to reach the position in the variable 'pos' delay(15); // wait 15ms for the servo to reach the position



void Open()


lcd.setCursor(0, 0);

lcd.print(&quot;Enter password&quot;);

customKey = customKeypad.getKey();

if (customKey) // make sure a key is actually pressed, equal to (customKey != NO_KEY)


data[data_count] = customKey; // store characters into data array

lcd.setCursor(data_count, 1); // move the cursor to show each new character

lcd.print(Data[data_count]); // print character at cursor

data_count++; // Increment the data array by 1 to store new characters, while keeping track of the number of characters entered


if (data_count == Password_Lenght – 1) // if array index equals expected number of characters, compare data to master


if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)




lcd.print(&quot;Door is open&quot;);

gate = 0;



lcd.print(&quot;Password error&quot;);


gate = 1;


clear data();




Leave a Reply

Your email address will not be published.