Arduino

Arduino Tutorial #20 Password lock on door using arduino

Password lock on door using arduino

In this tutorial we will see how to make door lock with password and when someone try wrong password buzzer will make sound.  So let’s get started. For this you will

  1. Arduino,
  2. Keypad,
  3. Servo motor,
  4. Buzzer,
  5. Breadboard (optional),
  6. Jumper wire,
  7. Steel wire to connect servomotor and sliding door lock.

 

Circuit diagram for password door lock using arduino
circuit diagram for password lock on door
circuit diagram for password lock on door

 

Do connection as shown in diagram.

Sketch for door lock password using arduino
#include <Servo.h>
#include <Keypad.h>

//Variables
int min_Ang = 0; 
int max_Ang = 135; 
int character = 0;
int activated =0;
char Pass_char[4] = {' ', ' ', ' ', ' '}; 
Servo myservo;
int buzzer=10;



//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
 {'1','4','7','*'},
 {'2','5','8','0'},
 {'3','6','9','#'},
 {'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
 myservo.attach(11); //attach the servo to pin 11
 pinMode(buzzer,OUTPUT); 
 myservo.write(min_Ang); 
 
}
 
void loop(){

char customKey = customKeypad.getKey(); //this function reads the presed key
 
 if (customKey){

if (character ==0)
 { 
 Pass_char[0]= customKey; 
 }

if (character ==1)
 { 
 Pass_char[1]= customKey; 
 }

if (character ==2)
 { 
 Pass_char[2]= customKey; 
 }

if (character ==3)
 { 
 Pass_char[3]= customKey; 
 activated=1;
 }
 
 character=character+1;
 }

if (activated == 1)
 {
// you can change your password here (current passowrd is 1357)

if(character==4 && Pass_char[0]=='1' && Pass_char[1]=='3' && Pass_char[2]=='5' && Pass_char[3]=='7' )
 {
 myservo.write(max_Ang);
 activated = 2;
 delay(1000);
 }
 else
 {

digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' ';
 }
 }
 if (activated == 2)
 {
 if(customKey == 'C' )
 {
 myservo.write(min_Ang);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' '; 
 }
 } 
}

 

Let’s come to the programming part. We are not using single wire connection for keypad that’s why in this sketch we are using keypad library. Which you can download by given link keypad library. After downloading unzip it. Copy that folder and paste it to directory of Arduino libraries. Now you are ready to go with keypad library.

Include keypad and servo library. Define integer minimum angle which is locked position at 0 degree angle.  Maximum angle is open position at 135 degree angle. You can change this angle according to your door open position. Initially character and activated is zero. Now we are defining character array which contain four element. Which is our password initially it is blank space. Create servo to myservo. Buzzer pin inserted at pin number 10. keypad matrix is taken from keypad library and you can adjust row & column pin number if it is not working correctly. In setup function Myservo is attached to pin number 11. Set pin mode of buzzer as an output. Myservo.write will rotate servo to zero degree angle. In loop function this function will read the pressed key. If any key is pressed it will check character is zero, if it is zero it will assign that pressed character to password character index number 0. Similarly for password character index number 1, 2, 3. It will increase character number on each keypress. When password character index number 3 is completed it will activate 1. If entered password is matched with exact number and sequence, my servo write will rotate the servo and open the door. In this case password is 1357. Now it will activate 2. If C character is pressed it will again lock the door and reset the activated and character equal to zero. It also reset entered password which is containing in form of array in password character. If password is not matched with our password, it will execute code written in else bracket. Buzzer will make sound. It will again reset all parameter activated and character is equal to zero.

You can see when I entered wrong password it makes sound and when I entered right password it rotate the servo 135 degree angle. It comes again to zero degree angle when I press C button.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2wjd7p4 (Keypad 4×4)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2weYm4R (servo motor)

KEEP LEARNING KEEP MAKING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *