Introduction: Connecting a 4 X 4 Membrane Keypad to an Arduino

Picture of Connecting a 4 X 4 Membrane Keypad to an Arduino

There are a lot of instructions and examples of how to connect a 4 x 3 membrane keypad but I couldn't find instructions on how to connect a 4 x 4 Membrane Keypad to an Arduino.

Step 1: Materials

Picture of Materials

All that is needed for this Instructable is:

  • An Arduino compatible board with 8 free digital pins
  • A 4 x 4 Membrane Keypad

Step 2: Install Keypad Library

Picture of Install Keypad Library

This library is available via the Arduino IDE library manager. If you are using a modern IDE (1.6.2 or above), you can simply use the menu:

Sketch->Include Library->Manage Libraries... Then search for Keypad.

Once found, click on its entry and the install button will appear.

The full instructions for the Keypad library can be found here.

Step 3: Modifying the Example Sketch

In Example-->Keypad the default sketch "HelloKeypad" is set up for a 4 x 3 matrix.

Here is the modified code for the 4 x 4 Keypad:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns char keys[ROWS][COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {9, 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); } }

Step 4: Connecting the Arduino to the Keypad

Picture of Connecting the Arduino to the Keypad

Using the diagram above as a reference the leftmost pin is pin 8 on the keypad and the rightmost is pin 1.

Pins 8, 7, 6, 5 on the keypad should be connected to digital pins 5, 4, 3, 2 on the Arduino respectively.

Pins 4, 3, 2, 1 on the keypad should be connected to digital pins 9, 8, 7, 6 on the Arduino respectively.

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Step 5: Testing

Picture of Testing

Upload the sketch to your Arduino and open the Serial Monitor.

The pressed keys should be displayed as in the window above.

Step 6: Going Further

In Examples-->Keypad there are several examples. All the example sketches can be made to function with the 4 x 4 matrix by changing the following lines of code:

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

Comments

PiotrK71 made it! (author)2017-08-10

On my Arduino Mega, program didn't want to compile immediately. There was a small error in declaring columns and rows and assigning them to pins. Problem solved. Now I'm working on displaying strings correctly on the LCD display 2004.

aqeel2hyder (author)PiotrK712017-09-03

That's really great. i am facing the same problem. can you please share the code?

fsoon (author)2017-08-23

Thanks for sharing.

I modified the code slight to avoid twisting of cables. It still works.

byte rPins[Rows]= {9,8,7,6}; //Rows 0 to 3 connect to digital pins [Pins 8, 7, 6, 5 ]

byte cPins[Cols]= {5,4,3,2}; //Columns 0 to 3 [Pins 4, 3, 2, 1 ]

Code to convert '*' to '.'

if (keypressed=='*')

{

keypressed='.'; // if you want to key in something like 0.2

};

beic (author)2017-06-27

Love your tutorial and I have one question regarding it...

What do I need to do in the code if I want to flip the the Keypad cable/pinout?

Thank you! ;)

ArcAiN6 (author)2016-08-01

This doesn't work for me at all..

When i do this, i press 1, and 1 is shown to be pressed...
I press 2, and it shows me 4 was pressed...
If i press 4 it says 2 was pressed.

It's completely turned around..

The thing is, i'm not sure if it's because your keypad is different, or because mine has a header, and i just plugged it in (pin 1 on the keypad header, to pin D2, and pin 8 on the keypad header, to pin D9 on the pro mini)

However, it appears you have your keypad split into two, and reversed..

My question, why is it i can't simply reverse the order in the sketch and get it to work, rather than having to dig out 8 jumper wires?

ccmakes (author)ArcAiN62017-04-04

The 8-pin header that came with my 4x4 keypad was installed backwards, so 1 was 8 and 8 was 1. Once I realized that, this code is perfect. Might be a common problem. Cheers! Chris.

mtokeyboard (author)2016-07-25

You explained it all very nicely.
so now i understand the library usage, and how to bend it for my 4X4 keypad.

thanks

amberrayh (author)2015-10-06

This is very useful. Thanks for sharing!