Interfacing hex keypad to arduino uno.

This article is about how to interface a hex keypad to arduino. Hex keypad is a very important component in embedded systems and the typical applications are code locks, calculators, automation systems or simply any thing that requires a character  or numeric input. This project will display the pressed key in the serial monitor window of the arduino IDE. The same project can be modified to display the pressed key on 7-segment LED display or an LCD display. Before going into the details, have a look at the hex keypad.

Hex keypad.

Hex key pad is simply an arrangement 0f 16 push button switches in a 4X4 matrix form. Typically a hex keypad will have keys for number 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and letters A, B, C, D, *, #. The hex keypad will have 8 connection wires namely R1, R2, R3, R4 and C1, C2, C3, C4 representing the rows and columns respectively. The schematic diagram and photo of a typical hex keypad is shown in the figure below.

hex keypad photo


The program identifies the pressed key by a method called column scanning. In this method a particular row is kept low and other rows are held high. The the logic status of each column line is scanned. If a particular column is found low, then that means the key that comes in between that column and row is short(pressed). Then the program registers that key being pressed. Then the same procedure is applied for the subsequent rows and the entire process is repeated. For example if row 1 is kept low and column 1 is found low during scanning, then that means key”1″ is pressed. Full circuit diagram of interfacing hex keypad is shown below.

Circuit diagram.

interfacing hex keypad and arduino

Rows R1, R2, R3 and R4 are interfaced to digital pins 6, 7, 8 and 9 pins of the arduino respectively. Columns C1, C2, C3 and C4 are interfaced to the digital pins 10, 11, 12, 13 of the arduino. The arduino is connected to PC through the USB port. The circuit is powered from the USB itself and no external power supply is needed.  The full program for interfacing hex keypad to arduino is given below. 

Arduino Program – Interfacing Hex Keypad (4×4)

int row[]={6,7,8,9};// Defining array-row pins of keypad connected to Arduino pins
int col[]={10,11,12,13};//Defining array-column pins of keypad connected to Arduino
int i,j; // Two counter variables to count inside for loop
int col_scan; // Variable to hold value of scanned columns
void setup()
{
Serial.begin(9600);
for(i=0;i<=3;i++)
{
pinMode(row[i],OUTPUT);
pinMode(col[i],INPUT);
digitalWrite(col[i],HIGH);
} }
void loop()
{ 
for(i=0; i<=3; i++)
{
digitalWrite(row[0],HIGH);
digitalWrite(row[1],HIGH);
digitalWrite(row[2],HIGH);
digitalWrite(row[3],HIGH);
digitalWrite(row[i],LOW);
for(j=0; j<=3; j++)
{
col_scan=digitalRead(col[j]);
if(col_scan==LOW)
{
keypress(i,j);
delay(300);
}}
}}
void keypress(int i, int j)
{
if(i==0&&j==0)
Serial.println("1");
if(i==0&&j==1)
Serial.println("2");
if(i==0&&j==2)
Serial.println("3");
if(i==0&&j==3)
Serial.println("A");
if(i==1&&j==0)
Serial.println("4");
if(i==1&&j==1)
Serial.println("5");
if(i==1&&j==2)
Serial.println("6");
if(i==1&&j==3)
Serial.println("B");
if(i==2&&j==0)
Serial.println("7");
if(i==2&&j==1)
Serial.println("8");
if(i==2&&j==2)
Serial.println("9");
if(i==2&&j==3)
Serial.println("C");
if(i==3&&j==0)
Serial.println("*");
if(i==3&&j==1)
Serial.println("0");
if(i==3&&j==2)
Serial.println("#");
if(i==3&&j==3)
Serial.println("D");
}

About the program.

Code Serial.begin(9600); sets the baud rate for serial communication at 9600. Baud rate is the number of signal changes happening in a second in a digitally modulated transmission line. Firstly row1 is held high and other rows are held low using digitalWrite command. Then the status of each column is read using digitalRead command. Then each column is checked for low using an if-else loop. If a particular column in found low, the key intersecting that column and row1 is assumed to be pressed and the name of that key is displayed on the serial monitor window using Serial.print command. A delay of 300ms is given between each condition checking loops in order to avoid multiple key registering during a single key press. If this delay is increased further more the response of the keypad will be reduced. After some trial and error, I found the optimum value for my scheme to be 300mS. Then row 2 is made low and other rows are kept high. Then the same thing is done for row 3 and then for row 4. The the entire cycle is repeated over time. The result will be the name of the pressed key displayed on the serial monitor any time.

