from machine import Pin, time_pulse_us
import time
trig = Pin(23, Pin.OUT)
echo = Pin(24, Pin.IN)
def get_distance():
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
duration = time_pulse_us(echo, 1)
distance = (duration * 0.0343) / 2
return distance
while True:
d = get_distance()
print("Distance:", d, "cm")
time.sleep(1)