12 keys keypad, library error when uploading!!

Hey, I’m new to Arduino and I’m trying to use a 12 keys keypad to serial print the key pressed.

I used one of the examples that came in the keypad.zip (library), but it always gives me an error.
I’m using the HelloKeypad.pde example:

/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
  }
}

When I click upload or verify it always gives me this error:

HelloKeypad:22: error: ‘Keypad’ does not name a type
HelloKeypad.cpp: In function ‘void loop()’:
HelloKeypad:29: error: ‘keypad’ was not declared in this scope

Any ideas?!

(I’m using an Arduino Mega 2560, if it matters)
Thanks in advance!

You didn't install the library in the right folder. Give me a screen shot of the folder that contains the library, with full disk path.

I'm using Mac! (BTW)

/Users/ruicostaesilva/Documents/Arduino/libraries/keypad

If the library wasn't in the right folder, would I be able to open the example sketches from examples menu? Just asking, because that was what I did!

Keypad goes in libraries, not in keypad in libraries.

Thanks, works great now! :)

After uploading I noticed the keys weren't right so I change the code to match the keypad. Yet several keys are not working... 2, 4, 6, 8 and 0 are not showing up on serial.write (they only show up when I press a few keys...)

:~

You are strongly recommended to have keypads under arduino/sketchbooks/libraries

This is where contributed libraries should go.

Check if you mistook rows for columns.

I’ve already replaced the files and it uploads just fine!

I think rows and columns are well defined, 'cause every other key it’s in the right place.
Rather than serial printing the wrong number, it just doesn’t print anything…

Yet if I combine 0 + # it prints 6!

The keys that don’t work are 2, 4, 6, 8 and 0 (in different columns and rows)

I suggest you to use a multimeter to check if you got 4 row by 3 column or 3 row by 4 column instead. The look is deceiving. Your symptom tells me you might have treated a column pin as a row pin.

3 columns x 4 rows... I re-checked all connections and change the code but the same 5 buttons aren't working.

This this a photo of the keypad.

What are the resistors doing there?

I follow the Adafruit Keypad Tutorial, and they had put resistors from keypad pin 3, 5, 6 and 7 to +5V.

I already take them off but the results remain! =(

Honestly I kind of hate these keypads. The membrane keypads are much more obvious with their connections. Here is some help:

Look at the diagram to see if you are connecting your keypad correctly.

I wired up the keypad again, according to the sparkfun tutorial, but I had to made a few changes in the code for put the keys in the right place.

Buttons 1, 3, 5, 7, 9, * and # are operational, yet the same 2, 4, 6, 8 and 0 not working!

Is it possible the keypad it’s damaged?
I bought it yesterday!

I discover that I can get the numbers missing by combining two or more keys (one that works, one that's not). For example:

Output - Key pressed 1 - 1 2 - (2 + 5) or (6 + 3) 3 - 3 4 - (4 + 5) or (0 + *) or (8 + 7) 5 - 5 6 - (6 + 8 ) or (0 + #) or (8 + 9) 7 - 7 8 - (6 + 9) 9 - 9 * - * 0 - (some combination I can't find out...)

-

This was the keypad I bought. http://www.ptrobotics.com/product.php?id_product=426

lookslikeruck: I discover that I can get the numbers missing by combining two or more keys (one that works, one that's not). For example:

Output - Key pressed 1 - 1 2 - (2 + 5) or (6 + 3) 3 - 3 4 - (4 + 5) or (0 + *) or (8 + 7) 5 - 5 6 - (6 + 8 ) or (0 + #) or (8 + 9) 7 - 7 8 - (6 + 9) 9 - 9 * - * 0 - (some combination I can't find out...)

-

This was the keypad I bought. http://www.ptrobotics.com/product.php?id_product=426

you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial) i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring

123 456 789 C0E

const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1','4','7'},
  {'2','5','8'},
  {'3','6','9'},
  {'C','0','E'}
};

zarobhr: you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial) i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring

123 456 789 C0E

const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1','4','7'},
  {'2','5','8'},
  {'3','6','9'},
  {'C','0','E'}
};

Yhap, but the mapping it's not the problem... It seems that when I pressed keys 2, 4, 6, 8 and 0 the Arduino just doesn't recognized them.

I'm not sure how the keypad works internally but I think it's based on the Voltage Divider Principle, with voltage drops between the keys that can tell wich of the key is being pressed. Is it possible that the resistors drop less voltage than the library it's configured for?

No, the matrix keypad does not work with voltage dividing principle. It instead works with columns touching rows. With a regular matrix keypad, where 123 are one row one, and 147 are on column 1, pressing 1 connects row 1 with column 1. If your column 1 is at GND and you sense row 1, you find row 1 to be LOW. If there is no pushing, then column 1 and row 1 are not connected so then row 1 will not read LOW (internally pulled to HIGH) and read HIGH. So the keypad library scans all columns and rows to determine which keys are pressed. Did that help?

lookslikeruck:

zarobhr: you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial) i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring

123 456 789 C0E

const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1','4','7'},
  {'2','5','8'},
  {'3','6','9'},
  {'C','0','E'}
};

Yhap, but the mapping it's not the problem... It seems that when I pressed keys 2, 4, 6, 8 and 0 the Arduino just doesn't recognized them.

I'm not sure how the keypad works internally but I think it's based on the Voltage Divider Principle, with voltage drops between the keys that can tell wich of the key is being pressed. Is it possible that the resistors drop less voltage than the library it's configured for?

matrix keypads do not work on voltage divider principle (it shorts the wires/pins) think of it like a switch, when you press a key only 1 pair of the seven wires will make contact and it should be nearly a complete short, just becuase the odd numbers work and even dont doesnt mean its not a mapping problem. its very likely that it is a mapping or a wiring problem

I've figured it out!!! :) :) :) :D

I didn't understand at the beginning what you meant with columns swapped with rows, but now I do.

My keypad it's configured slightly different.

3 6 9 # 2 5 8 0 1 4 7 *

4 x 3 and I was trying 3 x 4.

After re-map everything went just fine!! =) Thanks a lot!!