After the button controls the LED to turn on and off, we continue to implement an ultrasonic sensor to measure the distance. This function is often used to calculate the distance between itself and the object in front, such as the obstacle avoidance function of a robot or a moving vehicle to ensure Robots or vehicles will not collide with objects in front.
understand the above After the principle, it should be easier to understand by looking at the following program.
The functions used in this program, mainly utime, are explained as follows:
[Material]
- Raspberry Pi Pico x1
- Ultrasonic sensor HCSR04 x1
- Flat cable x4
[Wiring diagram]
HC-SR04 Trigger pin (Trig) is used to send current pulse, so Pico pin is set as output. The Echo pin receives the reflected pulse, so the Pico pin is set as an input.Pi Pico接腳 | HC-SR04 |
---|---|
Pin 36(+3.3V) | VCC |
Pin 38(GND) | GND |
Pin 19(GPIO14) | Trig |
Pin 20(GPIO15) | Echo |
[Code]
Before programming, there is a theorem that needs to be understood when using ultrasonic waves. It is the propagation speed of sound speed in the air. Using the speed of ultrasonic waves traveling in the air, how long does it take to calculate the distance to an object, because in Celsius The speed of sound at sea level at zero degrees is approximately 331.5 meters per second. For every increase of 1 degree Celsius, the speed of sound increases by 0.607 meters per second. A formula can be listed:- The speed of sound at 20 degrees Celsius is approximately: 331.5 + 0.607*20 = 343.64 meters/second
- the speed of sound meters/second converted to centimeters/microseconds: 343.64 * 100/1000000 = 0.034364 cm/microseconds
- the distance of ultrasound transmission is back and forth, so the one-way distance = the distance of the time difference / 2
understand the above After the principle, it should be easier to understand by looking at the following program.
The functions used in this program, mainly utime, are explained as follows:
- utime.sleep_ms(n): Delay n milliseconds, which should be a positive number or 0.
- utime.sleep_us(n): Delay n microseconds, should be a positive number or 0.
- utime.ticks_us(): Make it contain a timestamp in microseconds.
from machine import Pin import utime trigger = Pin(14, Pin.OUT) echo = Pin(15, Pin.IN) def ultra(): #Create a function utime.sleep_us(2) #Pause for two milliseconds to ensure the previous setting The low potential has completed the trigger . high () trigger.high() utime.sleep_us(5) # After pulling the high potential, wait for 5 milliseconds, and immediately set to Low trigger.low() while echo.value() == 0: #Create a while loop to check whether the echo pin is 0, record the time signaloff = utime.ticks_us() while echo.value() == 1: #Create a while loop to check Whether the echo pin value is 1, record the time signalon = utime.ticks_us() timepassed = signalon - signaloff #Calculate the time difference between sending and receiving #Sonic travel time x speed of sound (343.2 m/s , Which is 0.0343 cm per microsecond), and the back-and-forth distance is divided by 2 distance = (timepassed * 0.0343) / 2 print("The distance is : ",distance,"cm") while True: ultra() utime.sleep(1) #wait for 1 second