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
Keypad
2.png
Figure 1: Keypad
Servo
1.png
Figure 2: Servo
Connection From Keypad To Arduino
Connection From Servo To Arduino
Connection from LED to Arduino
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
Output
3.png
Figure 3: Output
Read more articles on Arduino: