Interfacing 4×4 Keypad with Arduino

Last Updated on March 16, 2024

Many projects needs keypads, the common 4×4 keypad has sixteen keys (hex input). To construct this keypad you need sixteen push buttons and skills to make row and columns between switches.




Nowadays we can get ready made keypads, in this keypad eight female connector placed to interface with other circuits, pins 1 – 4 are rows and pins 5 – 8 are columns.

Keypad-arduino

4×4 Keypad pinout

 

4x4-keypad-matrix

In our case we connecting 4 x 4 keypad with arduino and the sketch code helps to view pressed key from the keypad thru serial monitor.

Arduino 4×4 keypad sketch code

[code]

/*4×4 Matrix Keypad connected to Arduino
www.theorycircuit.com
This code prints the key pressed on the keypad to the serial port
Get keypad library file from : http://playground.arduino.cc/Code/Keypad */
#include <Keypad.h>

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{‘*’, ‘0’, ‘#’, ‘D’}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
Serial.begin(9600);
}

//If key is pressed, this key is stored in ‘keypressed’ variable
//If key is not equal to ‘NO_KEY’, then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}

[/code]




Components List

S.No Name Quantity
1. Arduino uno 1
2. 4×4 keypad 1
3. Connecting wires as required

Leave a Reply

Your email address will not be published. Required fields are marked *