6.1 Measuring Distance¶
The ultrasonic sensor module works on the principle of sonar and radar systems for determining the distance to an object.
Bill of Materials
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Kepler Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
Raspberry Pi Pico W |
1 |
|
2 |
Micro USB Cable |
1 |
|
3 |
Breadboard |
1 |
|
4 |
Wires |
Several |
|
5 |
Ultrasonic Sensor Module |
1 |
Schematic
Wiring
Code
Note
Open the
6.1_measuring_distance.py
file under the path ofkepler-kit-main/micropython
or copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it.Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.
For detailed tutorials, please refer to Open and Run Code Directly.
import machine
import time
TRIG = machine.Pin(17,machine.Pin.OUT)
ECHO = machine.Pin(16,machine.Pin.IN)
def distance():
TRIG.low()
time.sleep_us(2)
TRIG.high()
time.sleep_us(10)
TRIG.low()
while not ECHO.value():
pass
time1 = time.ticks_us()
while ECHO.value():
pass
time2 = time.ticks_us()
during = time.ticks_diff(time2,time1)
return during * 340 / 2 / 10000
while True:
dis = distance()
print ('Distance: %.2f' % dis)
time.sleep_ms(300)
Once the program is running, the Shell will print out the distance of the ultrasonic sensor from the obstacle ahead.
How it works?
Ultrasonic sensors produce high frequency sound waves (ultrasonic waves) emitted by the transmitting probe. When this ultrasonic wave hits an object, it is reflected as an echo, which is detected by the receiving probe. By calculating the time from transmission to reception, the distance can be calculated.
Based on this principle, the function distance()
can be derived.
def distance():
TRIG.low()
time.sleep_us(2)
TRIG.high()
time.sleep_us(10)
TRIG.low()
while not ECHO.value():
pass
time1 = time.ticks_us()
while ECHO.value():
pass
time2 = time.ticks_us()
during = time.ticks_diff(time2,time1)
return during * 340 / 2 / 10000
Among them, the first few lines are used to transmit a 10us ultrasonic wave.
TRIG.low()
time.sleep_us(2)
TRIG.high()
time.sleep_us(10)
TRIG.low()
Then, the program is paused and the current time is recorded when the ultrasonic wave has been emitted.
while not ECHO.value():
pass
time1 = time.ticks_us()
Subsequently, the program is suspended again. After the echo is received, the current time is recorded once again.
while ECHO.value():
pass
time2 = time.ticks_us()
Finally, based on the time difference between the two recordings, the speed of sound (340m/s) is multiplied by the time to obtain double the distance between the ultrasonic module and the obstacle (i.e., one round trip of the ultrasonic waves from the module to the obstacle). Converting the units to centimeters gives us the return value we need.
during = time.ticks_diff(time2,time1)
return during * 340 / 2 / 10000
Note that the ultrasonic sensor will pause the program when it is working, which may cause some lagging when writing complex projects.