Connecting a 4 X 4 Membrane Keypad to an Arduino
by CRCibernetica in Circuits > Arduino
111421 Views, 48 Favorites, 0 Comments
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.
Materials
All that is needed for this Instructable is:
- An Arduino compatible board with 8 free digital pins
- A 4 x 4 Membrane Keypad
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.
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); } }
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
Testing
Upload the sketch to your Arduino and open the Serial Monitor.
The pressed keys should be displayed as in the window above.
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