Skip to main content
  1. Portfolios/

Building an elecronic, keyless door lock

·4 mins· loading · loading · ·
Ch Virinchi
Author
Ch Virinchi
Elixir programmer, rocket propulsion enthusiast. Web Application developer.
Table of Contents

What is Arduino?

Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board.

Step 1: Get all your components ready

Step 2: Start wiring according to the cheatsheet

Using the given cheatsheet, wire up all the connections.

Step 3: Uploading the code

Upload the following code into the board using the Arduino IDE tool. You may also need to install a few libraries.

#include <Keypad.h>
#include<LiquidCrystal.h>
#include<EEPROM.h>
#include <Servo.h>


int pos = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 9, d7 = 8;
LiquidCrystal liquid_crystal_display(rs, en, d4, d5, d6, d7);
char password[4];
char initial_password[4],new_password[4];
int i=0;
char key_pressed=0;
const byte rows = 4;
const byte columns = 4;
char hexaKeys[rows][columns] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte row_pins[rows] = {2,3,A5,A4};  
byte column_pins[columns] = {A3,A2,A1,A0};
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
Servo Latch;
void setup()
{
  Latch.attach(1);
  liquid_crystal_display.begin(16,2);
  liquid_crystal_display.print("Welcome back sir");
  liquid_crystal_display.setCursor(0,1);
  liquid_crystal_display.print("How are you?");
  delay(10000);
  liquid_crystal_display.clear();
  liquid_crystal_display.print("Pass to go insid");
  liquid_crystal_display.setCursor(0,1);
  initialpassword();
}
void loop()
{
  key_pressed = keypad_key.getKey();
  if(key_pressed=='#')
    change();
  if (key_pressed)
  {
    password[i++]=key_pressed;
    liquid_crystal_display.print(key_pressed);
      }
  if(i==4)
  {
    delay(200);
    for(int j=0;j<4;j++)
      initial_password[j]=EEPROM.read(j);
    if(!(strncmp(password, initial_password,4)))
    {
      liquid_crystal_display.clear();
      liquid_crystal_display.print("You can enter!");
      Latch.write(90);
      delay(10000);
      Latch.write(0);
      liquid_crystal_display.setCursor(0,1);
      liquid_crystal_display.print("Pres # to change");
      delay(2000);
      liquid_crystal_display.clear();
      liquid_crystal_display.print("Pass to go insid");
      liquid_crystal_display.setCursor(0,1);
      i=0;
    }
      else
    {
      Latch.write(0);
      liquid_crystal_display.clear();
      liquid_crystal_display.print("Wrong Password");
      liquid_crystal_display.setCursor(0,1);
      liquid_crystal_display.print("Pres # to Change");
      delay(2000);
      liquid_crystal_display.clear();
      liquid_crystal_display.print("Pass to go insid");
      liquid_crystal_display.setCursor(0,1);
      i=0;
    }
  }
}
void change()
{
  int j=0;
  liquid_crystal_display.clear();
  liquid_crystal_display.print("Current Password");
  liquid_crystal_display.setCursor(0,1);
  while(j<4)
  {
    char key=keypad_key.getKey();
    if(key)
    {
      new_password[j++]=key;
      liquid_crystal_display.print(key);
    }
    key=0;
  }
  delay(500);
  if((strncmp(new_password, initial_password, 4)))
  {
    liquid_crystal_display.clear();
    liquid_crystal_display.print("Wrong Password");
    liquid_crystal_display.setCursor(0,1);
    liquid_crystal_display.print("Try Again");
    delay(1000);
  }
  else
  {
    j=0;
    liquid_crystal_display.clear();
    liquid_crystal_display.print("New Password:");
    liquid_crystal_display.setCursor(0,1);
    while(j<4)
    {
      char key=keypad_key.getKey();
      if(key)
      {
        initial_password[j]=key;
        liquid_crystal_display.print(key);
        EEPROM.write(j,key);
        j++;
      }
    }
    liquid_crystal_display.print("Password Changed");
    delay(1000);
  }
  liquid_crystal_display.clear();
  liquid_crystal_display.print("Pass to go insid");
  liquid_crystal_display.setCursor(0,1);
  key_pressed=0;
}
void initialpassword(){
    EEPROM.write(0, '3');
    EEPROM.write(1, '3');
    EEPROM.write(2, '3');
    EEPROM.write(3, '3');
  for(int j=0;j<4;j++)
    initial_password[j]=EEPROM.read(j);
}

