# Simple MicroPython code for LCD display in Wokwi
# This code displays information on a 16x2 LCD connected via I2C
from machine import Pin, SoftI2C
from time import sleep
# Simple I2C LCD Driver Class
class SimpleLCD:
def __init__(self, i2c, addr=0x27):
self.i2c = i2c
self.addr = addr
self.backlight = 0x08 # Backlight on
self.init_display()
def write_byte(self, data):
self.i2c.writeto(self.addr, bytearray([data | self.backlight]))
sleep(0.001)
def write_nibble(self, data):
self.write_byte(data | 0x04) # Enable high
self.write_byte(data & 0xFB) # Enable low
def write_cmd(self, cmd):
# Send high nibble
self.write_nibble(cmd & 0xF0)
# Send low nibble
self.write_nibble((cmd << 4) & 0xF0)
def write_data(self, data):
# Send high nibble with RS=1
self.write_nibble((data & 0xF0) | 0x01)
# Send low nibble with RS=1
self.write_nibble(((data << 4) & 0xF0) | 0x01)
def init_display(self):
sleep(0.05)
# Initialize in 4-bit mode
self.write_nibble(0x30)
sleep(0.005)
self.write_nibble(0x30)
sleep(0.005)
self.write_nibble(0x30)
self.write_nibble(0x20) # 4-bit mode
# Configure display
self.write_cmd(0x28) # 4-bit, 2 line, 5x8 font
self.write_cmd(0x0C) # Display on, cursor off
self.write_cmd(0x06) # Auto increment cursor
self.clear()
def clear(self):
self.write_cmd(0x01)
sleep(0.002)
def cursor(self, col, row):
positions = [0x80, 0xC0, 0x94, 0xD4] # Line positions
self.write_cmd(positions[row] + col)
def print(self, text):
for char in text:
self.write_data(ord(char))
# =============================================================================
# MAIN PROGRAM
# =============================================================================
# Initialize I2C (GPIO 21=SDA, GPIO 22=SCL)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
# Check if LCD is connected
print("Scanning for I2C devices...")
devices = i2c.scan()
if 0x27 in devices:
print("LCD found at address 0x27")
else:
print("No LCD found! Check connections.")
# Initialize LCD
lcd = SimpleLCD(i2c, 0x27)
# Clear screen and display welcome message
lcd.clear()
lcd.cursor(0, 0) # Column 0, Row 0
lcd.print("Hello Wokwi!")
lcd.cursor(0, 1) # Column 0, Row 1
lcd.print("ESP32 LCD Demo")
print("Initial message displayed")
sleep(3)
# Display different information
info_list = [
("Temperature:", "25.6 C"),
("Humidity:", "68.2 %"),
("Time:", "12:34:56"),
("Status:", "Online"),
("Voltage:", "3.3V"),
("Memory:", "Free: 45KB")
]
# Cycle through information
while True:
for line1, line2 in info_list:
lcd.clear()
lcd.cursor(0, 0)
lcd.print(line1)
lcd.cursor(0, 1)
lcd.print(line2)
print(f"Displayed: {line1} / {line2}")
sleep(2)
# Display counter
lcd.clear()
lcd.cursor(0, 0)
lcd.print("Counter Demo")
for count in range(10):
lcd.cursor(0, 1)
lcd.print(f"Count: {count:02d}")
print(f"Counter: {count}")
sleep(1)
# =============================================================================
# WOKWI SETUP INSTRUCTIONS:
#
# 1. Create ESP32 project in Wokwi
# 2. Add component: LCD 16x2 (I2C)
# 3. Wire connections:
# - ESP32 GPIO 21 -> LCD SDA
# - ESP32 GPIO 22 -> LCD SCL
# - ESP32 3.3V -> LCD VCC
# - ESP32 GND -> LCD GND
# 4. Set LCD I2C address to 0x27 in diagram.json
# 5. Copy this code to main.py
# 6. Run simulation
# =============================================================================