//Description:
//In this project, we want to set up a keypad with Arduino.
//Note, if you want to start the keypad with ESP32, you can use this code.
//You just need to connect the keypad to the ESP32's authorized GPIOs.
//You must install the Keypad library.
#include<Keypad.h>
//We want to turn on the LED by pressing any key on the keypad.
//Important note: There is a small LED on the Arduino module, which is called L.
//This LED is connected to pin 13 of Arduino.
//You can control this LED by outputting pin 13.
//Therefore, we defined pin 13 as the output so that the small LED lights up by pressing the keypad key.
const int LED=13;
const byte Rows=4;//Number of Rows
const byte Cols=4;//Number of Columns
char keys[Rows][Cols]={//Define a 2D array to specify the label on the keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
//If the order of the keys was not as you wanted, you can change the connection of rows and columns with the command written below or change it by hardware.
byte rowpins[Rows]={3,2,1,0};//Connecting Rows to Arduino
byte colpins[Cols]={7,6,5,4};//Connecting columns to Arduino
Keypad Ardo=Keypad(makeKeymap(keys),rowpins,colpins,Rows,Cols);
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
char Val=Ardo.getKey();
if(Val){
Serial.print("The pressed key is : ");
Serial.println(Val);
digitalWrite(LED, HIGH);
delay(200);
}
else{
digitalWrite(LED,LOW);
delay(200);
}
}