# How to use a keypad with an arduino

In some application we need to introduce some data(like some number values, PIN etc), so for that in this example we will connect a keypad 4×4(16 digits) to an arduino uno.

In this projects first we will understand how a keypad work and after, we have uploaded a small program with an 20×4 LCD and  a control with PIN aplication.

The keypad we use is like in the picture below: It has eight pins and starting from left they are :

1-row 1(123A)

2-row 2(456B)

3-row3(789C)

4-row 4(*0#D)

5-column 1(147*)

6-column 2(2580)

7- column 3(369#)

8-column 4(ABCD)

So how it works ???

Every row and every column it has a connection from a normal open contact(a digit). When you push a button, for example  5 between row 2 and column 2 the normal open contact has closed. If you have row 2 LOW state and for column 2 HIGH state, when you push the button the column 2 it will be LOW, so you will know is button 5. To know is button 5 you must create a “map” to connection on the arduino (which pin from arduino is connected to a specified pin on the keypad). For our project we have

row 1- pin 9

row2-pin 8

row3-pin 7

row4-pin 6

column1-pin 5

column2-pin 4

column3-pin 3

column 4-pin 2

First of all all in the beginning of the program all pins above had the HIGH state. Then row 1 it will be LOW and if checking every column one by one, one it has a LOW state then the button correspondig to row 1 and the  column LOW is pushed.

With these program we cannot introduce a value, it is for understand how a keypad work.

The program is:()

const int row1 = 9;

const int row2 = 8;

const int row3 = 7;

const int row4 = 6;

const int col1 = 5;

const int col2 = 4;

const int col3 = 3;

const int col4 = 2;

void setup() {

Serial.begin(9600);

// setup row outputs

pinMode(row1, OUTPUT);

pinMode(row2, OUTPUT);

pinMode(row3, OUTPUT);

pinMode(row4, OUTPUT);

// setup col inputs

pinMode(col1, INPUT);

pinMode(col2, INPUT);

pinMode(col3, INPUT);

pinMode(col4, INPUT);

// setup states of pins

digitalWrite(col1, HIGH);

digitalWrite(col2, HIGH);

digitalWrite(col3, HIGH);

digitalWrite(col4, HIGH);

digitalWrite(row1, HIGH);

digitalWrite(row2, HIGH);

digitalWrite(row3, HIGH);

digitalWrite(row4, HIGH);

}

void loop() {

digitalWrite(row1, LOW); // put row 1 in LOW state

if (digitalRead(col1)==0){ // if col 1 is low it means that button 1 is pressed

Serial.println(“Button 1 Pressed”);

}

else if(digitalRead(col2)==0){// col 2 = button 2

Serial.println(“Button 2 Pressed”);

}

else if(digitalRead(col3)==0){// col 13 = button 3

Serial.println(“Button 3 Pressed”);

}

else if(digitalRead(col4)==0){// col 4 = button A

Serial.println(“Button A Pressed”);

}

digitalWrite(row1, HIGH);

digitalWrite(row2, LOW);

Serial.println(“Button 4 Pressed”);

}

Serial.println(“Button 5 Pressed”);

}

Serial.println(“Button 6 Pressed”);

}

Serial.println(“Button B Pressed”);

}

digitalWrite(row2, HIGH);

digitalWrite(row3, LOW);

Serial.println(“Button 7 Pressed”);

}

Serial.println(“Button 8 Pressed”);

}

Serial.println(“Button 9 Pressed”);

}

Serial.println(“Button C Pressed”);

}

digitalWrite(row3, HIGH);

digitalWrite(row4, LOW);

Serial.println(“Button * Pressed”);

}

Serial.println(“Button 0 Pressed”);

}

Serial.println(“Button # Pressed”);

}

Serial.println(“Button D Pressed”);

}

digitalWrite(row4, HIGH);

}

Now we will introduce a PIN number from a keypad and visualise it on 20×4 lcd display:

The next program works like: if you introduce a pin(you can choose one) the led from pin 13 is switched on and if you introduce another pin the led is switched off. Also we have made that if you introduce wronk the pin if you push the “A” button it will be reset to zero.