23 Dec 2012

4x4 Keypad Interfacing with Arduino [Program]

Hello Friends! Last week while trying out some odd experiments, I came across a task wherein I was required to do 4x4 keypad matrix interfacing with Arduino UNO microcontroller board.



4x4 numeric keypads are most widely used in numerous mini project which requires the user or the operator to give command to the system or to feed in some data into the embedded system.

In this post, I want to share with you the Arduino code which I made for 4x4 keypad interfacing. The program shared hereby is simple and easy to understand.

If you search more over the internet, then you may find some program involving keypad matrix interfacing using libraries, but problem with those codes are that, if you want to make some modifications in the code, then you are required to 'break in your head' to understand the complete code of the library file. Contrary to those codes, this program does not involve any library and is completely coded by me based on the conventional algorithm which is most commonly found in numerous books and internet sources.

In this program, the row pins are connected to A5, A4, A3, A2 and the column pins are connected to A1, A0, 10, 11. If you are changing the connections, then make sure that you update the changes in the first 8 lines of the the program

The code for 4x4 keypad interfacing with Arduino board is given below:



/*Program demonstrating 4x4 Numeric Keypad interfacing with Arduino UNO
Program Written by: Amit Biswal (Speaking Technology)
URL               : http://amitbiswal.blogspot.com       
*/
int r1=A5;
int r2=A4;
int r3=A3;
int r4=A2;

int c1=A1;
int c2=A0;
int c3=10;
int c4=11;

void setup()
{
  Serial.begin(9600);
  
  pinMode(r1,OUTPUT);
  pinMode(r2,OUTPUT);
  pinMode(r3,OUTPUT);
  pinMode(r4,OUTPUT);

  pinMode(c1,INPUT);
  pinMode(c2,INPUT);
  pinMode(c3,INPUT);
  pinMode(c4,INPUT);
  
}
void loop()
{
  int val;
  //setting the columns as high initially
  digitalWrite(c1,HIGH);
  digitalWrite(c2,HIGH);
  digitalWrite(c3,HIGH);
  digitalWrite(c4,HIGH);
  
  //checking everything one by one
  //case 1: col1 =0 while other col as 1
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  //checking each column for row1 one by one
  if(digitalRead(c1)==0)
  {
    Serial.println("key 1 pressed");
  }
  else if(digitalRead(c2)==0)
  {
    Serial.println("Key 2 pressed");
  }
  else if(digitalRead(c3)==0)
  {
    Serial.println("Key 3 pressed");
  }
  else if(digitalRead(c4)==0)
  {
    Serial.println("Key A pressed");
  }
  
  //case 2: col2 =0 while other col as 1
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,HIGH);
  //checking each column for row1 one by one
  if(digitalRead(c1)==0)
  {
    Serial.println("key 4 pressed");
  }
  else if(digitalRead(c2)==0)
  {
    Serial.println("Key 5 pressed");
  }
  else if(digitalRead(c3)==0)
  {
    Serial.println("Key 6 pressed");
  }
  else if(digitalRead(c4)==0)
  {
    Serial.println("Key B pressed");
  }
  
  //case 3: col3 =0 while other col as 1
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,LOW);
  digitalWrite(r4,HIGH);
  //checking each column for row1 one by one
  if(digitalRead(c1)==0)
  {
    Serial.println("key 7 pressed");
  }
  else if(digitalRead(c2)==0)
  {
    Serial.println("Key 8 pressed");
  }
  else if(digitalRead(c3)==0)
  {
    Serial.println("Key 9 pressed");
  }
  else if(digitalRead(c4)==0)
  {
    Serial.println("Key C pressed");
  }
  
  //case 1: col1 =0 while other col as 1
  digitalWrite(r1,HIGH);
  digitalWrite(r2,HIGH);
  digitalWrite(r3,HIGH);
  digitalWrite(r4,LOW);
  //checking each column for row1 one by one
  if(digitalRead(c1)==0)
  {
    Serial.println("key F pressed");
  }
  else if(digitalRead(c2)==0)
  {
    Serial.println("Key 0 pressed");
  }
  else if(digitalRead(c3)==0)
  {
    Serial.println("Key E pressed");
  }
  else if(digitalRead(c4)==0)
  {
    Serial.println("Key D pressed");
  }
  //giving delay between keypress
  delay(200);
  
}


If you have any suggestions for the improvement of the given code, then do comment below and share the knowledge.
Related Post :


11 comments:

  1. Keep posting such useful and informative articles.
    Thanks.

    ReplyDelete
  2. That is an extremely smartly written article. I will be sure to bookmark it and return to learn extra of your useful information.

    ReplyDelete
  3. keep posting such good and meaningful articles.

    ReplyDelete
  4. Anonymous2/26/2013

    Sir how about if a lcd display is connected

    ReplyDelete
    Replies
    1. LCD library can be used for that

      Delete
  5. What about INPUT_PULL UPs..are they not required since it will float ??

    ReplyDelete
  6. sir can i print the key number in lcd display..and what will be the code for it ?

    ReplyDelete
  7. sir can i show the key in the lcd display...and what will be the code for it ? plz help me.

    ReplyDelete
  8. U have configured pins of C1,C2,C3 and C4 as input and used "digitalwrite" to write output values to those pins.. will it not damage the controller...?

    ReplyDelete
  9. U have configured pins of c1,c2,c3 and c4 as input and used "digitalwrite()" to write output value to those pins.. will it not damage the controller..?

    ReplyDelete

If you liked this blog then Join me at:
Facebook
Twitter
RSS