Displaying keypad to LCD

Hello, I am relatively new to Arduino and for a project I need to get an LCD to display the pressed buttons of a keypad. So far, it is not working but the code doesn't come up with any errors. This is my code:

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

// Define the pins for the LCD screen
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// Define the pins for the keypad
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] = {2, 3, 4, 5};
byte colPins[COLS] = {14, 15, 16, 17};

// Define the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  // Initialize the LCD screen
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Press a key:");

  // Initialize the serial monitor
  Serial.begin(9600);
}

void loop() {
  // Read the keypad
  char key = keypad.getKey();
  if (key != NO_KEY) {
    // Display the pressed key on the LCD screen
    lcd.setCursor(0, 1);
    lcd.print(key);
    
    // Print the pressed key to the serial monitor
    Serial.println(key);
    
    delay(200); // Debounce the keypad
  }
}

Please elaborate.

By the way, did you test with the keypad and display library examples, to verify that the hardware works?

Another thing, why do you have a debounce delay? Doesn't the keypad library do that for you?

Always show us a good schematic of your proposed circuit.
Show us a good image of your actual wiring.
Give links to components.

Welcome to the forum.

Which Arduino board do you use ? The Arduino Uno does not have a text label on the board that says pin "17".

Hi Koepel,

The board I have is an Arduino Mega2560.

Hi aarg,

The keypad itself works, but when I press a button on it the number doesn't display on the LCD. I was picking up the code from a partner who had added in debounce delay.

Hi LarryD,

I will be able to get a good image of the wiring in a few hours. Here is the parts kit I am using:

You might have a wiring error in the LCD display circuit.


Your schematic should be similar to this, your pins will be different though:

image

The potentiometer (try 10k) does not need a Vcc connection.

Can you write "Hello World" on the LCD display ?
Your problem seems to be how the LCD display is connected. Most of us had trouble with all those wires, so we prefer a I2C LCD with only four wires.

Your sketch with a working display and the correct wiring is working.
This is your sketch in Wokwi simulation:

1 Like

Also, you will find I2C LCDs are much less susceptible to noisy situations.

But you don't know if it's because of your code or wiring. Running the display library examples will resolve that question.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.