ESP32 and I2C LCD Integration
Started with ESP32 to link it with the LCD16x2 i2c
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD Configuration
// Address 0x27 is most common, try 0x3F if this doesn't work
// For 16x2 LCD: (address, columns, rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables for demonstration
int counter = 0;
unsigned long lastDisplayUpdate = 0;
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
Serial.println("ESP32 LCD Test Starting...");
// Initialize I2C - ESP32 default pins: SDA=GPIO21, SCL=GPIO22
Wire.begin();
// Initialize the LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.clear(); // Clear the display
// Display welcome message
lcd.setCursor(0, 0);
lcd.print("ESP32 Connected!");
lcd.setCursor(0, 1);
lcd.print("LCD Working!");
Serial.println("LCD initialized successfully");
delay(2000); // Show welcome message for 2 seconds
}
void loop() {
// Update display every second
if (millis() - lastDisplayUpdate >= 1000) {
lastDisplayUpdate = millis();
counter++;
// Clear the display
lcd.clear();
// First line: Show counter with ESP32
lcd.setCursor(0, 0);
lcd.print("ESP32 Counter:");
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(counter);
// Also print to Serial monitor for verification
Serial.print("Counter updated: ");
Serial.println(counter);
// Optional: Blink every 5 seconds to show it's alive
if (counter % 5 == 0) {
lcd.setCursor(12, 1);
lcd.print("!");
}
}
}
Outputs:
WARNING: library LiquidCrystal I2C claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s).
Sketch uses 307104 bytes (23%) of program storage space. Maximum is 1310720 bytes.
Global variables use 23476 bytes (7%) of dynamic memory, leaving 304204 bytes for local variables. Maximum is 327680 bytes.
esptool v5.1.0
Serial port COM14:
Connecting....
Connected to ESP32 on COM14:
Chip type: ESP32-D0WD-V3 (revision v3.1)
Features: Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency: 40MHz
MAC: 88:57:21:95:52:a8
Uploading stub flasher...
Running stub flasher...
Stub flasher running.
Changing baud rate to 921600...
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x0005bfff...
Compressed 25024 bytes to 16034...
Writing at 0x00001000 [ ] 0.0% 0/16034 bytes...
Writing at 0x000071c0 [==============================] 100.0% 16034/16034 bytes...
Wrote 25024 bytes (16034 compressed) at 0x00001000 in 0.6 seconds (330.2 kbit/s).
Hash of data verified.
Compressed 3072 bytes to 146...
Writing at 0x00008000 [ ] 0.0% 0/146 bytes...
Writing at 0x00008c00 [==============================] 100.0% 146/146 bytes...
Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.0 seconds (675.9 kbit/s).
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000 [ ] 0.0% 0/47 bytes...
Writing at 0x00010000 [==============================] 100.0% 47/47 bytes...
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (907.5 kbit/s).
Hash of data verified.
Compressed 307248 bytes to 170614...
Writing at 0x00010000 [ ] 0.0% 0/170614 bytes...
Writing at 0x0001c0b6 [=> ] 9.6% 16384/170614 bytes...
Writing at 0x00029c20 [====> ] 19.2% 32768/170614 bytes...
Writing at 0x0002f23a [=======> ] 28.8% 49152/170614 bytes...
Writing at 0x00034bfc [==========> ] 38.4% 65536/170614 bytes...
Writing at 0x0003a0ea [=============> ] 48.0% 81920/170614 bytes...
Writing at 0x0003f49d [================> ] 57.6% 98304/170614 bytes...
Writing at 0x00044b7c [===================> ] 67.2% 114688/170614 bytes...
Writing at 0x0004a59a [======================> ] 76.8% 131072/170614 bytes...
Writing at 0x00052c45 [========================> ] 86.4% 147456/170614 bytes...
Writing at 0x00058896 [===========================> ] 96.0% 163840/170614 bytes...
Writing at 0x0005b030 [==============================] 100.0% 170614/170614 bytes...
Wrote 307248 bytes (170614 compressed) at 0x00010000 in 3.4 seconds (730.0 kbit/s).
Hash of data verified.
Hard resetting via RTS pin...
Hardware outputs
Comments
Post a Comment