Thursday 28 May 2015

Interfacing Keypad with Arduino

Keypads Play Vital role in Embedded systems as Human Machine Interface devices.
Interfacing Keypad with arduino very easy.. We can find many tutorials on the web explaining how to do it and many of them are based on the Arduino Keypad library and we can find the sketch for our task....

like many arduino People the only language i know is Arduino Language and I don't want to use that library for simple Keypad interface which involves 12 switches (4 rows and 3 cols) cause i don't understand a bit in it. So i try to write a little code without library.

Basically Keypads as consist of Buttons which we can read the state of button with Arduino Digital Pin. when there are lot of inputs to read, it is not feasible too allocate one pin to each one of them. In this situations matrix keypad is useful.

if you consider reading a 25 inputs we can make 5*5 matrix which can implemented using 10 digital pins.

Internal Arrangement of Matrix Keypad is shown in fig.
Initially all switches are assumed to be released and there is no connection between Rows and Columns. when anyone of the switches are pressed the corresponding row and column is short circuited.... using this logic the button press can be detected.

How to write a Program to find the key pressed ?

The One of the technique to Identify the pressed keys are the method called Column Searching.
In This method particular Row is kept low and other rows are held High and logic state of each column line is scanned.

If a Particular is found to be having a state Low then that means that the key coming from in between that column and row.

steps to implement the program :

1.Declare the Row pins and column pins
2.initialize the key strokes with reference to the rows and cols as multidimensional array
3.set pin mode of Row pins to OUTPUTS
4.set pin mode of Column pins as inputs (remember this method is called column searching)
5.using for loop make one row pin Low each time and read the all column pins
6.if digitalRead returns low on col pins find the key using respective row and col numebrs.

The schematic of connections in proteus is

Arduino Code is:
/*
Keypad Interfacing with Arduino Without Keypad Library
 Author : Kunchala Anil
 It is implemented using a method called Column Searching
 */

const int row_pin[] = {
  5,4,3,2}; 
const int col_pin[] = {
  6,7,8}; // defining row and column pins as integer arrays

const int rows = 4, cols = 3; //defining the multi dimensional array size constants 

const char key[rows][cols] = {               // defining characters //for keystrokes in Multidimensional Array
  {
    '1','2','3'  }
  ,   
  {
    '4','5','6'  }
  ,
  {
    '7','8','9'  }
  ,
  {
    '*','0','#'  }  
};

void setup(){

  Serial.begin(9600); //begin the serial communication

  for(int i = 0; i<4; i++)
  {
    pinMode(row_pin[i],OUTPUT); //Configuring row_pins as Output Pins
    digitalWrite(row_pin[i],HIGH);//write HIGH to all row pins

    if(i<3)//we only have 3 columns
    {
      pinMode(col_pin[i],INPUT_PULLUP);//configure column pin as Input and activate internal //Pullup resistor
    }//end of if

  }//end of for loop

}//end of setup


void loop(){
  char key = read_key();
  if(key !='\n'){
    Serial.println(key);
    delay(100);
  }
}//end of loop

char read_key(){
  for(int row = 0;row < 4;row++)
  {
    digitalWrite(row_pin[0],HIGH);
    digitalWrite(row_pin[1],HIGH);
    digitalWrite(row_pin[2],HIGH);
    digitalWrite(row_pin[3],HIGH);
    digitalWrite(row_pin[row],LOW);
    //Serial.println(row_pin[row]);

    for(int col = 0;col<3;col++)
    {
      int col_state = digitalRead(col_pin[col]);
      if(col_state == LOW)
      {
        return key[row][col];
      }//end of if 
    }//end of col for loop
  }//end of row for loop
  return '\n';
}//end of read_key

Note : delay(100) is used to eliminate debounce 




you can see rows are changing the HIGH to LOW and and Low at column side when key is pressed.


No comments:

Post a Comment