The use of the ultrasonic distance measuring module for Raspberry Pi Hc-sro4

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.