from machine import Pin, time_pulse_us
from time import sleep_us, sleep
from utime import sleep
# Define the GPIO pin numbers for the trigger and echo pins.
ECHO_PIN = 13
TRIGGER_PIN = 1
led1 = Pin(28,Pin.OUT)
led2 = Pin(27,Pin.OUT)
led3 = Pin(26,Pin.OUT)
# Initialize trigger and echo pins.
trigger = Pin(TRIGGER_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
def measure_distance():
# Ensure trigger is low initially.
trigger.low()
sleep_us(2)
# Send a 10 microsecond pulse to the trigger pin.
trigger.high()
sleep_us(10)
trigger.low()
# Measure the duration of the echo pulse (in microseconds).
pulse_duration = time_pulse_us(echo, Pin.high)
# Calculate the distance (in centimeters) using the speed of sound (343m/s).
distance = pulse_duration * 0.0343 / 2
return distance
def main():
while True:
# Measure the distance and print the value in centimeters.
distance = measure_distance()
print("Distance: {:.2f} cm".format(distance))
if(0<=distance<100):
led1.on()
led2.off()
led3.off()
elif(10<=distance<200):
led1.off()
led2.on()
led3.off()
elif(200<=distance):
led1.off()
led2.off()
led3.on()
# Wait for 1 second before taking the next measurement.
sleep(1)
if __name__ == "__main__":
main()