from time import sleep
from hcsr04 import HCSR04
from machine import I2C, ADC, Pin
from i2c_lcd import I2cLcd
AddressOfLcd = 0x27
sensor = HCSR04(trigger_pin=12, echo_pin=14)
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
led1 = Pin(4, Pin.OUT)
led2 = Pin(2, Pin.OUT)
while True:
distance = sensor.distance_cm()
print('Distance = {:.2f} cm'.format(distance))
lcd.clear()
lcd.putstr('Distance = {:.2f} cm'.format(distance))
if distance <= 250:
led1.on()
led2.off()
elif distance >= 300:
led1.off()
led2.on()
else:
led1.off()
led2.off()
sleep(5)