Keypads
This page is about very simple matrix keyboards, without built-in diodes or other electronics. The most well-known keypad is a 4 × 4 block with 10 digits and A, B, C and D on it. Another well-known keyboard is the 5 × 4 keypad with 10 digits, two function keys, four-way arrows and Esc(cape) and Ent(er). As an Arduino programmer, you are free to choose the meaning of each key. It is nice if the label corresponds to the actual functions, so change the labels on the keys if necessary.
Keypad with 4 × 4 keys
Because different versions of this keyboard turn out to have different wiring, I still need to adjust the program; hopefully it will come soon. For the time being, look at the 5 × 4 keyboard to see how you can read the keys.
Keypad with 5 × 4 keys
Keypad
This keypad has five rows of four keys. To be able to use the keypad you need 9 pins of the Arduino. The transparent connection cable ("flatcable") of this keypad allows you to see clearly that there is a group of 5 and a group of 4 connections. This corresponds with the rows and columns of the keys. If you want to use the test program unaltered, you must connect the flatcable (from right to left) to pins 2 to 10. If you want to use other pins, you can adjust StartRows and StartColn. In the test program I have used a so-called struct, so that you can easily find out (via a Serial.print(Key.row) or Serial.print(Key.col) in which row or column the pressed key is located.
There are keyboards in which the rows and columns are connected differently. With this test program you can easily find out what that is and if necessary you can change the names (it is often easier to change the names than to change the program or the wiring).
// Test program for 5 x 4 keypad
// c(2018) Th.M. Hupkens
#define Rows 2 // Arduino pin for rightmost row connector
#define Cols 7 // Arduino pin for rightmost col connector
String KeyName[][4] = {
{"F1", "F2", "#", "*"},
{"1", "2", "3", "Up"},
{"4", "5", "6", "Down"},
{"7", "8", "9", "Esc"},
{"Left", "0", "Right", "Enter"}
};
struct KeyType {
byte row;
byte col;
};
KeyType Key;
void setup() {
for (byte row = Rows; row < Rows + 5; row++) {
pinMode(row, OUTPUT);
digitalWrite(row, LOW);
}
for (byte col = Cols; col < Cols + 4; col++) pinMode(col, INPUT_PULLUP);
Serial.begin(9600);
}
void RowHIGH() {
for (byte row = 0; row < 5; row++) digitalWrite(Rows + row, HIGH);
}
bool KeyIn() {
for (byte row = 0; row < 5; row++) {
RowHIGH();
digitalWrite(Rows + row, LOW);
for (byte col = 0; col < 4; col++)
if (digitalRead(Cols + col) == LOW) {
Key.row = row; Key.col = 3 - col;
while (digitalRead(Cols + col) == LOW); // Avoid double keystrokes
return true;
}
}
return false;
}
void loop() {
while (!KeyIn());
Serial.print("Key: "); Serial.println(KeyName[Key.row][Key.col]);
}