Need help to connect with 20X4 LCD and Keypad – Want to get the input from keypa

I'm connecting an Arduino UNO, with a 20X4 LCD, and a 4X3 keypad

Please see the code in below:
Need help to connect with 20X4 LCD and Keypad – Want to get the input from keypad and output show on 2-X4 LCD

I'm connecting an Arduino UNO, with a 20X4 LCD, and a 4X3 keypad

Please see the code in below:

///////////////////----------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("Printing text");
  delay(2500);
  lcd.clear();
  lcd.print("Enter a Number");
delay(1500);
}
 

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

void loop(){
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("HI"); // ( 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);
  }

}

Result:
I can print "Printing text" and "Enter a Number"
and the LCD is cleared

Then “HI” is print on the LCD

Now, I press a number “8” from keypad:

On Serial Monitor: 8 is showed
On LCD: The text” HI” is gone, but “8” did not show on the LCD

http://forum.arduino.cc/index.php/topic,148850.0.html

Hi, use the link above to show you how to post your code. It will make it easier to read.
Thanks and we will see what what we can do to help you.

Tom.... :slight_smile:

I have modified the post, thanks for your advise.
Please help to have a look on the code, too. Thank YOU!!!

Hi,
I am not sure you can expect this to work using the same pins for both LCD and Keyboard. Do you have any examples where this is supposed to work??

Using a 20x4 LCD that only uses 2 pins (because it has an I2C interface) like this one would make it easy:
See this on the ArduinoInfo.Info WIKI HERE:

Also, depending on how many keys you need, some smaller keypads use only one pin, like THESE:

Please tell us how this works out; if you can get the shared pins to work that will be interesting.

DISCLAIMER: Mentioned stuff from my own shop...

if (customKey!= NO_KEY){
Serial.println(customKey);
lcd.clear();
lcd.setCursor(0,2);
lcd.print("GOT KEY");
delay(2000);
}

And where is
lcd.print(customKey);
?

Cheers
Vaclav

Thank you to all of you, I did it now!!!

The circuit fail because pin12,11,10 also connected to lcd.
I have reconnect the circuit:
Original: keypad : 1, 2, 3, 4, 5,6,7
Arduino:12, 11, 10, 9,8,7,6

Modified: keypad : 1, 2, 3, 4, 5,6,7
Arduino:16, 15, 14, 9,8,7,6 (pin 16,15,14 are analog pin)

That is my final code with a password setting.

Code:

///////////////////----------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 16
Keypad pin 2 – digital pin 15
Keypad pin  3– digital pin 14
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>
#include <Password.h> 
Password password = Password( "1234" );
///////////////////----------Read and Store Date------////////////////////

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

///////////////////----------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]={15,6,7,9};
byte colPins[COLS]={14,16,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);
customKeypad.addEventListener(keypadEvent); //add an event listener for this keypad
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 //
  lcd.print("Printing text");
  delay(2500);
}
 

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

void loop() {
  lcd.clear();
  lcd.print("Enter a Number");
char customKey;

do {
   customKey = customKeypad.getKey(); 
 /////---It works on this part in the morning, GREAT THANKS for arduinodlb!!!!! ---////////
   /*if (customKey!= NO_KEY){
    Serial.println("Got Key");
    lcd.clear();
    lcd.print("GOT KEY");
    delay(1500);

    
    Serial.println("Displaying Key");
    Serial.println(customKey);
    lcd.clear();
    lcd.print("Displaying Key ");
    lcd.print(customKey);
    delay(1500);
  }*/
 ///////////////////////////////////////////////////////
} while (customKey == NO_KEY);

}
void keypadEvent(KeypadEvent eKey){
  switch (customKeypad.getState()){
    case PRESSED:
	Serial.print("Pressed: ");
	Serial.println(eKey);
        lcd.clear();
        lcd.print("Pressed: ");
        lcd.print(eKey);
        delay(600);
	switch (eKey){
	  case '*': checkPassword(); break;
	  case '#': resetPassword(); break;
	  default: password.append(eKey);
     }
  }
}

void checkPassword(){
  if (password.evaluate()){
    Serial.println("Success");
    lcd.clear();
    lcd.print("Success");
    delay(1500);
    //Add code to run if it works
  }else{
    Serial.println("Wrong");
    lcd.clear();
    lcd.print("Wrong");
    delay(1500);
    //add code to run if it did not work
  }
}

void resetPassword(){
    password.reset();
    Serial.println("Reset Password");
    lcd.clear();
    lcd.print("Reset Paaword");
    delay(1500);
  }

And I'm trying to learn how to shift register now!!Hope I can do it smart.

Thanks alot!!!!

@kingkong135, do not cross-post again. Other thread locked. Replies go here.

kingkong135:
Thank you to all of you, I did it now!!!

The circuit fail because pin12,11,10 also connected to lcd.
I have reconnect the circuit:
Original: keypad : 1, 2, 3, 4, 5,6,7
Arduino:12, 11, 10, 9,8,7,6

Modified: keypad : 1, 2, 3, 4, 5,6,7
Arduino:16, 15, 14, 9,8,7,6 (pin 16,15,14 are analog pin)

That is my final code with a password setting.

Code:

///////////////////----------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 16
Keypad pin 2 – digital pin 15
Keypad pin  3– digital pin 14
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>
#include <Password.h>
Password password = Password( "1234" );
///////////////////----------Read and Store Date------////////////////////

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

///////////////////----------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]={15,6,7,9};
byte colPins[COLS]={14,16,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);
customKeypad.addEventListener(keypadEvent); //add an event listener for this keypad
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 //
 lcd.print("Printing text");
 delay(2500);
}

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

void loop() {
 lcd.clear();
 lcd.print("Enter a Number");
char customKey;

do {
  customKey = customKeypad.getKey();
/////---It works on this part in the morning, GREAT THANKS for arduinodlb!!!!! ---////////
  /*if (customKey!= NO_KEY){
   Serial.println("Got Key");
   lcd.clear();
   lcd.print("GOT KEY");
   delay(1500);

Serial.println("Displaying Key");
   Serial.println(customKey);
   lcd.clear();
   lcd.print("Displaying Key ");
   lcd.print(customKey);
   delay(1500);
 }*/
///////////////////////////////////////////////////////
} while (customKey == NO_KEY);

}
void keypadEvent(KeypadEvent eKey){
 switch (customKeypad.getState()){
   case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
       lcd.clear();
       lcd.print("Pressed: ");
       lcd.print(eKey);
       delay(600);
switch (eKey){
 case '*': checkPassword(); break;
 case '#': resetPassword(); break;
 default: password.append(eKey);
    }
 }
}

void checkPassword(){
 if (password.evaluate()){
   Serial.println("Success");
   lcd.clear();
   lcd.print("Success");
   delay(1500);
   //Add code to run if it works
 }else{
   Serial.println("Wrong");
   lcd.clear();
   lcd.print("Wrong");
   delay(1500);
   //add code to run if it did not work
 }
}

void resetPassword(){
   password.reset();
   Serial.println("Reset Password");
   lcd.clear();
   lcd.print("Reset Paaword");
   delay(1500);
 }




And I'm trying to learn how to shift register now!!Hope I can do it smart.

Thanks alot!!!!

OK you got it working, however, it would work if you shared the LCD data bus with the output from keypad instead of the LCD control pins.
The LCD does not care where the data comes from.
Than you would have to make sure the correct data is on this shared bus at correct time.
Cheers
Vaclav