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. password = Password("0000"); //password to unlock, can be changed
  5. const byte ROWS = 4;
  6. // Four rows const byte COLS = 4;
  7. // columns
  8. // Define the Keymap
  9. char keys[ROWS][COLS] =
  10. {
  11. {
  12. '1',
  13. '2',
  14. '3'
  15. },
  16. {
  17. '4',
  18. '5',
  19. '6'
  20. },
  21. {
  22. '7',
  23. '8',
  24. '9'
  25. },
  26. {
  27. '*',
  28. '0',
  29. '#'
  30. }
  31. };
  32. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. byte
  33. rowPins[ROWS] =
  34. {
  35. 9,
  36. 8,
  37. 7,
  38. 6
  39. }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins. byte
  40. colPins[COLS] = {
  41. 5,
  42. 4,
  43. 3
  44. };
  45. // Create the Keypad
  46. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  47. void setup()
  48. {
  49. Serial.begin(9600);
  50. Serial.write(254);
  51. Serial.write(0x01);
  52. delay(200);
  53. pinMode(11, OUTPUT); //green light
  54. pinMode(12, OUTPUT); //red light
  55. myservo.attach(13); //servo on digital pin 9
  56. }
  57. void loop()
  58. {
  59. keypad.getKey();
  60. myservo.write(0);
  61. }
  62. //take care of some special events void keypadEvent(KeypadEvent eKey)
  63. {
  64. switch (keypad.getState())
  65. {
  66. case PRESSED:
  67. Serial.print("Enter:");
  68. Serial.println(eKey);
  69. delay(10);
  70. Serial.write(254);
  71. switch (eKey)
  72. {
  73. case '*':
  74. checkPassword();
  75. delay(1);
  76. break;
  77. case '#':
  78. password.reset();
  79. delay(1);
  80. break;
  81. default:
  82. password.append(eKey);
  83. delay(1);
  84. }
  85. }
  86. }
  87. void checkPassword()
  88. {
  89. if (password.evaluate())
  90. {
  91. //if password is right open Serial.println("Accepted");
  92. Serial.write(254);
  93. delay(10); //Add code to run if it works myservo.write(150); //deg
  94. digitalWrite(11, HIGH); //turn on delay(5000);
  95. //wait 5 seconds
  96. digitalWrite(11, LOW); // turn off
  97. }
  98. else
  99. {
  100. Serial.println("Denied"); //if passwords wrong keep locked Serial.write(254);
  101. delay(10); //add code to run if it did not work myservo.write(0);
  102. digitalWrite(12, HIGH); //turn on delay(500);
  103. //wait 5 seconds
  104. digitalWrite(12, LOW); //turn off
  105. }
  106. }
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: