~~ The complete kit for this tutorial is now for sale at www.razvantech.com   Get one! ~~

This instructable will show you how to make a pass-code lock system using the Arduino Mega board.
Whenyou type the right code, an LED lights up an the servo moves to open a lock.
What you will need:
--->one Arduino Mega (the arduino uno or duemilianove does not have enough digital pins for this project)
--->one LCD module
--->one Keypad
--->one Battery pack (or you can use the USB cable and PC power)
--->one 10K Ohm potentiometer
--->four 10K Ohm resistors
---> Breadboard
---> hookup wire
--->one servo

Step 1: Wire the LCD to the Arduino

The LCD module has 16 pins.
First of all, connect pins 1 and 16 of the LCD to the ground rail on the Breadboard
Then connect pins 2 and 15 of the LCD to the +5v rail on the breadboard
Now connect the ground rail(should be blue) of the breadboard to a ground pin on the Arduino;
Connect the +5v rail of the breadboard(this one is red) to one of the +5v pins on the Arduino board.

Now comes the contrast potentiometer which has to be connected to pin 3 of the LCD.
The potentiometer will have 3 pins. Take the middle pin and connect it to pin 3 of the arduino with hookup wire. Connect the othere two pins one to +5v and the other to GND(ground). The order does not matter.

Now let's do a test: power up the arduino. The LCD should light up. If it does then Great! If the LCD does not light up then turn off the power and check the wires.

Never change, move, or take out wires from the circuit board when the Arduino is powered up. You may permanently damage the Arduino.

If the light works rotate the potentiometer all the way to the right and all the way to the left until you see 2 rows of black squares. That's the contrast.

Now take out the power and let's hook up the LCD to the Arduino with the signal wires so we can display something on it.
Ready? Let's go!

Connect the pins as follows:
LCD Pin 4 --> Arduino Pin 2
LCD Pin 5 --> Arduino Pin 3
LCD Pin 6 --> Arduino Pin 4
LCD Pin 11 --> Arduino Pin 9
LCD Pin 12 --> Arduino Pin 10
LCD Pin 13 --> Arduino Pin 11
LCD Pin 14 --> Arduino Pin 12

And that should do it for the LCD circuit.
A test code for the the LCD: temporary.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2,3,4,9,10,11,12);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:

Copy and paste it in an arduino environment window, make sure you have the board and serial port set correct and click UPLOAD after you plug in the usb with the arduino.
You will see the TX and RX led's blinking, that means the code is going to the arduino.
push the reset botton once on the arduino, tune the contrast, and you should see Hello World displayed.
Congratulations! You've got the LCD working! :) 

Step 2: Wire the Keypad to the Arduino

OK. Now that we're done with the LCD and we got it working, it's time to connect the keypad to the arduino. This can be a little tricky depending on what type of keyboard you are using. In my case, I used a 3x4 keypad that I had for some time.
If you have a keyboard that is made especially for connecting to an arduino, then it's easy. You just look at the datasheet for it and it tells you exactly how to hook it up.
If you have a keypad and you have no datasheet for it then hang on cause I was in the same situation.
Mine had on the back a diagram that shows you which pins are connected together when you press a certain key.
If you don't have that, you will have to use a multimeter and figure out which pins are connected together when you press each key.
To do that, take your multimeter and set it on continuity(the diode symbol).
Then put the test leads on pins 1 and 2 of the keypad. Now press every key until you get continuity.
Take paper and a pen and write down the key(ex:1, 2, #) and the two pins(ex: 6[1;2]).
Do so for every key until you get all of them figured out.
Make a table:

That is what I got.
Whatever you get, if you write down the keys in that order you will see the logic in it.
From my table I can see that the row pins are 1,2,3,4; and the column pins are 5,6,7.

Now plug the pins of the keypad in a breadboard and let's start connecting it.
Connect the pins for rows 2 and 3( in my case pins 2 and 3) to +5v through 10K Ohm resistors. Do the same with the pins for column 1 and 3 pins( in my case pins 5 and 7).

If you have an arduino mega, connect the keypad as follows:
Keypad pin row1--> arduino pin 25
Keypad pin row2--> arduino pin 24
Keypad pin row3--> arduino pin 23
Keypad pin row4--> arduino pin 22
Keypad pin column1 --> arduino pin 28
Keypad pin column2 --> arduino pin 27
Keypad pin column3 --> arduino pin 26

(The arduino uno does not have enough digital pins so it does not fit this project.)

That should do it for the keypad. :) we're one step closer to finish. Hang in there. :) Almost done.

Step 3: Connecting the Servo

