No. 4 – Keypad Lock

Task: We want to turn on a LED and turn a servo to a certain position, after entering the right three-digit code on the keypad. A latch can be attached to the servo, e.g. to unlock and lock a door.

Required equipment: Arduino / Breadboard / Keypad (4×4 in this example) / cables / one red LED / one green LED / two 100 Ohm resistors / servo

Only if we enter the right code on the keypad, we want the green LED to light up and turn the servo to a certain position (lock open). If the lock is closed the red LED should light up and the servo should turn to an other position. This tutorial can be suggestion for a bigger project, like a door lock or a safe for example.

Let’s start with the setup. The wiring in this tutorial isn’t that complicated and is shown in the fritzing sketch below.

Hint: You can find the numbers 1 and 7 at the outermost contacts of the keypad. The contact with the number 1 gets connected to pin 2 on the arduino. One after another the keypad gets contacted to the arduino, until the contact from the keypad, with the number 7 on it, is connected to pin 8 on the arduino.

Setup:

Keypadlock

To run the keypad with the arduino we need a library, which has to be added to the arduino software.

Open the arduino software > choose “Sketch” > “Include Library” > “Manage Libraries..” >
search after “keypad” with the search bar on the top > choose the first library by Mark Stanley and install the library.

From now on we can use the keypad library in our code.

Code:


#include <Keypad.h> //Include Keypad and servo library

#include <Servo.h>

Servo servoblue; //The servo is called „servoblue“ from now on

char* password = „123“; //We set the password. In this case „123“

int position = 0;

const byte ROWS = 4; //In this two lines we define how many rows and columns

const byte COLS = 3; //our keypad has

char keys[ROWS][COLS] = { //The characters on the keys are defined here

{‚#‘, ‚0‘, ‚*‘},

{‚9‘, ‚8‘, ‚7‘},

{‚6‘, ‚5‘, ‚4‘},

{‚3‘, ‚2‘, ‚1‘}

};

byte rowPins[ROWS] = {5, 6, 7, 8}; //The connection with the arduino is

byte colPins[COLS] = {2, 3, 4}; //listed here

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int redLED = 12; //The red LED is connected to pin 12

int greenLED = 13; //The green LED is connected to pin 13

void setup()

{

pinMode(redLED, OUTPUT); //The LEDs are defined as Output

pinMode(greenLED, OUTPUT);

servoblue.attach(11); //The servo is connected to pin 11

setLocked(true);

}

void loop()

{

char key = keypad.getKey();

if (key == ‚*‘ || key == ‚#‘If the lock is open it can be locked again by pushing „*“ or „#“ on the //keypad

position = 0;

setLocked(true); //The command to close the lock after „*“ or „#“ is pushed

}

if (key == password[position])

{

position ++;

}

if (position == 3) //This part defines how many digits our code will have.In this case we have 3 digits //(123).

{

setLocked(false);

}

delay(100);

}

void setLocked(int locked)

{

if (locked) // If the lock is closed..

{

digitalWrite(redLED, HIGH); //..the red LED should light up..

digitalWrite(greenLED, LOW); //..the green LED not..

servoblue.write(90); //and the servo should turn to a 90 degree position.

}

else //if the lock is open..

{

digitalWrite(redLED, LOW); //..the red LED should be off..

digitalWrite(greenLED, HIGH); //..and the green LED should light up..

servoblue.write(0); //..and the servo should turn to a 0 degree position.

}

}


This tutorial is an example of how easy you can create a simple lock with just a few arduino parts.