Arduino + SparkFun Keypad

Ok, so I just got the sparkfun keypad in the mail today:

And am having such a brain fart with it. I don't even know exactly what the issue is. I started out thinking that the generic keypad code @ Arduino Playground - KeypadTutorial would work great. Not so much. I first tried it with my LCD4Bit lib and got a bunch of garbage when pressing a button so I just used the keypad library plus the code on the above playground page but ported it to Serial. No such luck. I'm not getting anywhere yet.

So the next thing I did was run over to SparkFun's site and grabbed the pic of the keypad board and started adding numbers and figuring out what should be the rows and columns. That didn't work either for me. I was a little disappointed that the product wasn't a little friendly in the beginning. Of course, I'm a newbie so maybe its my fault.

Anyway, I would appreciate some help getting this keypad up and running (inc code/where I screwed up).

Here is the code I'm using in Wiring/Arduino:

// An example for testing the matrix keypad driver.
// I don't show an output device but you can use neilzero's
// 4 Bit LCD driver or send characters out the serial port.
// A working example that uses neilzero's LCD driver is
// included in the attached file.
#include <keypad.h>

#define ROWS 4
#define COLS 3

// keypad kpd = keypad() defaults to a 4 by 3 keypad.
// Use ROWS and COLS to match your keypad up to 4x4 keys.
keypad kpd = keypad(ROWS, COLS);

void setup()
{
Serial.begin(9600);
kpd.init();
Serial.println("Ready...");
}

void loop()
{
char key = kpd.get_key();

if(key != '\0')
Serial.print(key);
}

The code above only spits out numbers when #7 and #8 are pressed on the keypad.

#7 spits out 459
#8 spits out 48 (in serial monitor)
Plus a lot of garbage stuck on one number half the time.

Here is the edited library pin settings:

#define COL0 11 // #05
#define COL1 12 // #01
#define COL2 13 // #03
#define COL3 5 // not used as this is a 3 Col x 4 Row keypad
#define ROW0 7 // #02
#define ROW1 8 // #07
#define ROW2 9 // #06
#define ROW3 10 // #04

The #xx above in comment are the pins that the Arduino Digital Pins connect to on the SparkFun keypad. Of course, there are 2 pins (one on each side of the pic at sparkfun) which I do not know whether I should connect or not (I did play with hooking these to 5V/GND with no change).

So here is my question. Is my code screwed up or is this just a pain in the a** because I don't have a matic that defines how the keypad works?

Note: My Arduino board is working fine...tested that. Also tested voltages on a few pins of the keypad and did not get anything higher than 5.7V when pressing/releasing a key.

Arduino is ran off USB (unfortunately Vista PC), latest firm, and Arduino 0010A programmer.

Maybe I'm a dumba** but who makes a component with anything more than a few graphics? Should there not be a PDF with details? Should I have gone with something else through Digi or Mouser?

I didn't look at your code because I don't have time but I did find this schematic of the matrix.

also

Yeah, I ran into the schematic too and that schematic makes a lot of sense concerning the keypad library. However, if you take a look at the numbers on the pic of the sparkfun keypad, the rows and columns are different.

I went ahead and hooked the keypad differently after adding everything up and thought it would work but it did not. Here is what I figured the deal was

Row0 = keypad pin 2 -> Arduino pin 7
Row1 = keypad pin 7 -> Arduino pin 8
Row2 = keypad pin 6 -> Arduino pin 9
Row3 = keypad pin 4 -> Arduino pin 10
Col0 = keypad pin 3 -> Arduino pin 11
Col1 = keypad pin 1 -> Arduino pin 12
Col2 = keypad pin 5 -> Arduino pin 13

After hooking that up and adding the pin settings to the keypad library I still ran into the same problem with only the 2 number keys spitting values out to the Serial monitor and both of those keys were spitting out 2 or 3 numbers at each press.

I'm I missing something here? This keypad definitely doesn't seem to be the same as the one in the Arduino Playground tutorial.

Did you change the keypad.cpp file to map to the matrix correctly?

Yes, I used the image of the board plus the numbering system to determine that the pins are as follows:

