Search code examples
pythontimeraspberry-pidisplaylcd

how to get a raspberry pi to display the current time


how can I get a raspberry pi 16x2 charter display show the current time in the EST time zone using the "rpi_lcd" library, thank you in advanced


Solution

  • Very easy, you just need to use datetime and create a custom time format and pass it to the lcd.text function. This code assumes that your circuit connection is fine.

    import datetime
    import pytz
    from time import sleep
    from rpi_lcd import LCD
    
    lcd = LCD(width=16, rows=2)
    tz = pytz.timezone("US/Eastern")
    
    while True:
        lcd.text(datetime.datetime.now(tz=tz).strftime(
            "%y-%m-%d %H:%M:%S"), 1)  # for a 16x2 display
        sleep(1)
        lcd.clear()