import HCSR04
from machine import Pin, SoftI2C
from i2c_lcd import I2cLcd
from time import sleep
led = Pin(4, Pin.OUT)
I2C_ADD = 0x27
TotalRows = 2
TotalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=1000)
lcd = I2cLcd(i2c, I2C_ADD, TotalRows, TotalColumns)
sensor = HCSR04.HCSR04(trigger_pin=12, echo_pin=14, echo_timeout_us=10000)
while True:
distance = sensor.distance_cm()
if distance < 10:
led.on()
print("Led ON")
lcd.putstr("Led ON")
time.sleep(2)
if distance > 10:
led.off()
print("Led OFF")
lcd.putstr("Led OFF")
timw.sleep(1)