Overview of 4×4 Matrix Keypad
A keypad is an input device that detects and processes key presses from the user.
A 4×4 keypad consists of 4 rows and 4 columns, with switches positioned between each row and column.
When a key is pressed, it establishes a connection between the specific row and column where the switch is located.
For more information on keypads and how to use them, refer to the “4×4 Keypad” topic in the Sensors and Modules section.
Connection Diagram of 4×4 Keypad with Arduino UNO
Reading a 4×4 Keypad with Arduino Uno
In this setup, we’ll read the key pressed on a 4×4 keypad and display it on the Arduino serial terminal.
We will use the Keypad library by Mark Stanley and Alexander Brevig.
Download the library from here, extract it, and add it to the Arduino IDE’s libraries folder.
For details on adding custom libraries and using example sketches, refer to “Adding Library to Arduino IDE” in the Basics section. You can also find information about directly importing the Keypad library into the Arduino IDE here.
After adding the library, open the Arduino IDE and load the “CustomKeypad” example sketch from the Keypad library.
Caution:
In the example sketch, digital pins 0 and 1 are assigned to keypad rows. However, on the Arduino Uno, pins 0 and 1 are used for serial communication (Tx and Rx). Since serial communication is required to display the pressed key on the serial terminal, using these pins may cause conflicts and incorrect behavior.
To avoid this, use pins other than 0 and 1 for the keypad rows. The example sketch below demonstrates how to do this.
Note:
byte rowPins[ROWS] = {R1, R2, R3, R4}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {C1, C2, C3, C4}; // Connect to the column pinouts of the keypad
Make sure to connect the pins according to this setup. Otherwise, key identification may not match the layout you’ve defined for the keypad.
4×4 Keypad Code for Arduino Uno
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
const byte ROWS = 4; /* four rows */
const byte COLS = 4; /* four columns */
/* define the symbols on the buttons of the keypads */
char hexaKeys[ROWS][COLS] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
byte rowPins[ROWS] = {10, 11, 12, 13}; /* connect to the row pinouts of the keypad */
byte colPins[COLS] = {6, 7, 8, 9}; /* connect to the column pinouts of the keypad */
/* initialize an instance of class NewKeypad */
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}
}
Functions Used
1. makeKeymap(keys)
This function initializes the internal keymap to match the user-defined keymap, as specified in the keys
function syntax above.
2. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols)
This defines and initializes an object called customKeypad
from the Keypad
class. The rowPins
and colPins
are the Arduino pins to which the rows and columns of the keypad are connected. The rows
and cols
represent the number of rows and columns on the keypad.
3. customKeypad.getKey()
This function is used to detect which key is pressed on the keypad.
1-Wire interfacing of Keypad With Arduino
The interfacing shown above uses 8 GPIO pins of the Arduino board. However, by utilizing the 1-wire interfacing method, we can achieve the same result with just 1 GPIO pin.
This method employs a simple voltage divider concept to generate different voltages for each key press.
Now, let’s explore how to interface a keypad with the Arduino using a single GPIO pin.
Connection Diagram of 1-Wire Keypad Interface With Arduino
1-Wire Interfacing of Keypad with Arduino
One Wire Keypad Code for Arduino Uno
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
int adc_val;
adc_val = analogRead(A1); /* Read input from keypad */
if (adc_val>850)
{
Serial.print("Key Pressed : ");
Serial.println("0");
delay(100);
}
else if ( adc_val>450 && adc_val<510)
{
Serial.print("Key Pressed : ");
Serial.println("1");
delay(100);
}
else if ( adc_val>300 && adc_val<350)
{
Serial.print("Key Pressed : ");
Serial.println("2");
delay(100);
}
else if ( adc_val>230 && adc_val<270)
{
Serial.print("Key Pressed : ");
Serial.println("3");
delay(100);
}
else if ( adc_val>160 && adc_val<180)
{
Serial.print("Key Pressed : ");
Serial.println("4");
delay(100);
}
else if ( adc_val>145 && adc_val<155)
{
Serial.print("Key Pressed : ");
Serial.println("5");
delay(100);
}
else if ( adc_val>125 && adc_val<135)
{
Serial.print("Key Pressed : ");
Serial.println("6");
delay(100);
}
else if ( adc_val>105 && adc_val<120)
{
Serial.print("Key Pressed : ");
Serial.println("7");
delay(100);
}
else if ( adc_val>92 && adc_val<99)
{
Serial.print("Key Pressed : ");
Serial.println("8");
delay(100);
}
else if ( adc_val>82 && adc_val<90)
{
Serial.print("Key Pressed : ");
Serial.println("9");
delay(100);
}
else if ( adc_val>77 && adc_val<81)
{
Serial.print("Key Pressed : ");
Serial.println("A");
delay(100);
}
else if ( adc_val>72 && adc_val<76)
{
Serial.print("Key Pressed : ");
Serial.println("B");
delay(100);
}
else if ( adc_val>63 && adc_val<68)
{
Serial.print("Key Pressed : ");
Serial.println("C");
delay(100);
}
else if ( adc_val>60 && adc_val<62)
{
Serial.print("Key Pressed : ");
Serial.println("D");
delay(100);
}
else if ( adc_val>57 && adc_val<59)
{
Serial.print("Key Pressed : ");
Serial.println("E");
delay(100);
}
else if( adc_val>52 && adc_val<56)
{
Serial.print("Key Pressed : ");
Serial.println("F");
delay(100);
}
else
{
}
delay(100);
}