Password based Door Locking System using Arduino

Ever wanted your own password based door locking system using Arduino? In this Arduino electronics tutorial we will show how you can make Arduino based door lock using 4x3 keypad, servo motor and LCD. It is very easy to make. You can use it not only for password protected door but also for password protected briefcase, cabinet etc. We will show you how it works using demonstration video and also explain and provide the codes.

Explanation & Demonstration

In this password based door lock system using arduino, the LCD will show "Enter Code:" message to the user. The user is prompted to enter the password. To enter the password, the user has to use * (astrick symbol on 4x3 keypad) to start entering the code. Then the user has to enter 4 digits(the number of digits can be programmed for longer digits via the software). Then the user has to enter #(pound symbol on 4x3 keypad) to validate the entered 4 digits password code.

Password based Door Locking System using Arduino

Now if the password is correct then the door will open(using the servo motor). If the password is not correct then the user is informed that the code is incorrect and asked to enter code again. The correct code in this example is 1234 so the user has to enter *1234# to open the door. In default state the door is open, to close the door the user has to enter *1111#. If the door is in open state, and the user enters the correct open door code, then the user is informed with message that the door is already open. Similarly if the door is already in closed state and the user enters the code to close the door, then the user is informed that the door is already closed. The state of the door, open or close is also indicated by a LED. If the LED is off then the door is open and if the LED is on then it indicates that the door is closed. The LED is used here to indicate the door state visually. 

 See the picture on the side that shows the Arduino based door lock using 3x4 keypad and servo motor and LCD.


See the demonstration video below so that you know what you can expect and what you will be building.


Hardware Aspects

Materials Required

What you will need for this project are as follows.

- Arduino
- 16x2 LCD
- Servo motor
- Door Lock
- 4x3 keypad
- LED
- 10Kohm potentiometer
- 1uF capacitor
- Connecting wires

Arduino Interfacing with Keypad, LCD, Servo Motor & LED

The schematic diagram is as shown below. We need to connect Arduino to 16x2 LCD, 4x3 Keypad, a LED and the Servo Motor.

schematic of Password based Door Locking System using Arduino

Here we have connected the Arduino pins A5 and A4 to LCD RS(Register Select) and E(Enable) pins respectively. The R/W pin of the LCD is grounded(that is for write mode). The Arduino pins 9 to 12 are connected to the LCD data pins D4 to D7 respectively. For the LCD contrast purpose, we have connected a 10Kohm POT to the contrast pin Vee of the LCD. The power supply for LCD comes from Arduino +5V and ground. A 1uF electrolytic capacitor is connected across the +5V and ground for smoothing the supply power for LCD.
 

The Arduino pins 2 through 8 are used for interfacing the 4 rows and 3 columns pins of the 4x3 keypad. Here Arduino pins 2, 3, 4 and 5 are connected to the keypad row pins 1, 2, 3 and 4 respectively. Similarly, the Arduino pins 6, 7 and 8 are connected to the 4x3 keypad columns pins 5,6 and 7 respectively.

The LED with 220Ohm resistor is connected to Arduino pin 13. The LED is used to indicate visually whether the Door is open or not.

The Servo motor signal pin(white) is connected to the Arduino A0 pin. The Servo motor power is supplied with separate +5V power supply. Using the same power power supply for Arduino and Servo Motor will cause the LCD to flicker. In fact, using same power supply to drive microcontroller(Arduino board) and any kind of motor whether it is Servo, Stepper or DC motor is not recommended because the motor coil can cause damage to the digital circuit.

Password Based Door Lock Arduino Code

Below is the code for arduino door lock using 3x4 keypad and servo motor.

#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(A5, A4, 9, 10, 11, 12);

Servo doorServo;

const byte rows = 4; // set display to four rows
const byte cols = 3; // set display to three columns

char keys[rows][cols] = {
                                    {'1', '2', '3'},
                                    {'4', '5', '6'},
                                    {'7', '8', '9'},
                                    {'*', '0', '#'}
                                    };
                       
byte rowPins[rows] = {8, 7, 6, 5};
byte colPins[cols] = {4, 3, 2};

