First, last picture.
The pin heel of this module is played before the three difference, in addition to VCC and GND two pins, but also more than two trig and echo pins, respectively, output and input, trig I was 20 pin, ECHO is 21
The module works by entering at least 10US of trigger signal to the trig foot, which emits 8 40kHz cycle levels and detects echoes. echo outputs a high-level echo signal once the echo signal is detected. The pulse width of the echo signal is proportional to the measured distance. The distance can be calculated by transmitting the signal to the received echo signal time interval. Formula: Distance = High Level * Sound velocity (340m/s)/2.
Here is the code
1 ImportRpi.gpio as GPIO2 Import Time3 4Trig_pin = 205Echo_pin = 216 7 Gpio.setmode (GPIO). BCM)8Gpio.setup (Trig_pin, GPIO. Out, initial =GPIO. Low)9 Gpio.setup (Echo_pin, GPIO. IN)Ten OneTime.sleep (2) A - defcheckdist (): - gpio.output (Trig_pin, GPIO. High) theTime.sleep (0.000015) - gpio.output (Trig_pin, GPIO. Low) - while notGpio.input (echo_pin): - Pass +T1 =time.time () - whileGpio.input (echo_pin): + Pass At2 =time.time () at return(T2-T1) *340/2 - - Try: - whileTrue: - Print 'distance:%0.2f M'%checkdist () -Time.sleep (3) in exceptKeyboardinterrupt: -Gpio.cleanup ()
found that the catch of the exception is for Crtl+c trigger, before my several modules if the output of the CTRL + C or will continue to output, I can only write a script to close the output of the corresponding pin, with this abnormal capture, it is more convenient
The actual measured distance is low.
Methods are copied, the original address: http://bbs.elecfans.com/forum.php?mod=viewthread&tid=451330
The use of the ultrasonic distance measuring module for Raspberry Pi Hc-sro4