Keypad interfacing with LCD

Hi,
A brief info. I'm using a 4x3 keypad (attached) and a 4x20 lcd (attached). The problem I'm having is that the keypad doesn't show the digits that I'm pressing on the keypad. Below is the code that I'm using: It is able to print out the words but it just cannot display the buttons that I pressed on the keypad. Pls give me some guidance on solving this. Thank you :slight_smile:

///////////////////----------LCD Connection------------////////////////
/*
The circuit:
LCD VDD pin to +5V
LCD V0 pin to GND
LCD RS pin to digital pin 12
LCD R/W pin to digital pin 11
LCD Enable pin to digital pin 10
LCD D4 pin to digital pin 5
LCD D5 pin to digital pin 4
LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
LCD +LED pin to digital pin 13
LCD -LED pin to GND
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////----------Keypad Connection------------////////////////
/*
The circuit:
Keypad pin 1 – digital pin 12
Keypad pin 2 – digital pin 11
Keypad pin  3– digital pin 10
Keypad pin  4– digital pin 9
Keypad pin  5– digital pin 8
Keypad pin  6– digital pin 7
Keypad pin  7– digital pin 6
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <Keypad.h>
#include <LiquidCrystal.h>


///////////////////----------Read and Store Date------////////////////////
char rsdata;
/////////////////////////////////////////////////////////////////////////////////////////

///////////////////----------Keypad Setting-------------////////////////////
const byte ROWS=4; //4 rows
const byte COLS=3; //3 columns
char key[ROWS][COLS]={
  
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'},
};

byte rowPins[ROWS]={11,6,7,9};
byte colPins[COLS]={10,12,8};

Keypad customKeypad=Keypad(makeKeymap(key) ,rowPins, colPins, ROWS, COLS);

//////////////////------------LCD Setting--------------------//////////////
int backLight = 13;    // pin 13 will control the backlight

// setup LiquidCrystal lcd variable
//                 LCD pin:  rs, r/w, enable, d4, d5, d6, d7 
//              Arduino pin:12, 11,        10,   5,   4,   3,  2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

////////////////////////////////////////////////////////////////////////////




void setup() {

Serial.begin(9600);

pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
 lcd.begin(20, 4);     // setting of LCD (4 x 20)

 // Try to print somethings on LCD (IT IS WORK FOR THIS PART)//
  lcd.print("I'm Medi the Medicine Dispenser");
  delay(2500);
  lcd.clear();
  /*lcd.print("Enter a Number");
delay(1500);*/
}
 

///////////////------void loop------////////////////////////////

void loop(){
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Please enter hp number 1: "); // ( I can print this "HI" on LCD)
char customKey = customKeypad.getKey();  
  if (customKey!= NO_KEY){
    Serial.println(customKey);
    lcd.clear();
    lcd.setCursor(0,2);
    lcd.print("GOT KEY");
    delay(2000);
  }

}

SparkFunKeypad.png

lcd.jpg

  if (customKey!= NO_KEY){
    Serial.println(customKey);
    lcd.clear();                       // <----------------------------
    lcd.setCursor(0,2);
    lcd.print("GOT KEY");

How can you see the digit on the LCD if you clear it?

steinie44:
How can you see the digit on the LCD if you clear it?

I removed the line of code which has been pointed out. But the LCD still doesnt display the digits.

///////////////------void loop------////////////////////////////

void loop(){
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Please enter hp number 1: "); // ( I can print this "HI" on LCD)
char customKey = customKeypad.getKey();  
  if (customKey!= NO_KEY){
    Serial.println(customKey);
    /*lcd.clear();*/                       <<----------------------------------------
    lcd.setCursor(0,2);
    lcd.print("GOT KEY");
    delay(2000);
  }

}

You have Serial.println(customKey);
Does it show in the Serial window? If yes, change to
lcd.print(customKey);

steinie44:
You have Serial.println(customKey);
Does it show in the Serial window? If yes, change to
lcd.print(customKey);

It remains the same sir. Just to check with you sir. Does the two lines of code I pointed out below affecting my display?

// Try to print somethings on LCD (IT IS WORK FOR THIS PART)//
  lcd.print("I'm Medi the Medicine Dispenser");
  delay(2500);
  lcd.clear();                           <------------(1)
  /*lcd.print("Enter a Number");
delay(1500);*/
}
///////////////------void loop------////////////////////////////

void loop(){
lcd.clear();                    <-------------(2)
lcd.setCursor(0, 1);
lcd.print("Please enter hp number 1: "); // ( I can print this "HI" on LCD)
char customKey = customKeypad.getKey();  
  if (customKey!= NO_KEY){
    Serial.println(customKey);
    lcd.clear();
    lcd.setCursor(0,2);
    lcd.print("GOT KEY");
    delay(2000);
  }

}

steinie44:

  if (customKey!= NO_KEY){

Serial.println(customKey);
    lcd.clear();                       // <----------------------------
    lcd.setCursor(0,2);
    lcd.print("GOT KEY");



How can you see the digit on the LCD if you clear it?

OP is NOT printing the number on the LCD. Serial instead.

OP, I see you attached a picture for connection but not YOUR setup. You could have connected the keypad backwards or just wrong. On a quick look at your code, your pins 10, 11, 12 are used by both LCD and keypad. How did you make that work?

liudr:
OP, I see you attached a picture for connection but not YOUR setup. You could have connected the keypad backwards or just wrong. On a quick look at your code, your pins 10, 11, 12 are used by both LCD and keypad. How did you make that work?

Hmm.. Do you mean they cannot work using the same pin sir?
P.S what does 'OP' stands for?

You are the OP, original poster. You posted the OP, original post. If you connect pins 10,11,12 to the LCD already, you may NOT use them for anything else. Take your LCD connections to the analog pins A0-A5. You can use them as digital pins with numbers 14-19.