Keypad Servo Lock

Introduction: Keypad Servo Lock

About: I am a hobbyist just trying to share some of my knowledge of what I learn about electronics to others and to make everything as simple as possible. Just a plain and simple kind of guy!

Hello everyone, hope you had a good day. If not hopefully you can just rewind with some open mindedness to this tutorial and some therapeutic music. Programming can be a hassle. Thankfully, this tutorial is not a hassle, so you maybe able to accomplish a popular thing called (in mighty voice): THE ULTIMATE SERVO KEYPAD LOCK!!! That's really just my introduction, hope you liked it. Anyway, let's get our fingers warmed up for typing. (A cool song: can-can song (classical music)).

Supplies

-Your fingers

-Your hands too, that's important

-USB cable for Arduino

-Any type of arduino available (I have the Arduino Mega)

-Matrix Keypad (keypad - ebay, keypad - amazon)

-Micro or Standard servo (I'm using a micro servo)

-*Optional* Breadboard - any type

-*Optional* Power supply (5V)

Step 1: Schematic Time!

So, what do first? Hook up the Arduino to the computer. Connect the keypad next. *Note - you need to connect the pins from the last pin on the keypad as the first digital pin on the arduino. And go from right to left after that*. Keypad has pins 2-9 on the arduino. The servo has pin 10 as digital signals. Tie the grounds of the arduino, keypad, and servo and the 5V too. *Also note - that the servo is best used with a external power source or an adapter with 5V (wall plug-in), but it's still optional.* You don't have to tie the grounds and 5V physically, what I mean is to just to attach a wire from the Arduino to the servo. This schematic doesn't help beginners, so allow me to explain in short detail. Basically, the last pin is 2 and the first one is 9, two is 8, three is 7, and so on. Go from left to right 2 <----- 9 or right to left 9 ------> 2.

Step 2: Coding Time!

Ok so now we're on to the coding part! This is where the real magic happens! So, there is a test program to first test the keypad and servo. Here:

//www.elegoo.com
//2016.12.9
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
  }
}
//www.elegoo.com
//2016.12.08 #include
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop() {
  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(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Here is the project code (I'm going to be honest with you, I didn't do much, I just added the servo part, say thanks to the people who made all the code!):

#include Password.h

#include Keypad
/*
  ||  Simple Password Entry Using Matrix Keypad
  ||  4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
  ||
*
/* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////
#include 
 //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include  //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
Password password = Password( "1234" );
Servo myservo;  // create servo object to control a servo
#define servoPin 10
const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2, }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  myservo.write(0);
}
void loop() {
keypad.getKey();
  
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState()) {
    case PRESSED:
      switch (eKey) {
        case '*': checkPassword(); break;
        default: password.append(eKey);
      }
  }
}
void checkPassword() {
  
  if (password.evaluate() == true) {
    Serial.println("Success");
    //Add code to run if it works
    myservo.write(180);
    
    delay(3000);
    myservo.write(0);
    password.reset();
  } else if (password.evaluate() == false) {
    Serial.println("Wrong");
    myservo.write(0);
    //add code to run if it did not work
  }
}

Step 3: What Does All This Mean?

The code up above (the first one) will give a signal to the Servo to "sweep" or just go from 0 to 180 degrees. Or just completing half a circle of rotation from left to right or right to left. If you want more details on the code I can tell you. Just please ask. No harm done, just a simple little question. Come on, you can do it! Anyway, the code in the middle test every button of the keypad. And the last of the code just "unlocks" with the servo when you type in the passkey "1234" into, you guessed it, the keypad.

Step 4: Troubleshooting

Q: My keypad is not working.

A: Remember the first pin is 9 so go down from there. Also make sure to connect the arduino to the USB port of the computer and program the Arduino.

Q: Servo is not working.

A: Remember that the servo pin is pin 10 and that you need a common ground, if you are connecting straight from the Arduino, remember that the orange pin (on the micro servo) is pin 10, the red pin is 5V, and the brown pin is the GND pin.

Q: Arduino is not working:

A: Did you even connect it? That sounds like a personal problem

Step 5: If You Have Any Questions, Feel Free to Ask in the Comment Section and Remember to Subscribe!

Bye, thanks for viewing my tutorial. It's been fun.

Be the First to Share

    Recommendations

    • Tiny Things Speed Challenge

      Tiny Things Speed Challenge
    • Hide It Challenge

      Hide It Challenge
    • 3D Printed Student Design Challenge

      3D Printed Student Design Challenge

    2 Comments

    0
    handyharris17
    handyharris17

    Reply 11 months ago

    Thanks for noticing!