from time import sleep
from machine import I2C, ADC, Pin, PWM
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04
AddressOfLcd = 0x27
i2c = I2C(0, scl=Pin(19), sda=Pin(18), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
ultrasonic = HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=1000000)
led = Pin(2, Pin.OUT)
buzzer = PWM(Pin(15, Pin.OUT))
buzzer.freq(4186)
buzzer.duty(0)
while True:
distance = ultrasonic.distance_cm()
print('Distance:', distance, 'cm', '|', distance/2.54, 'inch')
lcd.clear()
lcd.putstr('Distance= {:.2f} cm'.format(distance))
if distance <= 200:
buzzer.duty(512)
led.on()
else:
buzzer.duty(0)
led.off()
sleep(5)