Tired of using normal push buttons as input to your system? You should consider including a matrix keypad. Matrix keypad is widely use in our daily life. Often, matrix keypad is available in 3×4 or 4×4. For your information, matrix keypad is a good substitution to normal push button. Matrix keypad offers more input to the microcontroller with lesser I/O pins required as compared to buttons. Consider your system needs 16 inputs, you requires only 8 I/O pins with keypad instead of 16 I/O pins. So you can actually use the extra 8 pins for other functions.

images (1)
Membrane type 4×4 Matrix Keypad
4×4 Matrix Keypad

Still unclear? Let us look at how a 4×4 matrix keypad works.

keypad 5
Mapping of buttons with rows and columns

A 4×4 matrix keypads consists of 4 rows and 4 columns. This is roughly how the keypads looks like:

keypad 1

There is a switch connecting each row and column. So the combinations of rows and columns makes up the 16 inputs.

So, try to imagine this. Initially all the switch are open (not connected). When you pressed either one buttons, the switch is now closed (connected). As you can see, now there is a connection between the row and column.

Okay, now we know how a matrix keypad works, but how do we relate it with our microcontroller? So we connect the first 4 pins to the column as INPUT. The other 4 pins is connected to the row as OUTPUT. The input meant that is the input to the microcontroller while output is the output from the microcontroller. Note that the input to the microcontroller has to connect to pull high resistor, or you can use the internal pull up from Arduino itself.

keypad 3

Here comes the magic, the microcontroller send LOW to each row one at a time and check whether there is a LOW signal detected on the column. If there is no button pressed, the microcontroller will scan for the next row and read for LOW signal. Since the column is pull HIGH internally, so no button pressed would return 1 to the microcontroller. When you pressed one of the button, now the row and column are connected. The 0 from row would make the column return a 0 to microcontroller.

keypad 2

With the row and column, you can know which button you are pressing. Amazing.

Since the connections of a keypad is quite easy, I have actually made myself a DIY keypad with DIY PCB method instead of purchasing one. 🙂


Now let us look on how to interface with the hardware before moving on the the software part. For a 4×4 matrix keypad, usually it has 8 pins on it. How do we know which pins are associate to which rows and columns? A digital multimeter (DMM) would ease your job.

Turn your DMM to connectivity mode. Now put one of your meter probe to the first pin and the other one to the fifth pin. Try to press the button and see which button would gives you the “beep” sound. Next, try to probe to the sixth pin and press other button. Keep trying and you would understand the connection behind the keypad.

Finally we have reached to the software part. To use a keypad with Arduino, its pretty simple with the help of  Keypad.h library. You can download the library at here.

Note that when you are using this library, you don’t need to connect any external resistor as it has been internally pull high in Arduino.

Hardware connection with Arduino:

keypad 6

keypad 4

Example code:

#include <Keypad.h>

char keys[4][4]={

byte rowPin[4]={5,6,7,8};
byte colPin[4]={9,10,11,12};

Keypad keypad=Keypad(makeKeymap(keys),rowPin,colPin,4,4);

void setup()

void loop()
 char pressed=keypad.getKey();

Code explanation:

char keys[no_of_row][no_of_col]

– Define the keymap

byte rowPin[no_of_row]={pins}

– connect keypad row pins to the defined pins in {}

byte colPin[no_of_col]={pins}

– connect keypad column pins to the defined pins in {}

Keypad keypad=Keypad(makeKeymap(keys),rowPin,colPin,no_of_row,no_of_col)

– create the keypad


– check for the button pressed and return as define in keymap

Video demonstration:


PCB Heaven, How Key Matrices Works