Connecting a 4 X 4 Membrane Keypad to an Arduino

Intro: 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

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

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

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

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

Share

Recommendations

  • Plastics Contest

    Plastics Contest
  • Electronics Tips & Tricks Challenge

    Electronics Tips & Tricks Challenge
  • Audio Contest 2018

    Audio Contest 2018

10 Discussions

0
None
shakthiavindu

4 months ago

#include <Keypad.h>

byte rowpin [4] = {2, 3, 4,5};

byte colpin [4] = {6, 7, 8,9};

char keys[4][4] = {

{"1", "2", "3", "A"},

{"4", "5", "6", "B"},

{"7", "8", "9", "C"},

{"*", "0", "#", "D"},

};

Keypad shakthi = Keypad(makeKeymap(keys), rowpin, colpin, 4,4);

void setup() {

Serial.begin(9600);

}

void loop() {

char key = shakthi .getKey();

if (key != NO_KEY) {

Serial.println(key);

}

}

Capture.PNG
1 reply
0
None
tvbox

5 months ago

super nice explanation of the code and connections, but for the wiring you should of also added a Fritzing diagram!

It turned out to wrk fine on my arduino mega 2560 without changin any single thing!

Thanks.

0
None
fsoon

1 year ago

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

};

0
None
beic

1 year ago

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! ;)

0
None
ArcAiN6

2 years ago

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?

1 reply
0
None
ccmakesArcAiN6

Reply 1 year ago

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.

0
None
mtokeyboard

2 years ago

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

thanks

0
None
amberrayh

3 years ago

This is very useful. Thanks for sharing!