Table of contents
Sometimes your application requires a keypad to enter some user data. A good example of this is the signal generator. Many Arduino programs use either a 3×4 or 4×4 keypad like show below. Typically the keypad consists of 12 or 16 keys and 7 or 8 connections instead of 12 or 16 respectively. The connections are scanned by the program and if one keys is pressed the row and column connections in the matrix are connected. This way the specific key can be deduced and acted upon.
Example of a 4×4 matrix keypad.
Installing the keypad library
It might be that the keypad library is not installed in your Arduino IDE. If this the case you will get a compiler warning when you try to compile the keypad example below. If so then from the Arduino IDE select Menu | Sketch | Include Library | Manage Libraries … Then type “keypad” next to the Topic ALL to narrow the search result and select the Keypad library by Mark Stanley and Alexander Brevig as there are many keypad libraries available.
Keypad in loop()
Below is an example of a keypad that echoes the pressed key on the USB port.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// Keypad includes #include <Keypad.h> const byte kpRows = 4; // Number of rows on the keypad const byte kpCols = 4; // Number of columns on the keypad // Keypad layout const char kpMap[kpCols][kpRows] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; // Keypad pins byte kpRowPins[kpRows] = {A0, A1, A2, A3}; // Rows 0 to 3 byte kpColPins[kpCols] = {16, 17, 18, 19}; // Columns 0 to 3 Keypad kp = Keypad(makeKeymap(kpMap), kpRowPins, kpColPins, kpCols, kpCols); // RFzero include and object creation #include <RFzero.h> // MUST ALWAYS be included void setup() { SerialUSB.begin(9600); while (!SerialUSB); // Wait for the USB to be ready } void loop() { char key = kp.getKey(); if (key) SerialUSB.print(key); } |
If you haven’t heard about the Arduino yield() function you may even consider including the part of the code in the loop() in the yield() function so the keypad will be scanned more often.
Keypad with “enter”
Instead of just echoing the keys pressed why not wait until an “enter key” has been pressed? On a 4×4 keypads the “enter key” is often the pound sign or number key “#”. The below example collect the incoming keys pressed and when the #-key is pressed the buffer is printed on the USB port. Furthermore, when the “password”is entered a confirmation message is printed. Instead of printing a message the correct sequence could be used to perform some action in the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// Keypad includes #include <Keypad.h> const byte kpRows = 4; // Number of rows on the keypad const byte kpCols = 4; // Number of columns on the keypad // Keypad layout const char kpMap[kpCols][kpRows] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; // Keypad pins byte kpRowPins[kpRows] = {A0, A1, A2, A3}; // Rows 0 to 3 byte kpColPins[kpCols] = {16, 17, 18, 19}; // Columns 0 to 3 Keypad kp = Keypad(makeKeymap(kpMap), kpRowPins, kpColPins, kpCols, kpCols); // RFzero include and object creation #include <RFzero.h> // MUST ALWAYS be included void CollectKeys(const char keyCh) { static int keyBufCount = 0; static char keyBuf[100]; if (keyBufCount < sizeof(keyBuf) - 1) { // End of line found so parse the buffer if (keyCh == '#') { // Pound sign found so print the buffer SerialUSB.println(keyBuf); if (strcmp(keyBuf, "123") == 0) SerialUSB.println("You have entered the password"); keyBufCount = 0; // Clear buffer keyBuf[0] = 0; } else { // Pad buffer with latest char keyBuf[keyBufCount] = keyCh; keyBufCount++; keyBuf[keyBufCount] = 0; } } else { // Overflow so reset buffer keyBufCount = 0; keyBuf[0] = 0; } } void setup() { SerialUSB.begin(9600); while (!SerialUSB); // Wait for the USB to be ready } void loop() { char key = kp.getKey(); if (key) CollectKeys(key); } |