How to Read Keypad with Arduino

Keypad is set numeric or alphanumeric characters with a limited number of buttons. The numeric keypad buttons only contain numeric characters, from 0-9, whereas alphanumeric keypad same with numeric keypad but added character alphabet A - D. Both types of keypad is equipped with a special character '*' and '#'. Thus, a numeric keypad will contain 12 characters (12 keys), while the alphanumeric keypad consists of 16 characters (16 keys). This is difference from the keyboard, which is a set of keys with alphanumeric characters (plus special characters) which are more variations according to the ASCII standard.

Readings Keypad with Scanning Technique

Based on description above, it would take 12 or 16 I/O pinouts to handle input from the keypad. However, with the scanning technique, you can save pinouts for keypad reading. The trick is to create a keypad matrix by column and row then read column and row 'coordinate' that are pressed with scanning techniques. Here is an illustration



Based on picture above, then emerge the ‘term’ of keypad ‘3x4’ (3 columns x 4 rows) for numeric keypad, and keypad ‘4x4’ (4 columns x 4 lines) for alphanumeric keypad.

Look again at the picture above. Note that the numeric keypad would only need 7 pin, while the alphanumeric keypad will only need 8 pin only. Quite save pin I / O, isn’t it? It would be helpful if you're using Arduino boards with limited I/O pins such as Arduino Uno, Nano, Mini Pro, etc.

How to Read Keypad with Arduino

To read keypad with Arduino is very easy. Thanks God, because there is provided a special library to handle keypad with Arduino, namely 'keypad.h' library. This library includes 'Hardware Abstraction Library', which is a ‘turn-key’ library, covering all handling functions of basic hardware such as a button, keypad, LED, etc. So, programmers can focus more into programming purposes, no longer build program from the beginning. Hopefully, the HAL libraries can be fast and easy for programmers to build a system.

Tools and materials

1. Arduino Uno
2. 4x4 Keypad
3. Male to Male jumper Cable: 8 pcs


4x4 Keypad– Arduino Circuit

Nex,t connect your keypad (I use a 4x4 keypad) with the Arduino as shown below.


4x4 Keypad – Arduino Sketch Handler

First, download and install this library through the library manager in ‘Sketch-Include Library Manage Library’ menu. Then type 'keypad' in search box then selects 'Keypad' library. Install the library by pressing 'INSTALL' button.



Furthermore, input following sketch to your Arduino. All you need to do is pin adjustment for column and row if using others I/O pins, or if you use another type of keypad (eg 3x4 keypad)

/*
 * keypad 4x4 - Arduino sketch
 * arduinogeek.com
*/
#include <Keypad.h>

const byte ROWS = 4; //4 rows
const byte COLS = 4; //4 columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte colPins[COLS] = {5, 4, 3, 2}; // 2,3,4,5 pins for columns
byte rowPins[ROWS] = {9, 8, 7, 6}; // 6,7,8,9 pins for rows

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);
  }

}

To debug the results, please open your Serial Monitor window through the ‘Tools-Serial Monitor’ menu. Then press any key on your keypad, and note the character will appears in Serial Monitor window.


Keypad is required for applications or systems that require characters inputs but not too many, such as ATM keypad, access doors entries, etc. If using the keyboard instead will more complicated, isn’t it?
Newest
Previous
Next Post »