Author

5 Comments

  1. Midas Soldaten

    Thanks to Boulbahaiem Ali,
    These are nice shares with 3 step-by-step cost down programming codes. for the latest code one based upon your code explain in the sequences from ROW(Output) to COL(Input),the main loop should be changed to below for beginning staff :

    void loop()
    {
    for (int row = 0; row<=3;row++)
    {
    for (int col = 0; col<=3;col++)
    {
    digitalWrite(ArduinoPins_Row_OUTPUT[row],LOW);
    if (digitalRead(ArduinoPins_Col_INPUT[col]) == LOW)
    {
    Serial.println(Matrix[row] [col]);
    delay(200);
    }
    digitalWrite(ArduinoPins_Row_OUTPUT[row],HIGH);
    }
    }

  2. Boulbahaiem Ali

    int ArduinoPins_Row_OUTPUT[] = {6,7,8,9};
    int ArduinoPins_Col_INPUT[] = {10,11,12,13};
    char Matrix[4][4]={{‘1′,’2′,’3′,’A’},{‘4′,’5′,’6′,’B’},{‘7′,’8′,’9′,’C’},{‘*’,’0′,’#’,’D’}};
    void setup()
    {
    Serial.begin(9600);
    for (int i = 0; i<=3;i++)
    {
    pinMode(ArduinoPins_Row_OUTPUT[i],OUTPUT);
    digitalWrite(ArduinoPins_Row_OUTPUT[i],HIGH);
    pinMode(ArduinoPins_Col_INPUT[i],INPUT);
    digitalWrite(ArduinoPins_Col_INPUT[i],HIGH);//pull up resistor
    }
    }
    void loop()
    {
    for (int row = 0; row<=3;row++)
    {
    for (int col = 0; col<=3;col++)
    {
    digitalWrite(ArduinoPins_Row_OUTPUT[col],LOW);
    if (digitalRead(ArduinoPins_Col_INPUT[row]) == LOW)
    {
    Serial.println(Matrix[col] [row]);
    delay(200);
    }
    digitalWrite(ArduinoPins_Row_OUTPUT[col],HIGH);
    }
    }
    }

  3. Boulbahaiem Ali

    int ArduinoPins_Row_OUTPUT[] = {6,7,8,9};
    int ArduinoPins_Col_INPUT[] = {10,11,12,13};
    char Row_1_Data[] = {‘1′,’2′,’3′,’A’};
    char Row_2_Data[] = {‘4′,’5′,’6′,’B’};
    char Row_3_Data[] = {‘7′,’8′,’9′,’C’};
    char Row_4_Data[] = {‘*’,’0′,’#’,’D’};
    char *Matrix[] ={Row_1_Data,Row_2_Data,Row_3_Data,Row_4_Data} ;
    void setup()
    {
    Serial.begin(9600);
    for (int i = 0; i<=3;i++)
    {
    pinMode(ArduinoPins_Row_OUTPUT[i],OUTPUT);
    digitalWrite(ArduinoPins_Row_OUTPUT[i],HIGH);
    pinMode(ArduinoPins_Col_INPUT[i],INPUT);
    digitalWrite(ArduinoPins_Col_INPUT[i],HIGH);//pull up resistor
    }
    }
    void loop()
    {
    for (int row = 0; row<=3;row++)
    {
    for (int col = 0; col<=3;col++)
    {
    digitalWrite(ArduinoPins_Row_OUTPUT[col],LOW);
    if (digitalRead(ArduinoPins_Col_INPUT[row]) == LOW)
    {
    Serial.println(Matrix[col] [row]);
    delay(200);
    }
    digitalWrite(ArduinoPins_Row_OUTPUT[col],HIGH);
    }
    }
    }

  4. David Ford

    What you have been publishing is good material but I am needing something a little different and from comments I have seen, my problem is experienced by a lot of people. I want to store and read data from an SD card – mainly from a VC0706 camera module but the data source doesn’t matter. Any example code I have been able to download does not compile. There are always error messages (something not defined in some scope) that indicate there is something wrong with the Arduino libraries for the SD card. If you could publish something that works I think a lot of people would appreciate it.