Interfacing hex keypad to arduino

praveen December 5, 2014 5 Comments

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 arduinoRows 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.

Program (Version 1)

int r1=6;
int r2=7;
int r3=8;
int r4=9;
int c1=10;
int c2=11;
int c3=12;
int c4=13;
int colm1;
int colm2;
int colm3;
int colm4;

void setup()
{
  pinMode(r1,OUTPUT);
  pinMode(r2,OUTPUT);
  pinMode(r3,OUTPUT);
  pinMode(r4,OUTPUT);
  pinMode(c1,INPUT);
  pinMode(c2,INPUT);
  pinMode(c3,INPUT);
  pinMode(c4,INPUT);
  Serial.begin(9600);
  digitalWrite(c1,HIGH);
  digitalWrite(c2,HIGH);
  digitalWrite(c3,HIGH);
  digitalWrite(c4,HIGH);
}
void loop()
{
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  if(colm1==LOW)
  {Serial.println("1");
   delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("2");
    delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("3");
     delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("A");
      delay(200);}
   }}}

  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  if(colm1==LOW)
  {Serial.println("4");
    delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("5");
    delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("6");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("B");
       delay(200);}
   }}}

  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  if(colm1==LOW)
  {Serial.println("7");
     delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("8");
       delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("9");
        delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("C");
        delay(200);}
   }}}
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  colm1=digitalRead(c1);
  colm2=digitalRead(c2);
  colm3=digitalRead(c3);
  colm4=digitalRead(c4);
  if(colm1==LOW)
  {Serial.println("*");
      delay(200);}
  else
  {
   if(colm2==LOW)
   {Serial.println("0");
        delay(200);}
   else
   {
   if(colm3==LOW)
   {Serial.println("#");
      delay(200);}
   else
   {
   if(colm4==LOW)
   {Serial.println("D");
       delay(200);}

   }}}

}

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 200ms 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 200mS. Then row 2 is made low and other rows are kept high. The column scanning using if loop-else loop is done again. 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.

 Simpler Version of Above Program (Version 2)

We can easily simplify the program written above with smart usage of Arrays and For Loops.  We are adding a simplified version of the above program (which is half the number of lines of code above). You may compare both codes for understanding the topic.

int row[]={6,7,8,9};// Defining row pins of keypad connected to Aeduino pins
int col[]={10,11,12,13};//Defining 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");
}
Comments
  • Midas Soldaten
    February 9, 2016

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

  • Boulbahaiem Ali
    June 14, 2015

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

  • Boulbahaiem Ali
    June 14, 2015

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

  • April 8, 2015

    “Very nice explanation”

  • David Ford
    May 23, 2014

    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.

Leave a Reply

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