SR04 Acoustic Sensor principle and Arduino programming--everyone can play hardware _arduino

Source: Internet
Author: User
Tags prepare

Through the first two articles, we have become familiar with the basic GPIO programming of Arduino and the serial communication between PC and Arduino. Next we're going to start learning all kinds of sensor operations while delving into Arduino programming.

Before we start programming, we need to be familiar with the principles of SR04. Here is a digression: the vast majority of sensor circuits and interfaces are standard, as long as the same model, online documents are the same. Here we use SR04 acoustic sensors, a treasure can buy, not necessarily expensive on the good, learn to use words to buy a price is not expensive on the line.

SR04 working principle and interface


SR04 is a common kind of acoustic sensor, can be used to measure the distance, but the actual use is not accurate, not suitable for accurate ranging (accurate ranging with laser ranging module, a treasure has a sharp production of laser ranging module, seventy or eighty to hundreds of pieces of all have, early learning does not need to use so expensive, and interface communication Complex, Not suitable for getting started, theoretically SR04 can reach 10 meters, the actual most nominal is four five meters, but the actual use of the environmental impact, Bo Master now hand SR04 is only three meters within the more accurate, three meters above the range can not be measured.

SR04 have four pins, VCC, GND (or symbols like 〨), Echo, Trig. VCC can generally connect 3.3v-5v power, GND grounding is the negative, you can directly connect the Arduino on the 5V and GND pin power supply. Trig is the starting signal pin, the use of the time to give him 10us above the high level, and then pull down, SR04 will send eight 40khz of ultrasound, at this time, we directly to the echo pull low (so-called pull high pull low is output high level or output low level), And then we start to wait for the echo to get taller and we start to clock until he gets lower, and when it gets low, we can calculate the distance from that time.

To put it simply: Trig pull higher than 10us, "trig", "Echo becomes High" begins to Time "echo goes Low" to end the timer. The distance can be obtained by calculating the calculated time. Calculation formula is distance = calculated time (in seconds) x340 m/s ÷2

Note that the trig is higher than 10us.

That is to say, you can also pull high 1ms,1s. It can only be more or less.

If you only care about how to program, so far enough, you can directly look at the back of the Arduino programming section. If you also want to know SR04 work process please continue to look down.

SR04 has two speakers, trig pull higher than 10us and then pull lower, SR04 will think that received the start signal, one of the speakers will emit a specific frequency of ultrasound, sent after the high echo pin, the other speaker to accept this ultrasound, in the acceptance of the end of the echo pin down. At this point, Echo's high level of time represents the return time of the ultrasound, because it is a round-trip, so the calculation process needs to be ÷2, into a one-way time, and then multiply this one-way time by the speed of sound at 340 m/s, to get the distance from the current position to the opposite barrier

Second, the Arduino programming of SR04

Here we first connect echo 13,trig to 12. The following figure (wiring for oneself drawn, Arduino and SR04 models originate from the network)


Next is the code section. But before the code, we need to know a pulse timer function Pulsein (pin,stat,timeout), the return value is a microsecond number

Where the pin is a listening port number, stat can choose low or high, indicating whether it is a low-level pulse or a higher level pulse, timeout is an optional parameter, the unit is microseconds, the default value is 1000*1000US, used to specify the pulse waiting time

such as Pulsein (12,high,1000) indicates that the high level pulse time is calculated from the number 12th and waits up to 1000US. At this point, the Arduino will listen to the 12th-port high level, 12th to start the time after the high, lower after the timer, and return the calculation of the microsecond number. If the wait exceeds the 1000us,12 number, the wait timeout returns 0


Once this function is clear, we can start programming.

int sr_echo=13;

int sr_trig=12; -----------------------------------------------////This is the distance get function//This function executes when the corresponding operation is obtained by SR04 to obtain the distance//---------------------
  --------------------------//Float get_distances () {float sr_time=0,sr_dis=0;        Digitalwrite (Sr_trig,low);			    Send the low level first, prepare the delay (1) for sending the starting signal;       Delay 1ms digitalwrite (Sr_trig,high);			    Send ready signal, prepare signal is above 10US high level, here we send 1ms high level delay (1);        Keep Port 12th High level 1ms digitalwrite (Sr_trig,low);    High level send, delay end, when we pull low level, in order to read back to do preparation Sr_time=pulsein (Sr_echo,high);	    Start reading the return time, that is, the echo read into the high level of time sr_time=sr_time/1000/1000;             The microsecond unit is converted to the second unit SR_DIS=SR_TIME*340/2;			    The return time is the round-trip time therefore wants besides 2 to obtain one way time, the speed of sound is 340m/s, therefore again by sr_dis*=100;
Convert the unit of rice into centimeter unit return Sr_dis; //-----------------------------------------------////This is setup//This function will be executed//when the MCU is energized or reset-------------------------    ----------------------//Void Setup () {//Put your setup code here, to run Once:pinmode (Sr_echo, INPUT);   Set Sr_echo for Input mode Pinmode (Sr_trig, OUTPUT); SetThe fixed Sr_trig is the output mode Serial.begin (115200); Set baud rate}//-----------------------------------------------////The function is a loop execution program that will be executed after Setup ()////When the function is completed and will be automatically rerun/ /-----------------------------------------------//Void loop () {//Put your main code here, to run repeatedly:serial
  . println ("read Data");
  float sr_dis=get_distances ();         Serial.println (Sr_dis);
Sending the measured distance delay (1000) to the PC via USB serial port; }

After the program upload, open the serial detection tool, you should be able to see the following situation (if you still do not use the serial port tool, suggest references to the previous article serial Bowen)


Carefully rotate your SR04, or use your hand to block it, compare the serial tool display of the numerical changes, and then read the code and comments, perhaps you can fully understand

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.