EasyManua.ls Logo

ADEEPT PiCar-Pro - Page 93

ADEEPT PiCar-Pro
178 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
90
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) # Set the input terminal of the module to high level and send out an
initial sound wave.
3. time.sleep(0.000015)
4. GPIO.output(Tr, GPIO.LOW)
5.
6. while not GPIO.input(Ec): # When the module no longer receives the initial sound wave
7. pass
8. t1 = time.time() # Write down the time when the initial sound wave was emitted.
9. while GPIO.input(Ec): # When the module receives the return sound wave.
10. pass
11. t2 = time.time() # Write down the time when the return sound wave was captured.
12.
13. return round((t2-t1)*340/2,2) # Calculate the distance.
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.
1. for i in range(10):
2. print(checkdist())
3. time.sleep(1)
The ultrasonic module measures the distance and displays the data on the console.

Related product manuals