from machine import Pin, I2C
from time import sleep
from i2c_lcd import I2cLcd
# Inisialisasi I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 4, 20)
# Periksa perangkat I2C
devices = i2c.scan()
if devices:
print("I2C devices found:", devices)
else:
print("No I2C devices found")
# Inisialisasi LED
red_led = Pin(25, Pin.OUT)
yellow_led = Pin(26, Pin.OUT)
green_led = Pin(27, Pin.OUT)
def show_on_lcd(message):
lcd.clear()
sleep(0.1)
lcd.putstr(message)
sleep(0.1)
def traffic_light():
while True:
red_led.on()
show_on_lcd("STOP")
sleep(5)
red_led.off()
yellow_led.on()
show_on_lcd("READY")
sleep(2)
yellow_led.off()
green_led.on()
show_on_lcd("GO!")
sleep(5)
green_led.off()
# Jalankan traffic light
traffic_light()