ADEEPT PiCar-Pro Manual page 93

Table of Contents

Advertisement

For the complete code, please refer to the file ultra.py.
1.
import
RPi.GPIO as GPIO
2.
import
time
3.
4.
Tr = 11
# The pin number of the input end of the ultrasonic module
5.
Ec = 8
# Pin number of the output end of the ultrasonic module
6.
7.
GPIO.setmode(GPIO.BCM)
8.
GPIO.setup(Tr, GPIO.OUT,initial=GPIO.LOW)
9.
GPIO.setup(Ec, GPIO.IN)
Import the dependent library and initialize it.
1.
def
checkdist():
2.
GPIO.output(Tr, GPIO.HIGH)
initial sound wave.
3.
time.sleep(0.000015)
4.
GPIO.output(Tr, GPIO.LOW)
5.
6.
while not
GPIO.input(Ec):
7.
pass
8.
t1 = time.time()
9.
while
GPIO.input(Ec):
10.
pass
11.
t2 = time.time()
12.
return
13.
round((t2-t1)*340/2,2)
Define the ranging function. When the ultrasound module captures ultrasound, its output
terminal will output high frequency. The GPIO.input method can determine whether the output
terminal is outputting high-frequency signals.
for
in
1.
i
range(10):
2.
print(checkdist())
3.
time.sleep(1)
The ultrasonic module measures the distance and displays the data on the console.
# Set the input terminal of the module to high level and send out an
# When the module no longer receives the initial sound wave
# Write down the time when the initial sound wave was emitted.
# When the module receives the return sound wave.
# Write down the time when the return sound wave was captured.
# Calculate the distance.
90

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the PiCar-Pro and is the answer not in the manual?

Table of Contents

Save PDF