3 *4 KEYPAD INTERFACING WITH ARDUINO

Photo of author

By Jackson Taylor

  • BOARD -ARDUINO UNO 
  • SIMULATION -PROTEUS 8.1

SIMULATION
DOWNLOAD SIMULATION FILES


PROGRAM
#define R1 3  //pin 1
#define R2 4  //pin 2
#define R3 5  //pin 3
#define R4 6  //pin 4
#define C1 7  //pin 5
#define C2 8  //pin 6
#define C3 9  //pin 7
int key=12;
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
keypadinit();
}
void loop() {
  // put your main code here, to run repeatedly:
keycheck();
}
void keycheck(void)
{
digitalWrite(C1,LOW);digitalWrite(C2,HIGH);digitalWrite(C3,HIGH);//READS FIRST COLUMN
if(digitalRead(R1)==0) {key=1;}
if(digitalRead(R2)==0) {key=4;}
if(digitalRead(R3)==0) {key=7;}
if(digitalRead(R4)==0) {key=10;}
digitalWrite(C1,HIGH);digitalWrite(C2,LOW);digitalWrite(C3,HIGH);//READS FIRST COLUMN
if(digitalRead(R1)==0) {key=2;}
if(digitalRead(R2)==0) {key=5;}
if(digitalRead(R3)==0) {key=8;}
if(digitalRead(R4)==0) {key=0;}
digitalWrite(C1,HIGH);digitalWrite(C2,HIGH);digitalWrite(C3,LOW);//READS FIRST COLUMN
if(digitalRead(R1)==0) {key=3;}
if(digitalRead(R2)==0) {key=6;}
if(digitalRead(R3)==0) {key=9;}
if(digitalRead(R4)==0) {key=11;}
if(key<12) {Serial.println(key); key=12;keydelay();  }
}
//FUNCTIONS
void keypadinit(void)
{
pinMode(C1,OUTPUT);pinMode(C2,OUTPUT);pinMode(C3,OUTPUT);
pinMode(R1,INPUT_PULLUP);pinMode(R2,INPUT_PULLUP);pinMode(R3,INPUT_PULLUP);pinMode(R4,INPUT_PULLUP); //pull up
}
void keydelay(void)
{
delay(300);
}