In this tutorial we are going to see how to interface 4 x 4 matrix keypad with Arduino.
COMPONENTS REQUIRENT
Arduino UNO
4 X 4 KEYPAD
CODE
#define r0 10
#define r1 11
#define r2 12
#define r3 13
#define c0 9
#define c1 8
#define c2 7
#define c3 6
#define r1 11
#define r2 12
#define r3 13
#define c0 9
#define c1 8
#define c2 7
#define c3 6
volatile char key_value = 'x';
char key();
void keypad_init();
void setup() {
Serial.begin(9600);
keypad_init();
}
void loop() {
Serial.println(key());
delay(1000);
}
void keypad_init()
{
pinMode(r0, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(c0, INPUT_PULLUP);
pinMode(c1, INPUT_PULLUP);
pinMode(c2, INPUT_PULLUP);
pinMode(c3, INPUT_PULLUP);
}
char key()
{
digitalWrite(r0, LOW);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
if (digitalRead(c0) == LOW)
key_value = '*';
while (digitalRead(c0) == LOW);
if (digitalRead(c1) == LOW)
key_value = '0';
while (digitalRead(c1) == LOW);
if (digitalRead(c2) == LOW)
key_value = '#';
while (digitalRead(c2) == LOW);
if (digitalRead(c3) == LOW)
key_value = 'D';
while (digitalRead(c3) == LOW);
digitalWrite(r0, HIGH);
digitalWrite(r1, LOW);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
if (digitalRead(c0) == LOW)
key_value = '7';
while (digitalRead(c0) == LOW);
if (digitalRead(c1) == LOW)
key_value = '8';
while (digitalRead(c1) == LOW);
if (digitalRead(c2) == LOW)
key_value = '9';
while (digitalRead(c2) == LOW);
if (digitalRead(c3) == LOW)
key_value = 'C';
while (digitalRead(c3) == LOW);
digitalWrite(r0, HIGH);
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
if (digitalRead(c0) == LOW)
key_value = '4';
while (digitalRead(c0) == LOW);
if (digitalRead(c1) == LOW)
key_value = '5';
while (digitalRead(c1) == LOW);
if (digitalRead(c2) == LOW)
key_value = '6';
while (digitalRead(c2) == LOW);
if (digitalRead(c3) == LOW)
key_value = 'B';
while (digitalRead(c3) == LOW);
digitalWrite(r0, HIGH);
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
if (digitalRead(c0) == LOW)
key_value = '1';
while (digitalRead(c0) == LOW);
if (digitalRead(c1) == LOW)
key_value = '2';
while (digitalRead(c1) == LOW);
if (digitalRead(c2) == LOW)
key_value = '3';
while (digitalRead(c2) == LOW);
if (digitalRead(c3) == LOW)
key_value = 'A';
while (digitalRead(c3) == LOW);
return key_value;
Comments
Post a Comment