keypad_liquidcrystal — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by josephkinuthia541.

Components

Sketch code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Keypad Configuration
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'}
};

// ESP32 GPIO Pins connected to Keypad
byte rowPins[ROWS] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 2, 15};

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

void setup() {
  // Initialize I2C communication for LCD
  Wire.begin(21, 22);

  // Initialize LCD
  lcd.init();
  lcd.backlight();
  
  // Initial Display Message
  lcd.setCursor(0, 0);
  lcd.print("Press any key:");
  lcd.setCursor(0, 1);
  lcd.print("Key: ");
}

void loop() {
  // Check for keypad input
  char key = keypad.getKey();

  // If a key is pressed, display it on the LCD
  if (key) {
    lcd.clear();
    lcd.setCursor(0,0);
     // Move cursor right after "Key: "
    lcd.print(key);
    lcd.print(" ");      // Clear trailing characters if any
  }
}

More projects by josephkinuthia541