Door Lock By Keypad Using Arduino

Introduction

 
In this article, I have explained about locking the door by keypad using Arduino. Do not worry, it's easy to implement. If we have this setup then it can be used for home security. It can be used to open and close the door by password through the keypad.
 
Parts Of List
  • Arduino
  • 4*4 Matrix Keypad
  • Servo
  • 2k ohm Resistor
  • Red Led
  • Green Led
  • Hookup wires.
Keypad
 
2.png
 
Figure 1: Keypad
  • It is a miniature keyboard.
  • This is the keypad of the 3*4 matrix.
  • It consists of buttons for operating a portable electronic device, telephone, or other equipment.
  • The keypad consists of numeric numbers for the door password.
Servo
 
1.png
 
Figure 2: Servo
  • The servomotor is a rotary actuator or linear actuator that allows precise control of the angular or linear position.
  • It can consist of a suitable motor coupled to a sensor for feedback.
Connection From Keypad To Arduino
  • The connection from the * is to the digital pin 09,08 to the Arduino board.
  • The connection from the 0 is to the digital pin 07,06 to the Arduino board.
  • The connection from the # is to the digital pin 05,04 to the Arduino board.
  • The connection from the D is to the digital pin 03,02 to the Arduino board.
Connection From Servo To Arduino
  • The first pin can be connected to the digital pin 13 to the Arduino board.
  • The center pin can be connected to the Vcc of 5v to the Arduino board.
  • The third pin can be connected to the Gnd to the Arduino board.
Connection from LED to Arduino
  • The red led can be connected to the positive pin of digital pin 12 to the Arduino board and negative pin to the Gnd.
  • The green led can be connected to the positive pin of digital pin 11 to the Arduino board and negative pin to the Gnd.
Instruction
 
Before starting add the library of the following:
Programming
  1. #include < password.h >     
  2. #include < keypad.h >     
  3. #include < servo.h >     
  4.     
  5. password = Password("0000"); //password to unlock, can be changed       
  6. const byte ROWS = 4;    
  7. // Four rows const byte COLS = 4;       
  8. // columns       
  9. // Define the Keymap       
  10. char keys[ROWS][COLS] =     
  11. {    
  12.     {    
  13.         '1',    
  14.         '2',    
  15.         '3'    
  16.     },    
  17.     {    
  18.         '4',    
  19.         '5',    
  20.         '6'    
  21.     },    
  22.     {    
  23.         '7',    
  24.         '8',    
  25.         '9'    
  26.     },    
  27.     {    
  28.         '*',    
  29.         '0',    
  30.         '#'    
  31.     }    
  32. };     
  33. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. byte      
  34.     
  35. rowPins[ROWS] =     
  36. {    
  37.     9,    
  38.     8,    
  39.     7,    
  40.     6    
  41. }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins. byte       
  42. colPins[COLS] = {    
  43.     5,    
  44.     4,    
  45.     3    
  46. };    
  47. // Create the Keypad       
  48. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);    
  49. void setup()    
  50. {    
  51.     Serial.begin(9600);    
  52.     Serial.write(254);    
  53.     Serial.write(0x01);    
  54.     delay(200);    
  55.     pinMode(11, OUTPUT); //green light       
  56.     pinMode(12, OUTPUT); //red light       
  57.     myservo.attach(13); //servo on digital pin 9       
  58. }    
  59. void loop()    
  60. {    
  61.     keypad.getKey();    
  62.     myservo.write(0);    
  63. }     
  64. //take care of some special events void keypadEvent(KeypadEvent eKey)      
  65. {    
  66.     switch (keypad.getState())    
  67.     {    
  68.         case PRESSED:    
  69.         Serial.print("Enter:");    
  70.         Serial.println(eKey);    
  71.         delay(10);    
  72.         Serial.write(254);    
  73.         switch (eKey)    
  74.         {    
  75.             case '*':    
  76.                 checkPassword();    
  77.                 delay(1);    
  78.                 break;    
  79.             case '#':    
  80.                 password.reset();    
  81.                 delay(1);    
  82.                 break;    
  83.             default:    
  84.                 password.append(eKey);    
  85.                 delay(1);    
  86.         }    
  87.     }    
  88. }    
  89. void checkPassword()    
  90. {    
  91.     if (password.evaluate())    
  92.     {    
  93.         //if password is right open Serial.println("Accepted");       
  94.         Serial.write(254);    
  95.         delay(10); //Add code to run if it works myservo.write(150); //deg       
  96.         digitalWrite(11, HIGH); //turn on delay(5000);       
  97.         //wait 5 seconds       
  98.         digitalWrite(11, LOW); // turn off       
  99.     }    
  100.     else    
  101.     {    
  102.         Serial.println("Denied"); //if passwords wrong keep locked Serial.write(254);      
  103.         delay(10); //add code to run if it did not work myservo.write(0);       
  104.         digitalWrite(12, HIGH); //turn on delay(500);       
  105.         //wait 5 seconds       
  106.         digitalWrite(12, LOW); //turn off       
  107.     }    
  108. }   
Explanation
  • This implementation can be used for home security by door locking using servo meter.
  • The programming will work in a way when the password is correct then the green light will turn on and the door opens.
  • When the password is wrong it can't open the door and the red led turn on.
  • You can also set the delay time for 5 seconds.
Output
 
3.png
 
Figure 3: Output
 
Read more articles on Arduino: