import machine
import time
# I2C Address of the simulated PCF8574T chip controlling the LCD module
I2C_ADDRESS = 0x27
# Define LCD commands
LCD_CLEAR_DISPLAY = 0x01
LCD_RETURN_HOME = 0x02
# Define other commands as needed
# Initialize the I2C bus
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=400000) # Adjust pins and frequency as needed
# Function to send commands to the LCD
def send_command(command):
i2c.writeto(I2C_ADDRESS, bytes([0x00, command])) # Send command byte with RS bit low
time.sleep(0.01)
# Function to initialize the LCD
def init_lcd():
send_command(LCD_CLEAR_DISPLAY)
time.sleep(0.1)
send_command(LCD_RETURN_HOME)
time.sleep(0.1)
# Function to clear the LCD screen
def clear_lcd():
send_command(LCD_CLEAR_DISPLAY)
time.sleep(0.1)
# Function to display text on the LCD
def display_text(text):
for char in text:
send_command(ord(char) | 0x80) # Set RS bit high for data
time.sleep(0.01)
# Example usage
init_lcd()
display_text("Hello, Wokwi!")
time.sleep(2)
clear_lcd()
display_text("Goodbye, Wokwi!")
time.sleep(2)
clear_lcd()