This code is about basic example where we interface a 4×4 keypad with Arduino and one 20×4 LCD with Arduino. You can also use 16×2 LCD with Arduino in same code. Here is the complete working code example.
#include <LiquidCrystal.h>
#include <Keypad.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {8, 9, 10, 11}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A0, A1, A2, A3}; //connect to the column pinouts of the keypad
Keypad keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
lcd.begin(20, 4); // initialize the LCD with 20x4 characters
lcd.setCursor(0, 0);
lcd.print("Press a key:");
}
void loop(){
char key = keypad.getKey();
if (key){
lcd.setCursor(0, 1);
lcd.print("Pressed: ");
lcd.print(key);
}
}
Code language: Arduino (arduino)