Row0 = keypad pin 2 -> Arduino pin 7
Row1 = keypad pin 7 -> Arduino pin 8
Row2 = keypad pin 6 -> Arduino pin 9
Row3 = keypad pin 4 -> Arduino pin 10
Col0 = keypad pin 3 -> Arduino pin 11
Col1 = keypad pin 1 -> Arduino pin 12
Col2 = keypad pin 5 -> Arduino pin 13

and set the code in keypad.cpp to:

#define COL0 11 // #05
#define COL1 12 // #01
#define COL2 13 // #03
#define COL3 5
#define ROW0 7 // #02
#define ROW1 8 // #07
#define ROW2 9 // #06
#define ROW3 10 // #04

Did I miss something?

I just got my sparkfun keypad in (same one), and after I got through the (same) trouble of re-doing the row/col pin assignments (would've been easier if they had put all rows/columns on the same column in their diagram), and got random noise. Added a pull-up resistor on each pin, and it works fine.

Did you add a pull-up resistor for each pin? The tutorial they link to advises to do so, and it seems to work fine when I do.

Oh, and for those wanting a very clear view of the pin assignments:

ROW0 = 2
ROW1 = 7
ROW2 = 6
ROW3 = 4

COL0 = 3
COL1 = 1
COL2 = 5

Hey Fellas, :slight_smile:

After several days of toiling with my 4x4 Hex keypad[example:]] I bought from a local shop here, which for many reasons I was unable to get the Keypad tutorial [ Arduino Playground - HomePage ]to work for me also; I headed 4 the net to google solutions.

Briefly, Basics:

We make either the Rows or the Column Logic HIGH [output from Arduino]

and scan [read] the corresponding Columns or Rows that picks the Logic HIGH from

the Output!

A word of notice; we cant compare similar logic. So, my approach was to keep all my inputs [ROWS in my own case... I soldered my pull-down to the Columns lines mistakenly (Bad practice I know :D)] and switch them HIGH only when I intend scanning the particular row. While I read the columns for a corresponding HIGH

Here's my code and example. I certainly took a cue form Mark Sternley's work. feel free to share or contact me.


http://www.esnips.com/nsdoc/33740a2d-1df0-419f-96e6-751ee6f344ca/?action=forceDL

I'm a n00b too :wink: ;D :wink:

sorry, no schematics now. I'm pretty busy. But need to share this

Added a pull-up resistor on each pin, and it works fine.

Did you add a pull-up resistor for each pin? The tutorial they link to advises to do so, and it seems to work fine when I do.

I certainly must have been unlucky getting those "Pull-up resistor on each pin" instructions in the tutorial.

So for clarification, OUTPUTs certainly dont need pull-up nor pull-down resistors. Only connect the pull-up or pull-down resistors to the INPUT pins of the Arduino.


[NOTE: For N00bs like me....]
Whether you would be using a pull-up resistor pull ur input to normally HIGH [5v] or pull-down resisitor to normall LOW [0v] (like I did ) certainly depends on the logic you intend to scan with. i.e: use Pull-ups if you are sending logic HIGH at output and would switch current corresponding scanned row/column to LOW expecting to take action if you read a LOW from any input.

OR

you use Pull-down resistors [like mine] if you are sending logic LOW at your output and intend to switch HIGH (activate) for scanning so you can read any input that reports logic HIGH at any input. [Personally I thought this might be better... so we dont keep holding our outputs at logic HIGH if we are not scanning :D]

Pull-down config :

Pull-up config:

Added a pull-up resistor on each pin, and it works fine.

Did you add a pull-up resistor for each pin? The tutorial they link to advises to do so, and it seems to work fine when I do.

I certainly must have been unlucky getting those "Pull-up resistor on each pin" instructions in the tutorial.

So for clarification, OUTPUTs certainly dont need pull-up nor pull-down resistors. Only connect the pull-up or pull-down resistors to the INPUT pins of the Arduino.

We might clarify even further here:

The ROW pins on your keypad are inputs. (they will be attached to input pins on your arduino)
The COL pins on your keypad are outputs. (they will be attached to output pins on your arduino)

You must use a pull-up or pull-down on the ROW pins.

