Matrix keypad library not working

Hey

I bought this 7 pin 3x4 matrix keypad. I hooked it up to arduino with 1K resistors and used the keypad library and i can't make it work.

Pin assignments are correct, i did a continuity check with my multimeter and everything IS fine.. Any ideas what i should try, i can't seem to make it work and i've run out of ideas. No keys are responding so far.

I've also tried manual digital pin readings for inputs, and they keep holding at high. I've also noticed that in the library, internal pullups are enabled for input pins. Any ideas why that is? I understand the whole floating pins problem, i just don't understand, how there can be a value change if the input pins are always connected to high with pullups.. (i'm a computer engineer and i'm kinda new to electronics, so having some problems understanding some of these concepts, despite reading tutorials :))

Any calrification would be greatly appreciated. Cheers, Val.

EDIT:
Hey, i've tried writing high to a column, and reading input on a row, and it works! Pullups also enabled. Is there something wrong with the keypad library??..

The keypad library does not work the way you think. It's not putting a HIGH to a pin to read it at the input. It does configure all row pins as inputs and activate the internal pull-up. All column pins are also configured as inputs by default. Then the library does reconfigure one column pin after the other to be an output and put out a LOW on it. The corresponding input pin will also go LOW if a button is hit. After reading all inputs, it configures the column pin as an input again.

This is a component saving technique to save the Arduino from frying of a short circuit if two buttons are pressed concurrently. If you would have one pin HIGH while the other is LOW (both outputs), you could destroy that port. If you program the Arduino this way, you theoretically don't need resistor to connect the keypad, you can connect it directly to the pins (although I would use the anyway).

To help you with your specific problem, show us your exact wiring and the whole code.

Hey, thank you for the reply! Hm well, my version of the library (2.0) keeps column pins as outputs at all times. It does switch columns to low one by one and checks rows, which are inputs, then sets column to high again. That should work i think.

Anyway, i was trying to avoid having to draw the wiring (being lazy and what not :D) but here it is. Ow and sorry for the sketch, haven't installed eagle for drawing pcbs yet.

Codes:

#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] = {29, 34, 33, 31}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 28, 32}; //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);
  }
}

This is the helloKeypad example from lib, the only thing changed is rows, cols, key assigning and the pinout.

Thanks for the help. Cheers, Val

The library I was referring to is found here:

http://arduino.cc/playground/Code/Keypad

Are you using another one? This is version 2.0 too (at least that's the version in Keypad.h, Keypad.cpp says 2.1) and it's not setting the column pins to HIGH.

What output do you get from your code? Nothing?

Hey

Tnx for the reply!

You're quite right, this seems to be a newer version. Mine is 2.0, i can't seem to find the link, sorry.

Either way, i've tried the 2.1 as well, the only output i get is '1', without pressing anything, then nothing and unresponsive.

With 2.0 all i got was '*', then nothing..

I really have no idea, what could be wrong..

Tnx for your help anyway. If you have any more ideas, by all means, please reply :slight_smile: Cheers, Val

valekovski:
Either way, i've tried the 2.1 as well, the only output i get is '1', without pressing anything, then nothing and unresponsive.

If I understand your drawing correctly it looks like you are using 1k ohm resistors on both the columns and rows. If you really want to use resistors on the pins then you should only put them on either the columns or the rows but not both. Also, because the internal pullups are being used you should use smaller resistor values. I used to use 100 ohm resistors until I fixed the library so that it would not short pins together.

Hey

Thank you mstanley! That was one of my guesses too, just didn't have time to try it out yet + a friend of mine hooked it up the same way and didn't have problems, tho as far as my keypad's reference goes, contacts on my keypad have up to 500 ohm resistance by itself, which is unusual i think. Will try it and will post results later today. Thank you.

Cheers, Val

EDIT:
Like i've said in one of the above posts, i've also tried setting a column to high and reading result on a row input and it worked fine. That is with 1k resistors on every pin and pullups enabled. So i'm not completely sure, this is the problem.

Hi guys,

I also have a problem with the keypad. I have a 4x4 keypad. Everything is working perfect, except that if I press a key in column 2 it registers as the same key (row) but in column 1. So if I press 2, it reads 1, 5 reads 4, and so on. Any ideas?

Hey

@Janneslr
Sounds like a short circuit to me.. Try to make a continuity check on pins for column 1 and column 2. Maybe it's a soldering problem. Let us know how it turns out. Cheers, Val

@mstanley
I've tried your suggestion, doesn't work. I've also tried writing my own code from scratch which doesn't work either. Seems like i have some floating pins or something! At this point i have no idea what the problem is, maybe it's soldering as well, idk. Either way, i'm pretty sure it's not a problem with your library :blush:.

So thank you very much for replying, it's a great lib, keep up the good work! I think i'll just buy a new keypad or something lol.

I've also tried writing my own code from scratch which doesn't work

That just means you didn't do it right.
Start simply testing as you go and build things up. If you get stuck post the code and we can try and help. You also need to post the way you have wired it up.

500 ohms is OK for your keys. I was just checking mine today and they are a bit over 500 ohms when closed. Headed off to bed right now but if you'll give me until tomorrow night I will wire up my keypad to match yours and see what might be happening.

In the mean time I would double check your pins for your keypad. The pin numbers you are showing in your diagram don't seem to have any consistency in their order which is rather unusual for most matrix keypads.

If you need help identifying the pin out of your keypad there is a tutorial:
http://arduino.cc/playground/Main/KeypadTutorial

@Grumpy_Mike
My code looks like this.

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] = {29, 34, 33, 31}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 28, 32}; //connect to the column pinouts of the keypad

