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

Due 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:

• 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. 😉
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.
• 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};

rowPins, colPins,
rows, cols);

int redPin = 8;
int greenPin = 9;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}

void loop()
{
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);
}
}
```

### 32 Kommentare

1. #### ray

where do i have to enter the secret code?

• Kommentar des Beitrags-Autors

#### Stefan

It’s in the second line of the source code:
char* secretCode = „235711“;

Just change it to what you want.

• #### Alessandro

why ?
email: alessandrodisola2003@gmail.com

• #### Alessandro

but how do you camiare your password ? that I have to put numbers to change it ?
thank you

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

• Kommentar des Beitrags-Autors

#### Stefan

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

• Kommentar des Beitrags-Autors

#### Stefan

Glad my article was of use for you! 🙂

• #### tracy

Hi
If I wanted to change the PIN (secret code) via the internet could this be done using an Ethernet module with the Arduino?

• Kommentar des Beitrags-Autors

#### Stefan

Sure! Almost nothing is impossible. However, I haven’t tried that and can’t give any help here.

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

• Kommentar des Beitrags-Autors

#### Stefan

Hi Benjamin,
Cheers,
Stefan.

7. #### sept

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

thanks

• Kommentar des Beitrags-Autors

#### Stefan

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

• Kommentar des Beitrags-Autors

#### Stefan

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

thank you very much for your help, Stefan 🙂

• Kommentar des Beitrags-Autors

#### 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
if (key != secretCode[position]) {
position=0;
}

till nothing …

11. #### Teoman

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

• #### DEOKAR

PLZ TELL ME WHERE IS THE ERROR EXACTLY?

13. #### 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 ?

14. #### prathamesh shirole

how can i remove the bug

15. #### gap

what about setLocked was not declared in the scope

### 1 Pingback

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