from machine import pin, time_pulse_us
import time
#Pin setup
trigger = Pin(4, Pin .OUT)
echo =Pin(5, Pin.IN)
# Function to measure distance
def get_distance():
trigger.low()
time.sleep_us(2)
trigger.high()
time.sleep_us(10)
trigger.low()
duration= time.time_pulse(echo, 1, 30000)
if duration < 0:
return none
distance = duration/ 58.0
return distance
while True:
distance = get_()
if distance is not none:
# print to screen
print('Distance: {:.2f} cm '.format(distance))