(I apologize for not making it clearer in my earlier post)

Now, if you want to use a pull-down (checking for a state change from LOW to HIGH), you must either invert the logic in the keypad library, or roll your own code. Other than your preference for looking for state changes from LOW to HIGH vs. HIGH to LOW, I can't see any draw-back of using the standard method (at least, every example I can find...) of using pull-ups and checking for HIGH to LOW transitions. In fact, there's a benefit of doing this in that all you have to do is adjust the row/col definitions in keypad library.

!c

Sorry about being a total noob, but can someone pelase explain how to wire in the pull up resistors?
:-[

This is driving me nuts trying to get this to work!

edit...

actually, I think I have got it to work but not sure if I did it right.. I have just got 1x 10K resistor going from each input pin to the 5V

looking above at the diagram, I am not sure what you mean by having the 100ohm resistor in there?

can someone explain? I have for each pin what is above, without the 100ohm resistor...

Marty

Sorry about being a total noob, but can someone pelase explain how to wire in the pull up resistors?
:-[

This is driving me nuts trying to get this to work!

edit...

actually, I think I have got it to work but not sure if I did it right.. I have just got 1x 10K resistor going from each input pin to the 5V

looking above at the diagram, I am not sure what you mean by having the 100ohm resistor in there?

can someone explain? I have for each pin what is above, without the 100ohm resistor...

Marty

Marty, it's common for people to use a resistor on both sides of the connection, though not entirely necessary. (For this case, at least.) The reason they use a 100ohm resistor is that it has less resistance than the 100Kohm resistor -- meaning that when the switch is connected, the path from the pin to GND has less overall resistance, and therefore current flows from GND to pin. The switch has, 'effective infinite resistance' when open, meaning it's almost impossible for current to flow through the switch, so the 100Kohm path to +V is chosen for current flow.

However, you don't need the second resistor for your keypad. Here's an image of how I wired up my keypad:


Note that the red wire leads to +5V, and the purple wires lead to the arduino inputs. Resistors are connected to each ROW pin, making them pull-up's to +5V (I used 100Ko pull-ups because some of my pins go to RX/TX pins on the arduino, and this prevents image upload issues by giving too much voltage through them.)

In the second photo, you can see which pins are 'pulled-up' because they have 3 solder points bridged instead of two.

Hope this helps.

!c

(I used 100K pull-ups because some of my pins go to RX/TX pins on the Arduino, and this prevents image upload issues by giving too much voltage through them.)

Wow, thanks for your fast response, pictures and all!

I did some research on the web, and it does seem that 10K to 47K seems to be the 'standard' for pull up resistors on digital circuits.
Basically, I would like a 'standard' so I can just buy a bag of the little buggers and have them handy (saves driving across town to buy one 5cent part!)

I like to keep things simple, so would it be correct to say that:
Just stick a pull up resistor on all input switches when building a project. (not just keypads)
If you use a 100K?? resistor, it will be fine for any input pins, wont do damage, and wont stuff up other functions (like image uploads etc)

Marty

(I used 100K pull-ups because some of my pins go to RX/TX pins on the Arduino, and this prevents image upload issues by giving too much voltage through them.)

Wow, thanks for your fast response, pictures and all!

I did some research on the web, and it does seem that 10K to 47K seems to be the 'standard' for pull up resistors on digital circuits.
Basically, I would like a 'standard' so I can just buy a bag of the little buggers and have them handy (saves driving across town to buy one 5cent part!)

Well, you know, you can just buy a bundle of, say, 1,000 resistors for like $10 =) It would have a whole bunch of those, and a whole bunch of all the others you might need. =)

I like to keep things simple, so would it be correct to say that:
Just stick a pull up resistor on all input switches when building a project. (not just keypads)
If you use a 100K?? resistor, it will be fine for any input pins, wont do damage, and wont stuff up other functions (like image uploads etc)

Marty

Well, yes and no... To keep things really simply, just use arduino's built-in pull-up's grin : http://www.arduino.cc/en/Tutorial/DigitalPins

