import time
import board
import pulseio
# Define ultrasonic sensor pins
trigger_pin = board.D18
echo_pin = board.D24
# Create objects for trigger and echo
trigger = pulseio.Output(trigger_pin)
echo = pulseio.Input(echo_pin)
def measure_distance():
# Set trigger to HIGH for 10 microseconds
trigger.value = True
time.sleep(0.00001)
trigger.value = False
# Start timer when echo goes HIGH
start_time = time.monotonic()
while not echo.value:
start_time = time.monotonic()
# Stop timer when echo goes LOW
end_time = time.monotonic()
while echo.value:
end_time = time.monotonic()
# Calculate pulse duration and distance
pulse_duration = end_time - start_time