from machine import Pin, time_pulse_us
import utime
TRIG = Pin(20, Pin.OUT)
ECHO = Pin(21, Pin.IN)
LED = Pin(15, Pin.OUT)
while True:
TRIG.low()
utime.sleep_us(2)
TRIG.high()
utime.sleep_us(10)
TRIG.low()
duration = time_pulse_us(ECHO, 1)
distance = (duration * 0.0343) / 2
print("Distance:", distance, "cm")
if distance > 320:
LED.on()
else:
LED.off()
utime.sleep(1)