My problem was specifically related to using an external pull-up to the RX/TX pins, which means they were getting voltage that was interfering with serial communication. If you don't put an external pull-up on the RX/TX pins (digital 0 and 1), then you'll be ok =)

!c

Well, yes and no... To keep things really simply, just use arduino's built-in pull-up's grin : http://www.arduino.cc/en/Tutorial/DigitalPins

My problem was specifically related to using an external pull-up to the RX/TX pins, which means they were getting voltage that was interfering with serial communication. If you don't put an external pull-up on the RX/TX pins (digital 0 and 1), then you'll be ok =)
!c

I was reading about that last night and wondering if its that simple, why don't we use them on the keypad?

I just modified the keypad include:

origional (INIT Section only):
void keypad::init ()
{
for (int c=0; c<num_cols; c++)
{
pinMode(col

,OUTPUT);                        // Set column pins as outputs for writing
            digitalWrite(col[c],HIGH);                  // Make all columns inactive to start with.
      }
      col_select = 0;                                    // Start with the first column.
}[/color]

[b]New:[/b]
void keypad::init ()
{
      for (int c=0; c<num_cols; c++)
      {
            pinMode(col[c],OUTPUT);                        // Set column pins as outputs for writing
            digitalWrite(col[c],HIGH);                  // Make all columns inactive to start with.
      }
[color=#ff9900]
      for (int r=0; r<num_rows; r++)
      {
            digitalWrite(row[r],HIGH);                  // Make all rows turn on pullup resistor.
      }[/color]
      col_select = 0;                                    // Start with the first column.
}

and it seems to work without pullup resistors..  

anything wrong with that?  (keep in mind I am very new at electronics, C++ and Arduino - the total of what I know can be written on the back of a postage stamp with a sharpie...)  I would like to know if I have done something dumb!

I'm no expert myself, and I can't see any reason why it wouldn't work, but I've already got my keypad soldered up, so I have no means by which to try it =) (To be honest, I hadn't learned about the internal pull-up's until I was already using my own!)

So, it works for you then? Can't beat that - next time I'll save myself a few cents and ten minutes of soldering, hehe.

!c

well you see,i just got my keypads too and i was wondering,how the hell do i make it work??? someone got a complete schematic+code+fixed library??

could you like upload it to a simple and accurate tuto instead of the keypad tuto wich of course isn't working at all?

Hello gents - i have working code for this exact keyboard working on a Mega168. (I have it working with zero hardware mods - but it does require 7 IO pins. My eventual goal with that project was to make a small "piggyback" using a small AVR to interface the keypad into I2C.)

The only problem is that it is written in WinAVR C, and uses direct port addressing and such. So all the ports are referenced by their native name (ex: "PORTB &= ~(1<<PB0)"), and there's a lot of bit twiddling.

Do you want me to post it as is, or clean it up and get it running in the Arduino environment and re-post it?


UPDATE:

I just saw the Arduino Keypad Matrix Library here:
http://www.arduino.cc/playground/Main/KeypadTutorial

Ironically it functions the same way my code does, so if you can't get that working - mine wont help ya. I wish I would have seen that about 4 months ago before I spent all of that time writing my version (of course back then I wasn't "Ardunized", I was more of a pure Atmel guy.

It seems like (with the Arduino), that if you spend enough time looking, that there is already a lirbary in existence to do just about everything.

It almost like Arduino's (and sheilds, etc..) are electronic legos...

make it work for arduino and re-post it ^^ thanks

Lordzeppo - what's wrong with the existing tutorials, here: Arduino Playground - KeypadTutorial and here: PIC Tutorial  Nine - HEX Keypad

?

If you read the second, and understand the contents, the first should be all you need to know about the Arduino. Otherwise, everything is just basic keypad operation.

Now, it would be nice to include a primer on basic keypad operation in the arduino playground, but I have no access to do that (afaik).

!c

the fact is that i'm not a supper duper in C so I ain't able to program it correctly making it work,i'd like to have a simple library,working with the sparkfun keypad wich everybody buy.simple as it,cauz the one we have in the playground is all wired reverse and is not working at all..at least for me..and i'm sure i'm not alone.so a remade keypad library would make a lot of people happy isn'it?