char OPENCODE[4] = {'1', '2', '3', '4'};
char CLOSECODE[4] = {'0', '0', '0', '0'};
char attempt[4] = {0, 0, 0, 0};
//char store[4];

int z = 0;
bool doorstate = false; //open
const int ledPin = 13;
const int servoPin = A0;

void setup () {
    doorServo.attach(servoPin);
    lcd.begin(16,2);
    
    //setup Pins for Keypad
    pinMode(A4, OUTPUT);        //    RS pin
    pinMode(A5, OUTPUT);        // E pin

     for(byte r = 0; r < rows; r++){
           pinMode(rowPins[r], INPUT);        //set the row pins as input
           digitalWrite(rowPins[r], HIGH);        //turn on the pullups
     }
     
     for(byte c = 0; c < cols; c++){
           pinMode(colPins[c], OUTPUT);        //set the column pins as output
     }
    
    //setup Pins for Motor
    pinMode(servoPin, OUTPUT);
    
    //LED indicator
    pinMode(ledPin, OUTPUT);

    lcd.print("Enter Code: ");
}

void loop() {
     readKeypad();
}

void readKeypad(){
    
    char key = getKey();

    if(key){
        switch(key){
            case '*':
                z = 0;
                break;
            case '#':
                delay(100);
                checkCode();
                break;
            default:
                attempt[z] = key;
                z++;
                if(z == 4){
                    z = 0;
                }
        }
    }
}

char getKey(){
      char k = 0;
      
      for(byte c = 0; c < cols; c++){
        digitalWrite(colPins[c], LOW);
           for(byte r = 0; r < rows; r++){
            if(digitalRead(rowPins[r]) == LOW)
             {
            delay(20);        //20ms debounce time
            while(digitalRead(rowPins[r]) == LOW);
             k = keys[r][c];
             }
           }
     digitalWrite(colPins[c], HIGH); 
      }
      return k;
}


void checkCode(){
    int opencorrect = 0;
    int closecorrect = 0;

    for(int i = 0; i < 4; i++){
        if(attempt[i] == OPENCODE[i]){
            opencorrect++;
        }
        if(attempt[i] == CLOSECODE[i]){
            closecorrect++;
        }
    }

    if(opencorrect  == 4 && doorstate == 1){
        openDoor();
    }
    else if(opencorrect  == 4 && doorstate == 0){
            lcd.clear();
            lcd.print("Aready Open!");
            delay(2000);
            lcd.clear();
            lcd.print("Enter Code: ");
    }
    else if(closecorrect  == 4 && doorstate == 0){
            closeDoor();
    }
    else if(closecorrect  == 4 && doorstate == 1){
            lcd.clear();
            lcd.print("Aready Closed!");
            delay(2000);
            lcd.clear();
            lcd.print("Enter Code: ");
    }
    else{
        incorrectCode();    
        digitalWrite(ledPin, doorstate);
    }

    for(int a=0; a<4; a++){
        attempt[a] = 0;
    }
}

void openDoor(){
    lcd.clear();
    lcd.print("Door is opening");
    lcd.setCursor(0,1);
    lcd.print("Wait ....");
    doorServo.write(70);
    delay(1000);
    lcd.setCursor(0,0);
    lcd.print("Door is Open");
    delay(2000);
    lcd.clear();
    lcd.print("Enter Code: ");
    doorstate = 0;
    digitalWrite(ledPin, doorstate);
}

void closeDoor(){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Closing..");
    lcd.setCursor(0,1);
    lcd.print("Wait ...");
    doorServo.write(113);
    delay(1000);
    lcd.clear();
    lcd.print("Door is Closed");
    delay(2000);
    lcd.clear();
    lcd.print("Enter Code: ");
    doorstate = 1;
    digitalWrite(ledPin, doorstate);    
}


void incorrectCode(){
    lcd.clear();
    lcd.print("Incorrect Code");
    delay(2000);
    lcd.clear();
    lcd.print("Enter Code:");
}

Summary

In this Arduino electronics project, we have shown how to create a password based door locking system using Arduino. The interfacing has been explained along with picture and video demonstration of the arduino based door lock using 3x4 keypad and servo motor and LCD.  The keypad arduino code for password based door lock system using arduino has also been provided.

Post a Comment

Previous Post Next Post