from machine import Pin
import time

# Define GPIO pins
TRIG = Pin(4, Pin.OUT)
ECHO = Pin(5, Pin.IN)

def get_distance():
    TRIG.value(1)
    time.sleep_us(10)
    TRIG.value(0)

    while ECHO.value() == 0:
        pulse_start = time.ticks_us()

    while ECHO.value() == 1:
        pulse_end = time.ticks_us()

    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 0.0343 / 2  # Convert to cm

    return round(distance, 2)

while True:
    dist = get_distance() 
    print(f"Distance: {dist} cm")
    time.sleep(1)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT