#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with the I2C address (replace 0x27 with the address you found)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change 0x27 to your LCD's address
// Define the shared pin for the button and LED
const int sharedPin = 26;
void setup() {
// Initialize Serial for debugging
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize the LCD with 20 columns and 4 rows
Wire.begin(8, 9); // Use GPIO 8 (SDA) and GPIO 9 (SCL) for ESP32-S2
lcd.begin(20, 4);
lcd.backlight(); // Turn on the backlight
// Set up the shared pin as input with internal pull-up resistor
pinMode(sharedPin, INPUT_PULLUP);
// Display initial message
lcd.setCursor(0, 0);
lcd.print("Press the button");
Serial.println("LCD initialized. Waiting for button press...");
}
void loop() {
// Check if the button is pressed (LOW because of pull-up)
if (digitalRead(sharedPin) == LOW) {
Serial.println("Button pressed. Displaying 'Hello' and turning on LED...");
// Display "Hello" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hello");
// Turn on the LED by setting the shared pin as output and driving it HIGH
pinMode(sharedPin, OUTPUT);
digitalWrite(sharedPin, HIGH);
// Wait for 10 seconds
delay(10000);
// Clear the LCD after 10 seconds
lcd.clear();
Serial.println("Clearing LCD...");
// Turn off the LED by setting the shared pin back to input mode
pinMode(sharedPin, INPUT_PULLUP);
// Display the initial message again
lcd.setCursor(0, 0);
lcd.print("Press the button");
Serial.println("Waiting for button press...");
}
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1