from machine import Pin, I2C
from time import sleep_ms

i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)

# LCD Address
LCD_ADDR = 0x27

# LCD commands
LCD_CLEAR_DISPLAY = 0x01
LCD_RETURN_HOME = 0x02
LCD_ENTRY_MODE_SET = 0x04
LCD_DISPLAY_CONTROL = 0x08
LCD_CURSOR_SHIFT = 0x10
LCD_FUNCTION_SET = 0x20
LCD_SET_CGRAM_ADDR = 0x40
LCD_SET_DDRAM_ADDR = 0x80

# Display ON/OFF control flags
LCD_DISPLAY_ON = 0x04
LCD_CURSOR_ON = 0x02
LCD_BLINK_ON = 0x01

# Function Set flags
LCD_4BIT_MODE = 0x00
LCD_2LINE_MODE = 0x08
LCD_5X8_FONT = 0x00

# Set backlight control pin (GP18)
backlight = Pin(18, Pin.OUT)

def lcd_command(cmd):
    i2c.writeto(LCD_ADDR, bytes([0x00] + cmd))

def lcd_init():
    sleep_ms(50)  # wait for LCD to power on
    lcd_command([0x30])  # Wake up
    sleep_ms(5)   
    lcd_command([0x30])  # Wake up #2
    sleep_ms(1)   
    lcd_command([0x30])  # Wake up #3
    sleep_ms(1)   
    lcd_command([LCD_FUNCTION_SET | LCD_4BIT_MODE])
    sleep_ms(1)   
    lcd_command([LCD_FUNCTION_SET | LCD_4BIT_MODE])
    sleep_ms(1)   
    lcd_command([LCD_FUNCTION_SET | LCD_4BIT_MODE])
    sleep_ms(1)   
    lcd_command([LCD_FUNCTION_SET | LCD_4BIT_MODE | LCD_2LINE_MODE | LCD_5X8_FONT])
    sleep_ms(1)   
    lcd_command([LCD_DISPLAY_CONTROL])
    sleep_ms(1)   
    lcd_command([LCD_CLEAR_DISPLAY])
    sleep_ms(2)  
    lcd_command([LCD_ENTRY_MODE_SET, 0x06])  # Cursor move direction
    
def lcd_backlight(on):
    backlight.value(on)

def lcd_write(data):
    data_bytes = [ord(char) for char in data]
    i2c.writeto(LCD_ADDR, bytes([0x40] + data_bytes))

def lcd_clear():
    lcd_command([LCD_CLEAR_DISPLAY])
    sleep_ms(2)  

def lcd_move_to(x, y):
    if y == 0:
        addr = 0x80 + x
    else:
        addr = 0xC0 + x
    lcd_command([addr])

def main():
    lcd_init()
    lcd_backlight(True)
    while True:
        lcd_clear()
        lcd_write("Youssef")
if __name__ == "__main__":
    main()


BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT