from machine import Pin, SoftI2C
from hcsr04 import HCSR04
from lib_lcd1602_2004_with_i2c import LCD
from time import sleep
sensor = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=100000)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=1000000)
lcd = LCD(i2c)
led1 = Pin(17, Pin.OUT)
led2 = Pin(4, Pin.OUT)
led3 = Pin(2, Pin.OUT)
while True:
distance_cm = sensor.distance_cm()
lcd.clear()
lcd.puts("Distance: {:.1f} cm".format(distance_cm), 0, 0)
if distance_cm < 50:
lcd.puts("Close", 1, 0)
led1.on()
led2.off()
led3.off()
elif distance_cm > 150:
lcd.puts("Far", 1, 0)
led1.off()
led2.on()
led3.off()
else:
lcd.puts("Normal", 1, 0)
led1.off()
led2.off()
led3.on()
sleep(2)