Interfacing of Arduino with 4×4 Keypad

int row[]={11,10,9,8};
int col[]={5,4,3,2};
int i,j;
int col_scan;
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");
}


This program send the keypad char to serial port for easily view which key on the keypad is pressed.

It is also good choice to show the key pressed on the 16x2 LCD display. The below program send the keypad char to LCD display for easily view which key on the keypad is pressed.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, A0, A1, A2, A3);

int row[]={11,10,9,8};
int col[]={5,4,3,2};
int i,j;
int col_scan;
char charpress;
void setup()
{
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Welcome...");

  Serial.begin(9600);
  Serial.println("Welcome");
  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);
                lcd.print(charpress);
                delay(300);
            }
        }
    }
}
void keypress(int i, int j)
{
    if(i==0&&j==0)
      charpress='1';
    if(i==0&&j==1)
      charpress='2';
    if(i==0&&j==2)
      charpress='3';
    if(i==0&&j==3)
      charpress='A';
    if(i==1&&j==0)
      charpress='4';
    if(i==1&&j==1)
      charpress='5';
    if(i==1&&j==2)
      charpress='6';
    if(i==1&&j==3)
      charpress='B';
    if(i==2&&j==0)
      charpress='7';
    if(i==2&&j==1)
      charpress='8';
    if(i==2&&j==2)
      charpress='9';
    if(i==2&&j==3)
      charpress='C';
    if(i==3&&j==0)
      charpress='*';
    if(i==3&&j==1)
      charpress='0';
    if(i==3&&j==2)
      charpress='#';
    if(i==3&&j==3)
      charpress='D';
}


This program send the keypad char to 16x2 LCD display. In this program the keypress() function uses the manual method to convert i.j (row,column) to key code. It is also possible to use a two dimensional array to convert the i,j press the key code.

The below program send the keypad char to LCD display but the conversion of i,j to key code is implemented with the two dimensional array (hexaKeys).

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, A0, A1, A2, A3);

char hexaKeys[4][4] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};


int row[]={11,10,9,8};
int col[]={5,4,3,2};
int i,j;
int col_scan;
char charpress;
void setup()
{
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Welcome...");

  Serial.begin(9600);
  Serial.println("Welcome");
  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)
            {
                
                charpress=hexaKeys[i][j];
                lcd.print(charpress);
                delay(300);
            }
        }
    }
}



You can leave a response, or trackback from your own site.

3 Responses to “Interfacing of Arduino with 4×4 Keypad”

  1. Ileana Sarks says:

    Thanks for one’s marvelous posting! I actually enjoyed reading it, you could be a great author.I will ensure that I bookmark your blog and will come back in the future. I want to encourage you to definitely continue your great work, have a nice weekend!

  2. I genuinely value your piece of work, Great post.

Leave a Reply to the article



Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!