#----------------- LCD 2--------------------------#
####### TEST 2 ######
from machine import Pin, I2C
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd # Using the newer i2c-lcd.py library
# ========== HARDWARE CONFIGURATION ==========
# I2C Configuration (Real ESP32 pins)
I2C_SDA = Pin(21)
I2C_SCL = Pin(22)
LCD_ADDR = 0x27 # Try 0x3F if display doesn't respond
LCD_ROWS = 4 # 4 rows
LCD_COLS = 20 # 20 columns
# Button Configuration
BTN1 = Pin(14, Pin.IN, Pin.PULL_UP)
BTN2 = Pin(15, Pin.IN, Pin.PULL_UP)
BTN3 = Pin(16, Pin.IN, Pin.PULL_UP)
# ========== INITIALIZATION ==========
# Initialize I2C
i2c = I2C(0, sda=I2C_SDA, scl=I2C_SCL, freq=400000)
# Scan I2C bus to verify LCD is connected
print("Scanning I2C bus...")
devices = i2c.scan()
print(f"Found devices: {[hex(x) for x in devices]}")
# Initialize LCD with proper 4-line mode
try:
lcd = I2cLcd(i2c, LCD_ADDR, LCD_ROWS, LCD_COLS)
print("LCD initialized successfully!")
except Exception as e:
print(f"LCD init failed: {e}")
raise
# ========== LCD FUNCTIONS ==========
def display_lcd(lines, clear=True):
#
#Display text on 20x4 LCD
#Args:
# lines: List of up to 4 strings
# clear: Whether to clear display first
#
if clear:
lcd.clear()
for row in range(min(4, len(lines))): # Ensure max 4 lines
lcd.move_to(0, row)
lcd.putstr(lines[row][:20]) # Trim to 20 chars
# Debug output
print("Displaying:")
for i, line in enumerate(lines):
print(f"Line {i+1}: {line}")
# ========== BUTTON FUNCTIONS ==========
def debounce(button):
# Simple button debounce
time.sleep_ms(20)
return button.value() == 0
# ========== MAIN PROGRAM ==========
# Startup message showing all 4 lines work
display_lcd([
"20x4 LCD Test",
"All lines working",
"Line 3 confirmed",
"Line 4 operational"
])
while True:
# Check Button 1
if not BTN1.value() and debounce(BTN1):
display_lcd([
"Button 1 pressed",
"Starting process",
"Status: Running",
"Task 1 active"
])
while not BTN1.value(): # Wait for release
time.sleep_ms(10)
# Check Button 2
elif not BTN2.value() and debounce(BTN2):
display_lcd([
"Button 2 pressed",
"Stopping process",
"Status: Idle",
"System ready"
])
while not BTN2.value():
time.sleep_ms(10)
# Check Button 3
elif not BTN3.value() and debounce(BTN3):
display_lcd([
"Menu options:",
"1. System info",
"2. Configuration",
"3. Diagnostics"
])
while not BTN3.value():
time.sleep_ms(10)
time.sleep_ms(50)
"""
#--------------------------LCD 1------------------------------#
####### TEST 1 ######
from machine import Pin, I2C
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd # Using the newer i2c-lcd.py libraryy
# === WOKWI I2C LCD SETUP ===
# Default I2C pins in Wokwi (fixed for simulation)
I2C_SDA = Pin(21)
I2C_SCL = Pin(22)
LCD_ADDR = 0x27 # Try 0x3F if display doesn't work
LCD_ROWS = 4 # 4 rows
LCD_COLS = 20 # 20 columns
# Initialize I2C
i2c = I2C(0, sda=I2C_SDA, scl=I2C_SCL, freq=400000)
# Initialize LCD
lcd = I2cLcd(i2c, LCD_ADDR, LCD_ROWS, LCD_COLS)
# === BUTTON SETUP ===
SW1 = Pin(14, Pin.IN, Pin.PULL_UP)
SW2 = Pin(15, Pin.IN, Pin.PULL_UP)
SW3 = Pin(16, Pin.IN, Pin.PULL_UP)
# === LCD FUNCTIONS ===
def show_lcd(lines, clear=True):
#---Display text on LCD (max 4 lines)---
if clear:
lcd.clear()
for row, text in enumerate(lines[:4]):
lcd.move_to(0, row)
lcd.putstr(text)
print("LCD:", lines) # Debug output
def wait_release(button):
while not button.value(): # Wait until button released
time.sleep_ms(20)
# === MAIN PROGRAM ===
show_lcd([
"Wokwi LCD 20x4 Test",
"Press any button:",
"SW1=Start",
"SW2=Stop"
])
while True:
if not SW1.value(): # Button 1 pressed
show_lcd([
"Button 1 Pressed",
"Starting...",
"Status: ACTIVE",
"Task 1 Running"
])
wait_release(SW1)
elif not SW2.value(): # Button 2 pressed
show_lcd([
"Button 2 Pressed",
"Stopping...",
"Status: INACTIVE",
"Shutting down"
])
wait_release(SW2)
elif not SW3.value(): # Button 3 pressed
show_lcd([
"EMERGENCY STOP!",
"System HALTED",
"Check logs",
"Reset required"
])
wait_release(SW3)
time.sleep_ms(50)
#--------------------------------------------------------#
#--------------------------LCD 0------------------------------#
####### TEST 0 ######
from machine import Pin, I2C
import time
from esp8266_i2c_lcd import I2cLcd # Wokwi-compatible LCD library
# === WOKWI I2C LCD SETUP ===
# Default I2C pins in Wokwi (fixed for simulation)
I2C_SDA = Pin(21)
I2C_SCL = Pin(22)
LCD_ADDR = 0x27 # Try 0x3F if display doesn't work
LCD_ROWS = 4 # 4 rows
LCD_COLS = 20 # 20 columns
# Initialize I2C
i2c = I2C(0, sda=I2C_SDA, scl=I2C_SCL, freq=400000)
# Initialize LCD
lcd = I2cLcd(i2c, LCD_ADDR, LCD_ROWS, LCD_COLS)
# === BUTTON SETUP ===
SW1 = Pin(14, Pin.IN, Pin.PULL_UP)
SW2 = Pin(15, Pin.IN, Pin.PULL_UP)
SW3 = Pin(16, Pin.IN, Pin.PULL_UP)
# === LCD FUNCTIONS ===
def show_lcd(lines, clear=True):
#----Display text on LCD (max 4 lines)---
if clear:
lcd.clear()
for row, text in enumerate(lines[:4]):
lcd.move_to(0, row)
lcd.putstr(text)
print("LCD:", lines) # Debug output
def wait_release(button):
while not button.value(): # Wait until button released
time.sleep_ms(20)
# === MAIN PROGRAM ===
show_lcd([
"Wokwi LCD 20x4 Test",
"Press any button:",
"SW1=Start",
"SW2=Stop"
])
while True:
if not SW1.value(): # Button 1 pressed
show_lcd([
"Button 1 Pressed",
"Starting...",
"Status: ACTIVE",
"Task 1 Running"
])
wait_release(SW1)
elif not SW2.value(): # Button 2 pressed
show_lcd([
"Button 2 Pressed",
"Stopping...",
"Status: INACTIVE",
"Shutting down"
])
wait_release(SW2)
elif not SW3.value(): # Button 3 pressed
show_lcd([
"EMERGENCY STOP!",
"System HALTED",
"Check logs",
"Reset required"
])
wait_release(SW3)
time.sleep_ms(50)
"""
""" ........ END........"""