Friday 15 May 2015

Arduino Lesson 11 : Interfacing 4*4 Hex Keypad with Arduino UNO

Intro to topic :

Keyboard or in this case keypad is very essential for many types of user data entry like passwords,number, or data integers so it is important to know about interfacing of keypad with arduino and manipulating it. Here i am discussing that how to interface keypad with arduino UNO.

Hardware required :

  1. Arduino UNO board.
  2. Hex keypad 4*4
  3. Connecting wires/Jumper wires.
  4. PC to configure arduino board (with arduino software installed).
  5. Bread board.


Steps to follow :


  1. Connect the Keypad according to image.
  2. Use equal length jumper wires for ease.
  3. After this connect the board to PC by USB cable and then open up the IDE in PC
  4. Then upload the following Code.



Code #1 to be uploaded :

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

                                                 OR

Code #2 to be uploaded :

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

   }}}

}

After the successful uploading of program goto PC and open up serial monitor and the press any key what ever you will press automatically comes on screen of PC in serial monitor.


Notice that this code can be used to display up characters on the screen of LCD we have covered in last Lesson.

No comments:

Post a Comment