Often times, we need to secure a room at our home or office (perhaps a secret dexter’s laboratory) so that no one can access the room without our permission and ensure protection against theft or loss of our important accessories and assets. There are so many types of security systems present today but behind the scene, for authentication they all relay on fingerprint, retina scanner, iris scanner, face id, tongue scanner, RFID reader, password, pin, patterns, etc. Off all the solutions the low-cost one is to use a password or pin-based system. So, in this project, I have built an Arduino Keypad Door Lock which can be mounted to any of your existing doors to secure them with a digital password.
Before building our password door locking project, first, we need to collect the required components and then go ahead and follow the step by step building process.
List of components
- Arduino Uno/Pro/Mini or Custom board using Atmega 328p Microcontroller
- 16 x 2 LCD (Liquid Crystal Display)
- 4 x 3 or 4 x 4 matrix keypad for Arduino
- Servo motor
- 3D printed door locker/customized door locker
- Additional components for power supply of 1 Amp 5 Volt mobile charger
- 4’’ / 6’’plastics boxes, jumper wires, nuts bolts, plastic casing, etc.
First of all, we start from the brain of this project which is the Arduino UNO board. The Arduino board is connected to an LCD and a servo motor. The servo motor is used to push (lock) or pull (unlock) the latch on the door.
A 16 x 2 LCD is required to display the message by Arduino, 16 x 2 means it has 16 number of columns and 2 number of rows. If you are completely new to 16x2 LCD Display Modules, you can check this Arduino LCD Interfacing tutorial to learn more about it.
Keypad | Arduino |
Pin 1 (row 1) | Digital Pin1 |
Pin 2 (row 2) | Digital Pin 2 |
Pin 3 (row 3) | Digital pin 3 |
Pin 4 (row 4) | Digital pin 4 |
Pin 5 (columns 5) | Digital pin 5 |
Pin 6 (columns 6) | Digital pin 6 |
Pin 7 (columns 7) | Digital pin 7 |
Arduino Code for Digital Keypad Door Lock
The complete Arduino Door Lock Code can be found at the bottom of this page. You can directly upload the code, but it is recommended to read the below paragraphs to understand how the code works. Also, it is important to make sure you have added the following keypad library to your Arduino IDE to compile the code successfully. To do that just 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 that you just downloaded.
After inserting all the header and library files, assign all the pin for LCD and define the password length and set the initial position of the servo to 0. After that, take a “char” datatype for declaring the number that can hold it including the null character.
//#include <Keypad.h> #include <LiquidCrystal.h> #include <Servo.h> Servo myservo; LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); #define Password_Lenght 7 // Give enough room for six chars + NULL char int pos = 0; // variable to store the servo position char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7 char Master[Password_Lenght] = "123456";With this piece of code (char Master[Password_Lenght] = "123456";) -- under the Char Master, I declare the password of the door lock, then assign the number of rows and columns in the keyboard and also declare keyMaps and connect with rows and columns. Under the void setup, initialize the servo signal pin D9, servo status closed and print the name of the project/device/company with 3 seconds of delay on LCD time of starting the device.
void setup() { myservo.attach(9); ServoClose(); lcd.begin(16, 2); lcd.print(" Arduino Door"); lcd.setCursor(0, 1); lcd.print("--Look project--"); delay(3000); lcd.clear(); }
void ServoOpen() { for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } void ServoClose() { for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
Write the position of the servo with 15 seconds of delay to reach the servo position. Under the void open function, print on LCD “Enter Password” then the condition specifies that the entered password should be same with custom key, within this body data counted and stores char into the data array, incremented and input password printed on LCD if the data length (number of key) and input password matches with Data Master (where the pre-defined keys are present). Then as an action LCD clear, servo drive, print on LCD “Door is open” and the data counter is reset to 0.
If the input key does not match with Data Master, as an action on LCD clear print on LCD “Wrong Password “ to notify with 1 second of delay and remains in its lock position with data counter sets to 1 and continue this process in a loop.
if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master { if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0) { lcd.clear(); ServoOpen(); lcd.print(" Door is Open"); door = 0; } else { lcd.clear(); lcd.print(" Wrong Password"); delay(1000); door = 1; } clearData(); }
Arduino Keypad DoorLock Assembling and Testing
Now, fit everything on a 4 inch/ 6-inch plastic box and power it with a mobile charger, line up everything nicely using casing. Ideally, I could have used 3D printing to assemble my lock, but after designing my files, I found that 3D printing is very costly, so I just fixed the servo first, then connected the normal slide locker with my servo using metal plate and covered it up with glass fiber, although it is working fine, it is less secure.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo myservo;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
#define Password_Lenght 7 // Give enough room for six chars + NULL char
int pos = 0; // variable to store the servo position
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "123456";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
bool door = true;
byte rowPins[ROWS] = {1, 2, 3, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7}; //connect to the column pinouts of the keypad
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
void setup()
{
myservo.attach(9);
ServoClose();
lcd.begin(16, 2);
lcd.print(" Arduino Door");
lcd.setCursor(0, 1);
lcd.print("--Look project--");
delay(3000);
lcd.clear();
}
void loop()
{
if (door == 0)
{
customKey = customKeypad.getKey();
if (customKey == '#')
{
lcd.clear();
ServoClose();
lcd.print(" Door is close");
delay(3000);
door = 1;
}
}
else Open();
}
void clearData()
{
while (data_count != 0)
{ // This can be used for any array size,
Data[data_count--] = 0; //clear array for new data
}
return;
}
void ServoOpen()
{
for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void ServoClose()
{
for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void Open()
{
lcd.setCursor(0, 0);
lcd.print(" Enter Password");
customKey = customKeypad.getKey();
if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
{
Data[data_count] = customKey; // store char into data array
lcd.setCursor(data_count, 1); // move cursor to show each new char
lcd.print(Data[data_count]); // print char at said cursor
data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
}
if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
{
if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)
{
lcd.clear();
ServoOpen();
lcd.print(" Door is Open");
door = 0;
}
lcd.clear();
lcd.print(" Wrong Password");
delay(1000);
door = 1;
}
clearData();
}
else
{}
Comments
Post a Comment