Matrix Keypad library
Last update June 10, 2009
The Keypad library allows your Arduino to read a matrix type keypad. You can scavenge these keypads from old telephones or you can get them from almost any electronics parts store for less than $5 USD. They come in 3x4, 4x4 and various other configurations with words, letters and numbers written on the keys. This library is capable of supporting all of those.

First you need to get a piece of paper and draw the right hand diagram as you see it below. I've already written my pin numbers (1,2,3 across the bottom and 7,6,5,4 down the right side) which you can just leave off of your drawing. Next, you are going to use your Ohm meter to find out which pins are connected to which keys. The first thing to do is count how many pins are on your keypad (as seen in the photo below.) The photo is showing 14 pins though not all of the pins are used. Don't worry, the unused pins will just be ignored.

/* Keypadtest.pde
*
* Demonstrate the simplest use of the keypad library.
*
* The first step is to connect your keypad to the
* Arduino using the pin numbers listed below in
* rowPins[] and colPins[]. If you want to use different
* pins then you can change the numbers below to
* match your setup.
*
* Note: Make sure to use pullup resistors on each of
* the rowPins.
*/
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}
void loop()
{
char key = kpd.getKey();
if(key) // same as if(key != NO_KEY)
{
switch (key)
{
case '*':
digitalWrite(ledpin, LOW);
break;
case '#':
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
}
}
}
Make sure you are using pull-up resistors on the row pins of your keypad.
You can pretty much connect your keypad to any pins you would like. Be careful not to use the serial pins if you are using them for communication.
If key presses take a long time to register then you need to make sure that you are not using long delay()'s in your code.
Make sure you understand the pin mappings and have the keypad wired up to match. If you did wire the pins incorrectly you may be able to redefine the pins and/or keymap to make your keypad work.
The library supports user defined pins and keymaps so it should not be necessary to change the library. However, if you do then you should delete the library's .o file after any changes so that the library will be properly recompiled.