Code Explanation

First of all, we will include the libraries for the 4X4 keypad, LCD, and a library for storing the password. The EEPROM library is used to store the password.

#include <Keypad.h>
#include<LiquidCrystal.h>
#include<EEPROM.h>

The default password stored in it is ‘1234’. But, I’ve changed it to ‘3333’ in the above code. You can change it manually in the code by changing the following-

void initialpassword(){
    EEPROM.write(0, '3');
    EEPROM.write(1, '3');
    EEPROM.write(2, '3');
    EEPROM.write(3, '3');
  for(int j=0;j<4;j++)
    initial_password[j]=EEPROM.read(j);
}

In the loop function, the key pressed will be stored in the ‘key_pressed’ variable and it will be compared with the other keys. If the pressed key is ‘#’, then the change function will be called and it will ask for the new password.

key_pressed = keypad_key.getKey();
  if(key_pressed=='#')
    change();

The keys pressed are stored in the ‘password’ variable and these are shown on the LCD when the keys are pressed. Then these keys will be matched with the initial password stored in the EEPROM. If the keys that were pressed match the initial password, then the lock will open and “Pass accepted” will be printed on the LCD. If the password does not match, then it will ask you to enter a password again.

if (key_pressed)
  {
    password[i++]=key_pressed;
    liquid_crystal_display.print(key_pressed);

      }
if(i==4)
  {
    delay(200);
    for(int j=0;j<4;j++)
      initial_password[j]=EEPROM.read(j);
    if(!(strncmp(password, initial_password,4)))
    {
      liquid_crystal_display.clear();
      liquid_crystal_display.print("Pass Accepted");
      digitalWrite(relay_pin, LOW);
      delay(2000);

If the ‘#’ key is pressed, then it will call the ‘change()’ function. In the change function, it will ask you to enter the current password. If the current password is correct, then it will ask you to enter the new password. Upon entering the new password, the password will be changed.

void change()

{
  int j=0;
  liquid_crystal_display.clear();
  liquid_crystal_display.print("Current Password");
  liquid_crystal_display.setCursor(0,1);
  while(j<4)
  {
    char key=keypad_key.getKey();
    if(key)
    {
      new_password[j++]=key;
      liquid_crystal_display.print(key);
    }
    key=0;
  }
  delay(500);

Step 4: Check for errors.

After having uploaded the code, your circuit may be malfunctioning. Following could be the reasons-

  • Wires have shorted out and have lost continuity. Check wires for continuity using a multimeter.
  • Loose connections may have risen due to which there is no continuity. Ensure all connections are secure

Step 5: Attach the servo to the latch

Attach the servo to the latch and choose a good password! Remember, whenever you press the reset button on the board, the password is reset to ‘3333’.

Good Luck!

Related

Building Jarvis using Python to help people
·8 mins· loading · loading
Personal voice assistant using Python This assistant can open applications, search the web, Youtube, Wikipedia, and search the entire Wolfram Alpha Database for all of your questions.
Solar Filter
·2 mins· loading · loading
What is a Solar Filter? Solar Filter is a telescope accessory used by astronomers to enhance the details of the Sun. If viewed without a Solar Filter, the heat from the Sun is enough to fry the eyeballs right in our sockets.
The Periodic Table
·1 min· loading · loading
The Periodic Table I built using Bulma CSS The Periodic Table is one of the most significant achievements in Science. Dmitri Ivanovich Mandeleev started working on the Periodic Table in 1869.