how to use arduino keypad with LCD dislpay tutorial


Interfacing 16×2 LCD and 4x4 matrix keypad with Arduino microcontroller.



Parts and components

  • Arduino Uno board
  • 16x2 LCD
  • 1 K ohm potentiometer
  • 4x4 matrix keypad
Other requirements

1. you need to add the liquid crystal master library to your Arduino IDE



2. you need to add the Keypad master library  in your Arduino IDE



How to add a Library in Arduino IDE







Step 1: Schematic



                                                    

              Step 2:Codeing


There  are two different codes you can try both of them without changing the wiring

1. without  keypad libraries 

2. with keypad libraries
                     

                     

 
 without keypad libraries

#include<LiquidCrystal.h>

LiquidCrystal lcd(12,13,0,1,2,3);//Rs-12,E-13,D4-0,D5-1,D6-2,D7-3

#define r1 11
#define r2 10
#define r3 9
#define r4 8

#define c1 7
#define c2 6
#define c3 5
#define c4 4


void setup() {
  
  
lcd.begin(16,2);

pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(r3,OUTPUT);
pinMode(r4,OUTPUT);

pinMode(c1,INPUT_PULLUP);
pinMode(c2,INPUT_PULLUP);
pinMode(c3,INPUT_PULLUP);
pinMode(c4,INPUT_PULLUP);


  digitalWrite(r1,1);
  digitalWrite(r2,1);
  digitalWrite(r3,1);
  digitalWrite(r4,1);
 
}

void loop() {

   digitalWrite(r1,0);

if(digitalRead(c1)==0){
  lcd.print('1');}
  
if(digitalRead(c2)==0){
  lcd.print('2');}

if(digitalRead(c3)==0){
  lcd.print('3');}

if(digitalRead(c4)==0){
  lcd.print('A');}
  
  
  digitalWrite(r1,1);
  digitalWrite(r2,0);
  

if(digitalRead(c1)==0){
  lcd.print('4');}
  
if(digitalRead(c2)==0){
  lcd.print('5');}

if(digitalRead(c3)==0){
  lcd.print('6');}

if(digitalRead(c4)==0){
  lcd.print('B');}

  
  digitalWrite(r2,1);
  digitalWrite(r3,0);

  

if(digitalRead(c1)==0){
 lcd.print('7');}
  
if(digitalRead(c2)==0){
  lcd.print('8');}

if(digitalRead(c3)==0){
  lcd.print('9');}

if(digitalRead(c4)==0){
  lcd.clear();}
  
  
  digitalWrite(r3,1);
  digitalWrite(r4,0);
  

if(digitalRead(c1)==0){
  lcd.print('*');}
  
if(digitalRead(c2)==0){
  lcd.print('0');}

if(digitalRead(c3)==0){
  lcd.print('#');}

  if(digitalRead(c4)==0){
  lcd.print('D');}
  
  digitalWrite(r4,1);


  delay(220);
  }


With keypad libraries

#include <Keypad.h>  

#include<LiquidCrystal.h>

LiquidCrystal lcd(12,13,0,1,2,3);//Rs-12,E-13,D4-0,D5-1,D6-2,D7-3

const byte ROWS = 4;
const byte COLS = 4;


char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {11,10,9,8};
byte colPins[COLS] = {7,6,5,4};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

  lcd.begin(16,2);
  
  }


  void loop(){


  char key = myKeypad.getKey();

  if (key != NO_KEY){

    lcd.print(key);
  
       }
       
    if (key == 'C'){

    lcd.clear();
  
       } 
       
       
       }