loading

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
<p>This doesn't work for me at all.. <br><br>When i do this, i press 1, and 1 is shown to be pressed... <br>I press 2, and it shows me 4 was pressed... <br>If i press 4 it says 2 was pressed. <br><br>It's completely turned around.. <br><br>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)<br><br>However, it appears you have your keypad split into two, and reversed.. <br><br>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?<br></p>
<p>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.</p>
<p>You explained it all very nicely. <br>so now i understand the library usage, and how to bend it for my 4X4 keypad.</p><p>thanks</p>
<p>This is very useful. Thanks for sharing!</p>

About This Instructable

13,363views

33favorites

License:

More by CRCibernetica:Como Usar El MIT App Inventor Con El Itead BLE Shield Como conectar el SONOFF WiFi Switch a io.adafruit.com via MQTT Como usar una Printrbot Play de Inventoría 
Add instructable to: