Connecting a 4 X 4 Membrane Keypad to an Arduino

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

4 People Made This Project!

Recommendations

  • Audio Challenge 2020

    Audio Challenge 2020
  • Maps Challenge

    Maps Challenge
  • Backyard Contest

    Backyard Contest

14 Discussions

1
ollmer
ollmer

1 year ago on Step 4

Why there is no gnd pin?

1
creator8958
creator8958

Reply 7 months ago

the keypad is based on the fact that it recognizes buttons based on which row and column they're in. So basically, the library sets either the rows or the columns (pins 8765 or 4321) as outputs. (refer to the diagram). Then it sets the remaining 4 pins, wether they're the rows or the columns. Subsequently, the library will turn on and off the first 4 pins in a rapid succession. When one of these blinks result in an output, depending on the pin that output is connected to, the library will submit a value to the arduino IDE. For example, if I press 9, with the library code running, it will blink pins 8 and 7, until it gets to the third row, connected to pin 6, resulting in an output at pin 2. The library is then designed to take the knowledge of (pin 6 is fired and pin 2 shows a reaction) and display a 9. To answer your question, however, there really is not necessarily a ground connected, however if you count inputs as the end of the electrical circuit, then either pins (8-5) or (4-1) are your gnd pins. Sorry for long explanation, but hope you can understand how the membrane keypad works :)

0
DavidB552
DavidB552

Question 1 year ago

Question is it hard to mod this code to put 2 key pads I need to have 26 buttons for my project

1
Batool_Khader
Batool_Khader

Question 1 year ago on Step 3

Thank you for this instructable. I have a question regarding the code. Why did you identify the size of the array and the pin numbers to be in bytes?

0
shakthiavindu
shakthiavindu

2 years 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
Arduino_having11_Guy
Arduino_having11_Guy

Reply 1 year ago

Get rid of the last comma in the array

0
tvbox
tvbox

2 years 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
fsoon
fsoon

2 years 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
beic
beic

3 years 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
ArcAiN6
ArcAiN6

3 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?

0
ccmakes
ccmakes

Reply 3 years 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.

1
mtokeyboard
mtokeyboard

3 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
amberrayh
amberrayh

4 years ago

This is very useful. Thanks for sharing!