OK, the servo is very simple.
It has 3 wires: Red, Yellow(or white or orange), and black.
Connect the red wire to +5v, the black wire to GND, and the middle wire to digital pin 8.
That's it for the servo.

Step 4: Preparations for Coding

Before we put in the final code we have to make some modifications.

G o to the link above and download the libraries: keypad and password.
They are two files. take the files and put them in /Arduino/Libraries.

if the download does not work for bizarre reasons, go to:



Step 5: The Code, and the End

Now it's time for the code.
Make sure you have all the wires in place and connect the USB cable.
Upload the following code to the arduino. Copy and paste it in the arduino window just like last time.

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
LiquidCrystal lcd(2,3,4,9,10,11,12);
Password password = Password( "4321" );
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'*','0',' ',}
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
const int buttonPin = 7;
int buttonState = 0;

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

#define ledPin 13

void setup(){
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
digitalWrite(ledPin, LOW); // sets the LED on
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
switch (eKey){
case ' ': guessPassword(); break;

void guessPassword(){
if (password.evaluate()){
digitalWrite(ledPin,HIGH); //activates garaged door relay
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 15ms for the servo to reach the position
for(pos = 180; pos>=50; pos-=1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 15ms for the servo to reach the position

digitalWrite(ledPin,LOW); //turns off door relay after .5 sec
lcd.print("VALID PASSWORD "); //
password.reset(); //resets password after correct entry

lcd.print("INVALID PASSWORD ");
password.reset(); //resets password after INCORRECT entry

Give it a test: type 4321 then press #.
You should see the message VALID PASSWORD Welcome

After that the LED on the arduino board will light up for a short time and the servo will move to open the lock. 

And that's it, you've got yourself a password access system.
Put it on your door, or make a safe, or make a..... whatever you want. Put it on your bird cage so no one can steal your expensive exotic talking parrots.
If you have any problems or questions regarding this instructable, feel free to post a comment. I will answer as soon as I can.
Also, if you like the project, consider voting it in the following contests:




Thanks for reading this 'till the end and I hope you liked it.

You might be able to modify this for the Uno/duemilanove if you use a 2x16 serial LCD...they only have one data wire, plus you can exclude the liquidcrystal.h because you simply use serial print.
<p>Is there a way of finding out how to do this for a raspberry pi?</p>
hey I found an error in arduino software about memory space after uploading tha LCD testing program<br><br>so please help to rectify the problem...thanks in advance!!!<br>
<p>i still didn't get the use of digital pin 7 in this code...</p><p>please explain me</p>
thank you for such an awesome instructable :)
<p>i still didn't get the use of digital pin 7 in this code...</p><p>please explain me</p>
Thanks for checking it out. I hope I'll have time in the near future to make some more.
<p>hello sir.. What's about if I want to overwrite or create new password by storing into somewhere(sd card or EEPROM)? Will u share this type of coding to me? abdulhadimuda@gmail.com much appreciate for your respond and feedback sir</p>
Any way you can comment on what or where you got your two breadboards and what kind of servo your using?
I've got three breadboards on there:<br> -a <a href="http://www.robotshop.com/sfe-170-tie-point-mini-self-adhesive-solderless-breadboard-blue.html">small one</a> for the keypad<br> -a <a href="http://www.robotshop.com/400-tie-point-solderless-interlocking-breadboard.html">medium one</a> for the LCD, potentiometer, and also as a bus<br> -a <a href="http://www.robotshop.com/elenco-9830-breadboard.html">large one</a> for experimenting<br> -the <a href="http://www.hobbypartz.com/9gmiseexis1.html">servo </a>is an RC plane servo. The same one I used on the hovercraft.<br> <br> Of course if you follow the connection layout you can use solderable PCB's and make a more durable and permanent circuit. But make your first try and tests on the solderless breadboards. You can also get theese from radioshack (except for the servo). Go to the orange hyperlinks and check those out. And, good luck on your future projects! :)
Any specific PCB or website that would be good to get PCB's from?
Sure.<br>I put the links in the comment (the orange words are hyperlinks ) :) go there and you'll find everything you need for your robotics/electronix projects.<br>The main webpage is www.robotshop.com ... It's a great website. <br>I hope you'll find what you need and if you need any further help don't hesitate to ask. I'll be happy to help you.
What is keypad event ? can u give an examples as how it works?
I'm using the arduino IDE 1.05, and I tried to run the examples provided in the Password library. However, i just got the following error: <br>In file included from SerialMonitor.pde:15: <br>C:\Users\Administrator\Documents\Arduino\libraries\Password/Password.h:61: error: 'byte' does not name a type. <br> <br>Is it because the newest version of the arduino IDE(1.05) dose not support byte?
&quot;...yte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad <br>const int buttonPin = 7; <br>int buttonState = 0; .....&quot; <br> <br>how does the arduino know when i am done entering my password and press # to 'enter' the system? it seems like a condition check is applied to Pin no 7 of the arduino board. I couldn't find anything attached to pin 7 of arduino...could you please look into it and confirm? I am assuming there is a typo in the code.
Hey, here's how it works: <br>The keypad is set up so that there is a space instead of a '#' <br>// Define the Keymap <br>char keys[ROWS][COLS] = { <br>{'1','2','3',}, <br>{'4','5','6',}, <br>{'7','8','9',}, <br>{'*','0',' ',} <br>}; <br> <br>then, when the Arduino reads what you write, it knows when you finished typing your password because you press the # key at the end: <br> <br>switch (eKey){ <br>case ' ': guessPassword(); break; <br>default: <br>password.append(eKey); <br>} <br> <br>That bit of the code basically says: &quot;Okay, so if the key that you press just sends me a white space, I will perform guessPassword(), which checks to see if your password is correct.&quot; <br> <br>I hope that helps. Let me know if you need any more info.
Sir, is it possible to display only asterisk as password? uhmm..encrypting the password being inputted by a user, uhm,,how can we do this? thank you
Yes, just change the lcd.print(key) to lcd.print(&quot;*&quot;).
helo. i would like to request the circuit schematics. email me at luqmansemidin@gmail.com . thanks in advance :)
I went to verify the final code and got these errors, can you help?: <br> <br> <br>In file included from sketch_dec28a.cpp:1: <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Password/Password.h:33:22: error: WProgram.h: No such file or directory <br>In file included from sketch_dec28a.cpp:1: <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Password/Password.h:61: error: 'byte' does not name a type <br>In file included from sketch_dec28a.cpp:3: <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:50: error: 'byte' does not name a type <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:51: error: 'byte' does not name a type <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:63: error: 'byte' has not been declared <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:63: error: 'byte' has not been declared <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:63: error: 'byte' has not been declared <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:63: error: 'byte' has not been declared <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:78: error: ISO C++ forbids declaration of 'byte' with no type <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:78: error: expected ';' before '*' token <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:79: error: ISO C++ forbids declaration of 'byte' with no type <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:79: error: expected ';' before '*' token <br>sketch_dec28a:24: error: no matching function for call to 'Keypad::Keypad(char*, byte [4], byte [3], const byte&amp;, const byte&amp;)' <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:63: note: candidates are: Keypad::Keypad(char*, int*, int*, int, int) <br>C:\Documents and Settings\Michael\My Documents\Arduino\arduino-1.0-windows[1]\arduino-1.0\libraries\Keypad/Keypad.h:57: note: Keypad::Keypad(const Keypad&amp;)
You do not have the libraries where they are supposed to be. The error message is telling you that it can't find the libraries. <br><br>
I have the libraries in the same library folder where all my other projects are.
are you using mac or windows?<br>
Windows XP
[... here's what they say on the Arduino webpage...]<br><br>If you're using one of these libraries, you need to install it first. To do so, download the library and unzip it. It should be in a folder of its own, and will typically contain at least two files, one with a .h suffix and one with a .cpp suffix. Open your Arduino sketchbook folder. If there is already a folder there called libraries, place the library folder in there. If not, create a folder called libraries in the sketchbook folder, and drop the library folder in there. Then re-start the Arduino programming environment, and you should see your new library in the Sketch &gt; Import Library menu.<br><br>http://www.arduino.cc/en/Reference/Libraries
How do i add the libarys i extracted them to arduino/libarys? 2 folders but not working
I can't get the LCD to show two rows of black squares. Any ideas?
Hi there! <br>That's most likely a problem with your contrast potentiometer. If the potentiometer does not work, you may have not wired it right or maybe it is not 10KOhm. Try using a 10kohm resistor and lcd.print() something to see if it works.<br><br>Good luck!
Maybe it is good if you try to find out by asking direct and visited the campus in question. So that everything will be much clearer.pellet for sale
I built something similar for my cs160 final. I didn't have the money for the LCD, keypad, or servo, so mine was just a bunch of buttons on a breadboard with LEDs. I would love to dig out my Arduino stuff and build one of these again though.

About This Instructable




More by razvan_iycdi:Home Speaker Detector DIY 9V Battery Clip Build your own hovercraft 
Add instructable to: