Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the principle of ultrasonic module HC-SR04 and raspberry pie programming

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to understand the principle of ultrasonic module HC-SR04 and raspberry pie programming. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Ultrasonic ranging module HC-SR04 provides 2cm-400cm non-contact measurement function, and the ranging accuracy can reach 3mm. The module includes ultrasonic transmitter, receiver and control circuit. Basic principles of work:

(1) use IO triggers for at least 10us high-level signals

(2) the module automatically sends 8 40 kHz and detects whether there is a pulse signal returning.

(3) if the signal returns, through the high level, the high output IO duration is the time from sending the ultrasonic wave to the return. Test distance = (high level time × speed of sound (340M/S) / 2

The wires are directly connected as follows:

5 volt power supply

Trigger pulse input

Echo pulse output

0V grounding

Electric Parameter:

Trig and Echo are TTL levels, so they can be directly connected to the GPIO port of the TTL level.

Time sequence diagram

You only need to provide a short pulse of 10uS to the input of the trigger to start ranging.

Then, the module will send out 8 cycles of ultrasonic pulses at a frequency of 40 kHz and improve its echo.

The echo is a distance object whose pulse width is proportional to the distance.

You can calculate the range by the time interval between sending a trigger signal and receiving an echo signal. Formula: uS/58= cm or uS/148= inch; or: range = high water level time * speed (340M/S) / 2

We recommend using a measurement period of more than 60 milliseconds to prevent the trigger signal from becoming an echo signal.

5. Operation:

Set both trig and echo ports low during initialization

First send a high-level pulse of at least 10 us to the trig (the module automatically sends out 8 40K square waves)

Then wait, capture the rising edge of the echo output, while capturing the rising edge, turn on the timer to start the clock, wait for the falling edge of the echo to be captured again, and read out the time of the timer when the falling edge is captured

Because the time of the echo high level is equal to the time the ultrasonic wave runs in the air, the distance between the ultrasonic wave and the obstacle can be calculated according to the test distance = (high level time * speed of sound (340M/S)) / 2.

Connect to this module on the raspberry pie and write the program ultrasonic_hc-sr04.py:

#! / usr/bin/python#-*-coding: utf-8-*-import RPi.GPIO as GPIO import time SONIC_ECHO = 36SONIC_TRIG = 37 def checkdist (): # send trigger signal GPIO.output (SONIC_TRIG,GPIO.HIGH) # keep 10us above (I choose 20us) time.sleep (0.000020) GPIO.output (SONIC_TRIG GPIO.LOW) while not GPIO.input (SONIC_ECHO): pass # found that high level on time timing T1 = time.time () while GPIO.input (SONIC_ECHO): pass # High level end stop timing T2 = time.time () # return distance Unit: return (t2-t1) * 340/2GPIO.setmode (GPIO.BOARD) GPIO.setup (SONIC_TRIG,GPIO.OUT,initial=GPIO.LOW) GPIO.setup (SONIC_ECHO) GPIO.IN) time.sleep (2) while True: L_distance = checkdist () if L_distance > 10: pass else: print 'Distance:% 0.2f m'% L_distance time.sleep (0.5) GPIO.cleanup () on how to understand the principle of ultrasonic module HC-SR04 and raspberry pie programming. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report