How to Read Keypad with Arduino without Library

In this arduino electronics tutorial you will learn how to interface and read keypad with Arduino without library. In Arduino IDE you can find many keypad libraries such as Keypad.h which makes programming easier. The keypad library hides the programming details underneath it and sometimes you want to know how the keypad scanning algorithm with code works. So here we illustrate program code without library that reads 4x3 keypad and displays the pressed key on serial monitor with Arduino.














Interfacing 4x3 Keypad with Arduino

The schematic diagram of interfacing 4x3 keypad with Arduino is as shown below.

schematic of 4x3 keypad interfacing with Arduino

Before picture shows interfacing of the 4x3 keypad with Arduino on a breadboard.



Here we have connected the four rows R1, R2, R3 and R4 of the keypad to the digital pins 8, 7, 6 and 5 respectively. We connected the three columns C1, C2 and C3 to the digital pins 4, 3 and 2 respectively.





Programming & Coding

The keypad arduino code without library is below.
// Keypad Scanning without Library
// ee-diary.blogspot.com

const char rows = 4; // set display to four rows
const char cols = 3; // set display to three columns

const char keys[rows][cols] = {
              {'1','2','3'},
               {'4','5','6'},
               {'7','8','9'},
               {'*','0','#'}
               };
               
char rowPins[rows] = {8, 7, 6, 5};
char colPins[cols] = {4, 3, 2};

void setup () {
      Serial.begin(9600);
      
   for(char r = 0; r < rows; r++){
         pinMode(rowPins[r], INPUT);    //set the row pins as input
         digitalWrite(rowPins[r], HIGH);    //turn on the pullups
   }
   
   for(char c = 0; c < cols; c++){
         pinMode(colPins[c], OUTPUT);   //set the column pins as output
   }
   
}

void loop() {
      char key = getKey();
      
      if(key != 0){
   Serial.println(key);
      }
}

char getKey(){
      char k = 0;
      
      for(char c = 0; c < cols; c++){
        digitalWrite(colPins[c], LOW);
         for(char r = 0; r < rows; r++){
            if(digitalRead(rowPins[r]) == LOW){
            delay(20);    //20ms debounce time
            while(digitalRead(rowPins[r])== LOW);
            k = keys[r][c];
            }
         }
   digitalWrite(colPins[c], HIGH); 
      }
      return k;
}

Code explanation:

In the above program code we have first declared some constants such as the number of rows, number of columns, keypad 4 by 3 array corresponding to the keypad digits and arrays which maps the digital pin connected to rows and columns. 

In the setup() function, we have setup the serial port @9600 baud rate for displaying the key pressed via Arduino IDE serial monitor using the Serial.begin() function. Then we have set up the rows and column ports. First we have made the four rows inputs and enabled the pullup resistors so that the rows are initially in High state using a for loop. Similarly, we set up the three columns as output using again a for loop.

In the loop() function we use the getKey() function(explained later) to scan and get the key pressed. Then we output the key pressed onto the serial monitor using the Serial.println() function.

The important function in this program code is the getKey() function. The getKey() function is the one which scans and detects which key is pressed and returns the key that is pressed. In the function getKey(), using a for loop, the three columns are sequentially made low(set low using the digitalWrite())function). For each column that is currently made low, we read the state of the four rows using a for loop. If anyone of the row being read is low then we wait for 20ms(debounce time), wait for that row to go back to high state using the while() function for checking the state of that row. Once that row goes high we pass that particular row and that particular column to the 2-D keys[][] array and save it as the key pressed in a variable k. Finally before returning our detected k from the getKey() function we make the column high again using the digitalWrite() function.

Once you upload the sketch to Arduino, you should open the serial monitor. Then press any key on the keypad and the pressed key will be shown on the serial monitor.




If you want to learn how to use keypad with arduino library see the post Arduino Keypad and LCD Interfacing with Code.
 
Recommended Tutorials 

6 Comments

Previous Post Next Post