Home Of MetaWops.

OS X. iOS. Gadgets. Social Media. Basketball. Music. Synths. Electronics.

Arduino

Keypad code lock with Arduino

ArduinoKeypadCodeLockDue to popular demand … (there was this one guy on YouTube who asked for the source code) … here’s the source code for my little keypad code lock experiment I made with my Arduino Uno some time ago.

A few notes:

  • Find the included Keypad.h header file with the Keypad library here.
  • Basically, there are two types of keypads out there. This experiment uses one with 7 pins so that there’s NOT one pin for one key but instead the keypresses are encoded in 4 rows plus 3 columns.
  • If you don’t know what pin represents which row or column (because you don’t have a data sheet for the keypad) you have to reverse-engineer it. Connect every combination of two pins with your multimeter and for each combination press every key and note when there’s no resistance during the button press. See the photo of the notes in my Moleskine and you’ll know what I mean. 😉ReverseEngineeredKeypad
    Turns out in my case that the first three pins are the columns 1..3 and the last four pins are the rows 1..4. Simple!
  • Connect the red LED with a 220 Ohm resistor to pin 8 and the green LED with a 220 Ohm resistor to pin 9. And the keypad to pins 1 (column 1) to 7 (row 4). If you have a prototyping shield and breadboard you might try the following setup and you just have to put the seven keypad pins directly into the Arduino ports 1 to 7. You can see that in the YouTube video.KeypadCodeLockSetup
  • The program has a bug. Can you find it? What happens if you input 2 3 5 7 1 2 3 5 7 1 ? Why?
#include <Keypad.h>

char* secretCode = "235711";
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] = {4, 5, 6, 7};
byte colPins[cols] = {1, 2, 3};

Keypad keypad = Keypad(makeKeymap(keys),
                       rowPins, colPins,
                       rows, cols);

int redPin = 8;
int greenPin = 9;

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 == 6) {
    setLocked(false);
  }
  delay(50);
}

void setLocked(int locked)
{
  if (locked) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
  }
  else {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
  }
}
  1. ray

    where do i have to enter the secret code?

  2. peter

    could you tell me what’s the bug in the programme? I’m stuck with it. Thankyou!

  3. gab

    hi!

    Great project! i encountered this prob can someone help me? thanks

    sketch_jul01b:18: error: ‚Keypad‘ does not name a type
    sketch_jul01b.ino: In function ‚void loop()‘:
    sketch_jul01b:34: error: ‚keypad‘ was not declared in this scope

    • Hmmm… looks like you haven’t installed the Keypad library correctly. Could that be the case? How did you do it?

    • ahmed ibrahim

      you need to identify the keypad library first .
      you can go to arduino site and download it (keypad library and also password library )
      then copy it and paste on arduino library on C portion

  4. biru

    thankyou ! sir … you really solved my problem, now i can use this concept in my project of home security system

  5. José María

    Thanks a lot for the code. It helped a lot.
    Best regards from Spain.

  6. Benjamin

    When I insert this format directly into Arduino and the compiler had an error ‚“keypad‘ does not name a type.“ What is this error telling me to do and what type of function is Keypad keypad=Keypad…..

    Thanks

    • Hi Benjamin,
      did you read the whole article above? Did you read the comments? Most likely your problem is that you haven’t downloaded the Keypad library. The link is in the article.
      If you still have this problem after downloading and installing the Keypad library I’ll look into this. Just drop me another line here in the comments.
      Cheers,
      Stefan.

  7. sept

    And how would one go to do this with a keypad with one key for one button?

    thanks

    • What do you mean by „one key for one button“? Do you mean „one pin for each key“? Well, I don’t have such a keypad but I’d guess the straight forward plan would be: connect each keypad pin to its own pin on the Arduino. But first I’d search for a library which supports this. Otherwise you have to implement the low level functions yourself.

      • sept

        Exactly this, but I couldn’t find a library, but admittedly I don’t really know how to call it, so it’s hard to find.

        • sept

          It works just fine with the keypad library you used. You just have to set the cols to 1 and the rows to however many keys you have. The rest is exactly the same.

  8. Uli

    ATTENTION!

    Hi Stefan,

    I take a look at your sketch and it looks fine / shortly at the first view. But I want to find the bug before going into deep:

    ATTENTION:
    This sketch isn’t secret! Try any code – any code works, if it is long enough.

    It is only necessary, that you hint the next number (at any time!) that is stored in the next position in the array. To “hack” the keypad, you only need a few seconds. Please try it yourself, for example this code: 123456789012345678901

    -> Set the position counter to zero, if the NEXT pressed key isn’t equal to the number at the position in the code array. I think it must be something like “(key != secretCode[position])” around the row 43.

    cu

    Uli

    • FINALLY someone who accepted my challenge and thought about where the bug might be. Congrats! 🙂
      Yeah, I never said „use this hard & software for your gold vault“. It’s just a *sketch*, a quick & dirty hack, some ground work to take and start off with and build rock solid securecode locks. 😉

  9. Apo

    Hi stephan, thanks for the article! I have a question : I have downloaded the keypad file in the librairy, the message „keypad’ does not name a type.” Is gone but I still have a problem with the char function:“ keypad was not declared in this scope“.
    Thanks !
    Apo

  10. denny

    hy Stefan, I want your help, how to change the password by keypad not from the program? I mean if we applying this to our door or box , and we want to change the password, we just change it from keypad as we want, because it become much easier, so we don’t need to open the PC, open the IDE arduino, write the password, upload it. hehe 😀 , please I need your help in coding, you can reply here or send to my email, dennyhadiwibowo@gmail.com

    thank you very much for your help, Stefan 🙂

    • Well, although a valid question I’m afraid I can’t help you here. You got to learn how to program! Or maybe you find somebody else who can help? It shouldn’t be too difficult for a medium experienced developer. But I definitely don’t have the time to write that program for you, I’m sorry.

      • Jaroslav

        Please show me the magic !!! I’ve spend whole day an get nothing !!! Tried
        if (key != secretCode[position]) {
        position=0;
        }
        else position++;
        but till get nothing
        if (key == secretCode[position]) {
        position++;
        }

        if (key != secretCode[position]) {
        position=0;
        }

        till nothing
        char nkey =keypad.getKey();
        if (key != secretCode[position]) {
        position=0;
        }

        till nothing …
        Please save my soul!!!

  11. Teoman

    I found the bug
    if (position == 6) {
    setLocked(false); // this means when you pressed 6 buttons it will be unlocked

  12. ehtisham fazal

    sir i want to change password through coding means i have select any one key while user press them then all above password will discard and they will get new paswword ?

  13. prathamesh shirole

    how can i remove the bug

Schreibe eine Antwort

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

Theme von Anders Norén

%d Bloggern gefällt das: