Apple • Gadgets • Synths • Electronics • Math • Mostly German


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] = {

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);

void loop()
  char key = keypad.getKey();
  if (key == '*' || key == '#') {
    position = 0;

  if (key == secretCode[position]) {

  if (position == 6) {

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


    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

    • Kommentar des Beitrags-Autors

      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…..


    • Kommentar des Beitrags-Autors

      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.

  7. sept

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


    • Kommentar des Beitrags-Autors

      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


    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:

    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.



    • Kommentar des Beitrags-Autors

      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 !

  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,

    thank you very much for your help, Stefan 🙂

    • Kommentar des Beitrags-Autors

      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]) {
        else position++;
        but till get nothing
        if (key == secretCode[position]) {

        if (key != secretCode[position]) {

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

        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

  14. gap

    what about setLocked was not declared in the scope

Schreibe eine Antwort

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