You, the evil genius wouldn't rest easy till you are sure that all of your creations are locked away somewhere safe, now would you? Well, this project is exactly about that! I'm gonna show you how to make your very own keypad security lock using an arduino board and a bit of cunning.
Parts Needed
- Arduino Uno or a clone
- Two 270 Ohm metal film resistors
- A Green 5mm LED
- A Red 5mm LED
- A 4X3 Matrix Keypad
Understanding the Concept
The key switches are placed at the intersection of row and column wires. When a key is pressed, it connects a specific row to a specific column. By arranging the keys in a grid like this, it means that we only need to use 7 (4 rows + 3 columns) of our digital pins rather than 12 (one for each key). However, it also means that we have to do a bit more work in the software to determine which keys are pressed. The basic approach we have to take is to connect each row to a digital output and each column to a digital input. We then put each output high in turn and see which inputs are high.
The Adjacent figure shows how you can connect 7 pins to the keypad. Now, we just need to find out which pin on the keypad corresponds to which row or column. If you are lucky, the keypad will come with a datasheet that will tell us this. If not, get some paper, draw a diagram of the keyboard connections, and label each pin with a letter from a to g. Set the multimeter to continuity so that it beeps when you connect the leads together. Then write a list of all the keys. After that, hold each key down in turn, and find the pair of pins that make the multimeter beep indicating a connection. If you are confused, each pin will have two keys that make the multimeter beep. Release the key to check that you have indeed found the correct pair. After a while you will be able to see how the pins relate to rows and columns. Note that conveniently the keypad has seven pins that will just fit directly into the Digital Pin 1 to 7 socket on the Arduino board, so we only need the breadboard for the two LEDs.
The Adjacent figure shows how you can connect 7 pins to the keypad. Now, we just need to find out which pin on the keypad corresponds to which row or column. If you are lucky, the keypad will come with a datasheet that will tell us this. If not, get some paper, draw a diagram of the keyboard connections, and label each pin with a letter from a to g. Set the multimeter to continuity so that it beeps when you connect the leads together. Then write a list of all the keys. After that, hold each key down in turn, and find the pair of pins that make the multimeter beep indicating a connection. If you are confused, each pin will have two keys that make the multimeter beep. Release the key to check that you have indeed found the correct pair. After a while you will be able to see how the pins relate to rows and columns. Note that conveniently the keypad has seven pins that will just fit directly into the Digital Pin 1 to 7 socket on the Arduino board, so we only need the breadboard for the two LEDs.
Software
You may think that we would just write a software which interprets which key is pressed by receiving signals from the pins and the game would be over, but this is not the case. The key doesn't simply go from being open to being closed when you press it, but opens or closes many times. This is called bouncing and we need to incorporate some mechanism which overcomes bouncing. The mechanism will be called debouncer.
No no, you need to fret! Because fortunately for us, there exists a library that handles this for us. This is the link to the library. To install it, just extract from the zip folder, and paste the extracted folder in C:\Program Files\Arduino\Arduino-0017\hardware\libraries or where ever you have installed the arduino IDE. Check that the library is installed by restarting the IDE, starting a new sketch, and choosing the menu option Sketch
Import Library | Keypad. This will insert the text “#include <Keypad.h>” on the top of the file. The code that I'm going to show you is just an example. You may have to change your int pins to agree with the layout of your keypad.
Here's the code:
#include <Keypad.h>
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 9;
int greenPin = 8;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLocked(true);
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4)
{
setLocked(false);
}
delay(500);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
The loop function checks for a key press. If the key pressed is # or *, it sets the position variable to 0. If, on the other hand, the key pressed is one of the numerals, it checks to see if it is the next key expected (secretCode[position]) is the key just pressed, and if it is, it increments position by one. Finally, the loop checks to see if position is 4, and if it is, it sets the LEDs to their unlocked state. Then it waits a second, and turns the red light green. Load the completed sketch for Project 10 from your Arduino Sketchbook and download it to the board.
If you have trouble getting this to work, it is most likely a problem with the pin layout on your keypad. So persevere with the multimeter to map out the pin connections.
And it goes without saying that if you have any problems, any at all, just mail me or comment.
OVER AND OUT.
0 comments:
Post a Comment
Do Comment on Anything you like or dont like!