Arduino Tutorial for Beginners – Using 4×4 Keypad With Arduino




In this post on Arduino Tutorial For Beginners, this topic about how to use 4×4 Keypad with Arduino. A keypad is one of the most commonly used input devices in microprocessor applications. In a standard keypad wired as an X-Y switch matrix, normally-open switches connect a row to a column when pressed. Look the picture below to know how the keypad button are connected.

DiagramInside

On the left is the circuit inside keypad has 4 rows are connected together and 4 columns are connected together, on the right is the pins respectively. The keypad 4×4 in the picture is the popular keypad we usually use to create project, it has 8 pins from the left to right, first 4 pins is rows and the rest one is columns. Now we will move to init part, you will know how Arduino capture the button from keypad.

Setting up pins on Arduino:
+ With ROWs: Set LOW voltage OUTPUT
+ With COLUMNs: Set INPUT_PULLUP – Internal pull-up resistor (Default column’s status is HIGH)
After we’d initialized, the status of keypad would be like below picture

Afterinit
All the rows are OUTPUT LOW voltage and all the columns are OUTPUT HIGH voltage.
Assume we press key 8, we will follow some steps below to check this button.

Step1_2

STEP 1: In loop function we will check column by column from Left to Right or Right to Left to determine the column are containing key 8 – that is C2 (The C2 will switch from HIGH to LOW voltage). At this time we only check this columns.
STEP 2: After we’d known the column is C2, we would check row by row. When we check rows, we will set OUTPUT that row from LOW to HIGH and then check the column C2 whether are switched from LOW to HIGH as the same time. If change -> OUTPUT the key respectively, else check to the next row. OKAY you will see the C2 still LOW voltage -> check next row.

Step3_4

STEP 3: We also check condition as STEP 2. That is set next row from LOW to HIGH and then check C2’s status. If change -> OUTPUT the key respectively, else check to the next row. OKAY you will see the C2 still LOW voltage -> check next row.
STEP 4: We also check condition as STEP2. That is set next row from LOW to HIGH and then check C2’s status. If change -> OUTPUT the key respectively, else check to the next row. OKAY now you can see the C2’s status was changed from LOW to HIGH. We will print to Serial Monitor key ‘8’. Then set all rows to LOW again.
Above are four basic steps we can follow to know how to capture key. Below is pin diagram and basic code without library.

Diagram

Coding, copy and paste to your Arduino IDE and enjoy this project.


void setup()
{
Serial.begin(9600);
// Set all rows OUTPUT LOW voltage
for(int i = 4; i < 8; i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
// Set all columns is INPUT_PULLUP (HIGH status)
for(int i = 8; i < 12; i++)
{
pinMode(i,INPUT_PULLUP);
}
}
void loop()
{
//C1
if(digitalRead(8) == 0)
{
// Set R1 to HIGH
// Then check C1
digitalWrite(4,HIGH);
if(digitalRead(8) == 1)
Serial.println("1");
else
{
// Set R2 to HIGH
// Check C1 again
digitalWrite(5,HIGH);
if(digitalRead(8) == 1)
Serial.println("4");
else
{
// Set R3 to HIGH
// Check C1 again
digitalWrite(6,HIGH);
if(digitalRead(8)== 1)
Serial.println("7");
else
{
// Set R4 to HIGH
// Check C1 again
digitalWrite(7,HIGH);
if(digitalRead(8)== 1)
Serial.println("*");
}
}
}
for(int i = 4; i < 8; i++)
digitalWrite(i,LOW);
}
// C2
else if(digitalRead(9) == 0)
{
// Set R1 to HIGH
// Then check C2
digitalWrite(4,HIGH);
if(digitalRead(9) == 1)
Serial.println("2");
else
{
// Set R2 to HIGH
// Check C2 again
digitalWrite(5,HIGH);
if(digitalRead(9) == 1)
Serial.println("5");
else
{
// Set R3 to HIGH
// Check C2 again
digitalWrite(6,HIGH);
if(digitalRead(9)== 1)
Serial.println("8");
else
{
// Set R4 to HIGH
// Check C2 again
digitalWrite(7,HIGH);
if(digitalRead(9)== 1)
Serial.println("0");
}
}
}
for(int i = 4; i < 8; i++)
digitalWrite(i,LOW);
}
// C3
else if(digitalRead(10) == 0)
{
// Set R1 to HIGH
// Then check C3
digitalWrite(4,HIGH);
if(digitalRead(10) == 1)
Serial.println("3");
else
{
// Set R2 to HIGH
// Check C3 again
digitalWrite(5,HIGH);
if(digitalRead(10) == 1)
Serial.println("6");
else
{
// Set R3 to HIGH
// Check C3 again
digitalWrite(6,HIGH);
if(digitalRead(10)== 1)
Serial.println("9");
else
{
// Set R4 to HIGH
// Check C3 again
digitalWrite(7,HIGH);
if(digitalRead(10)== 1)
Serial.println("#");
}
}
}
for(int i = 4; i < 8; i++)
digitalWrite(i,LOW);
}
// C4
else if(digitalRead(11) == 0)
{
// Set R1 to HIGH
// Then check C4
digitalWrite(4,HIGH);
if(digitalRead(11) == 1)
Serial.println("A");
else
{
// Set R2 to HIGH
// Check C4 again
digitalWrite(5,HIGH);
if(digitalRead(11) == 1)
Serial.println("B");
else
{
// Set R3 to HIGH
// Check C4 again
digitalWrite(6,HIGH);
if(digitalRead(11)== 1)
Serial.println("C");
else
{
// Set R4 to HIGH
// Check C4 again
digitalWrite(7,HIGH);
if(digitalRead(11)== 1)
Serial.println("D");
}
}
}
for(int i = 4; i < 8; i++)
digitalWrite(i,LOW);
}
delay(500);
}

You can also use Keypad library. Watch my video above to know how to use library and how to edit appropriately code. Thanks!


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*