void setup(){
  Serial.begin(9600);
}

void loop(){
  for (int i = 0; i < COLS; ++i) {
    pinMode(colPins[i], INPUT);
    //digitalWrite(colPins[i], HIGH);
  }
  
  for (int i = 0; i < ROWS; ++i) {
    pinMode(rowPins[i], INPUT);
    digitalWrite(rowPins[i], HIGH);
  }
  
  for (int i = 0; i < COLS; ++i) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], LOW);
    
    for (int j = 0; j < ROWS; ++j) {
      Serial.print(keys[j][i]);
      Serial.print(": ");
      Serial.println(digitalRead(rowPins[j]));
    }
    
    pinMode(colPins[i], INPUT);
    //digitalWrite(colPins[i], HIGH);
  }
  
  delay(5000);
}

It's the same as keypad's library.

@mstanley
Thank you for your effort, but please, don't loose too much time on this. I'm pretty sure it's a wiring problem. I have NO idea where the problem is. I've checked for short circuits, i've checked every connection, i've even checked my protoboard for possible factory faults..

I am pretty sure pin assignments are correct. I've checked at least 5 times, i've read the tutorial on keypads and applied the same procedure. I used continuity check and it beeps as expected when i press the right buttons. One of my first thoughts was the same tho, i find it weird that there's no order at all in pin assignments.

Still, at this point, i am convinced there's a problem with wiring or pin assignments (but as far as i know (i kinda suck at math :D), no other permutations would work, continuity check works as expected on every single key..), at least that's the only thing i can deduce from above code's output. Or maybe a short circuit, i've no idea.. I must be missing something really stupid lol. And i'm really sorry for the hassle heh, thanks for helping everyone. Cheers, Val

Output:

If i don't press anything:
1: 0
4: 1
7: 1
*: 0
2: 0
5: 1
8: 1
0: 0
3: 1
6: 1
9: 1
#: 1

Ok this might be more representative:

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

If i press '2':

0 0 1
1 1 1
1 1 1
0 0 1

'5':

0 0 1
0 0 1
1 1 1
0 0 1

'8':

0 0 1
1 1 1
0 0 1
0 0 1

'0':

0 0 1
1 1 1
1 1 1
0 0 1

'1':

0 0 0
1 1 1
1 1 1
0 0 0

'4':

0 0 1
0 1 1
0 1 1
0 1 1

'7':

0 0 1
1 1 1
0 1 1
0 1 1

'*':

0 0 0
1 1 1
1 1 1
0 0 0

EDIT:
The result when not pressing anything can only suggest a short circuit, since every button should have read HIGH.. What else could it be, but multimeter doesn't detect any short circuits..

EDIT:
Ok, i've connected 5V to any pin, and all other pins get 5V on them! How the hell is that possible if there is no short circuits!.. Is that some open collector thingy? I thought keypads were simple switches, just wires..

I thought keypads were simple switches, just wires..

Yes they are but there might be some diodes in them.

Ok, i've connected 5V to any pin

No.........
Never go connecting 5V to any pin, this is a recipe for disaster. If the pin is an output with a logic zero on it you will blow up that pin.

and all other pins get 5V on them! How the hell is that possible

It is because the pins are floating and the induced charge on one pin is capacitavly coupled to all the others. This is normal and it is something you should not do.
Read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Hey

Thanks for the explanation. I ought have been more clear, i connected 5V to a pin on the keypad (the rest of the pins are disconnected - floating) and that was through a resistor also. I'm a computer engineer and a bit new to electronics, but i know the current draw of directly connecting 5V to an input will probably fry it :slight_smile:

Anyway, to be more clear. 5V is connected through a resistor to a random pin on the keypad, the rest of keypad's pins are floating and they all have 5V on them lol. Cheers, Val

Ok that's fine.

the rest of keypad's pins are floating

I think that be where you are going wrong. The input pins need to be pulled in one direction of the other at all times. Normally they are pulled up with a resistor and then pulled down by having the switch connect it to an output that is being driven low.

Hey!

SOLVED!

It was a soldering problem, still no idea where lol. Anyway, i resoldered the header pins and everything is working fine, finally! I think the header pins might have been a bit too short for the protoboard before, causing unreliable connections.. Tho that is really weird, since i did continuity checks and everything seemed fine..

Either way, mastanley, great library and thanks for your help and i'm really sorry for misleading, i just didn't know :slight_smile: Many thanks for your help too Grumpy_Mike.

Cheers, Val

No apology necessary, I'm just really glad to see you got it going.