Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Using a membrane keypad with an Arduino is common for password locks, calculators, menus, and control panels. In this post, we will learn how to use Keypads in Arduino projects.
Keypads are made up of multiple push buttons connected in the form of a matrix. A 3X4 keypad has 4 rows and 3 columns, while a 4X4 keypad has 4 rows and 4 columns. These keypads do not require power supply to operate.
The schematic of a 4×4 keypad illustrates how the rows and columns are internally connected:
The schematic for a 4X4 keypad by circuitbasics
When a button is pressed, one row and one column are connected, allowing current to flow between the corresponding row pin and column pin.
Keypad pins include row pins and column pins.
3×4 keypad pins (from left to right): Row1, Row2, Row3, Row4, Column1, Column2, Column3. 4×4 keypad pins (from left to right): Row1, Row2, Row3, Row4, Column1, Column2, Column3, Column4.
To detect a press, rows are usually configured as OUTPUT and columns as INPUT. When no buttons are pressed, the column pins are held HIGH, and the row pins are held LOW.
When a button is pressed, the corresponding column pin is set to LOW, allowing the Arduino to detect which column contains the pressed button.
The controller then activates each row one at a time by setting the row pin to HIGH. At the same time, it checks whether the column pin returns to HIGH. If the column pin remains LOW, the controller moves on to the next row.
When the column pin goes HIGH again, the Arduino identifies the active row connected to the pressed button. Finally, all row pins are set back to LOW.
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
// Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROWS and COLS to Arduino pins
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Create keypad object
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop() {
char key = kpd.getKey();
if (key) {
Serial.println(key);
}
}
If you want the keypad to only input numbers, use the code segment below.
void loop()
{
char key = kpd.getKey();
if (key >= '0' && key <= '9'){ //only act on numeric keys
Serial.println(key);
}
}
We are now printing a character rather than an actual number. To convert a character to an integer, we can use its ASCII value. For example, the character '3' has the ASCII value 51, and '0' has the ASCII value 48. By subtracting the ASCII value of '0' (48) from a numeric character, we can obtain its corresponding integer value.
void loop()
{
char key = kpd.getKey();
if (key >= '0' && key <= '9'){ //only act on numeric keys
int digit = key - '0'; // Converts '3' to 3
Serial.println(digit);
}
}
For an example project using the keypad, visit Automatic Pet Feeder.