All post of ElecFreaks

HC-SR04 Demo For Arduino

By May 11, 2011 21 Comments
Ultra01_01

Now, we are pleased to introduce the Ultrasoni modules HC-SR04 to you, which has stable performance and high  accuracy , making it a popular module in electronic market.  Compared to the Shap IR ranging modules, HC-SR04 is more cost-effective than it , but it has the same  accuracy and longer  distance.

The HC-SR04 module is easy to use and we has a simple Aruduino demo for the beginning user.

Soon, we will provide another Ultar Sonic Modules SDM-IO, which has faster response and no Blind Area. Stay tuned.

First, it should define two kinds of pins for trigger, 8 for trigger and 9 for echo.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define CM 1      //Centimeter
#define INC 0     //Inch
#define TP 8      //Trig_pin
#define EP 9      //Echo_pin  

void setup()
{
             pinMode(TP,OUTPUT);       // set TP output pin for trigger
             pinMode(EP,INPUT);        // set EP input pin for echo
             Serial.begin(9600);      // init serial 9600
             Serial.println("-------------------Ultra_Demo_Start---------------------------------");
}
void loop()
{
             long microseconds = TP_init();   // trigger and receive
             Serial.print("microseconds = ");
             Serial.println(microseconds);
             long distacne_cm = Distance(microseconds, CM); // Calculating the distance
             Serial.print("Distacne_CM = ");
             Serial.println(distacne_cm);   // printf the distance about CM
             delay(3000);
}

Second, init the trigger signal, you only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo.You can calculate the range through the time interval between sending trigger signal and receiving echo signal. the range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.Then we could Calculating the distance, because we have the pulse width and ues Formula:

Distance_CM= ((Duration of high level)*(Sonic :340m/s))/2.

So, we have bellow othre  two function:


1
2
<span style="color: #888888;"> long Distance(long time, int flag) { long distacne; if(flag) distacne = time /29 / 2 ; // Distance_CM = ((Duration of high level)*(Sonic :340m/s))/2 // = ((Duration of high level)*(Sonic :0.034 cm/us))/2 // = ((Duration of high level)/(Sonic :29.4 cm/us))/2 else distacne = time / 74 / 2; // INC return distacne; } long TP_init() { digitalWrite(TP, LOW); delayMicroseconds(2); digitalWrite(TP, HIGH); // pull the Trig pin to high level for more than 10us impulse delayMicroseconds(10); digitalWrite(TP, LOW); long microseconds = pulseIn(EP,HIGH); // waits for the pin to go HIGH, and returns the length of the pulse in microseconds return microseconds; // return microseconds }</span></pre>
<pre>
 

There, you can change the trigger pin and echo pin thougth marco define and also use CM or ICN , than you get the distance show as centimeter or inch, we just show you CM demo.OK, now you get the distance in centimeter . It’s very easy , right ?

If you have any question , please send email to us at services@elecfreaks.com

The more information is the datasheet:

Download the Datasheet from here.

Download the Demo for Arduino.

VN:F [1.9.13_1145]
Rating: 9.8/10 (10 votes cast)
VN:F [1.9.13_1145]
Rating: +2 (from 4 votes)
HC-SR04 Demo For Arduino, 9.8 out of 10 based on 10 ratings
ElecFreaks

Author ElecFreaks

More posts by ElecFreaks

Join the discussion 21 Comments

Leave a Reply