HC-SR04 Demo For Arduino

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.


#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:


<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 (9 votes cast)
VN:F [1.9.13_1145]
Rating: +3 (from 3 votes)
HC-SR04 Demo For Arduino, 9.8 out of 10 based on 9 ratings

Related Posts

19 Comments

  1. get redirected here

    Thanks again for the blog.Really thank you! Really Cool.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  2. learn here

    “Thanks for sharing, this is a fantastic blog post.Thanks Again. Cool.”

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  3. click the following article

    I really liked your article post.Really thank you! Cool.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  4. just click the following website

    “Great, thanks for sharing this article post.Really looking forward to read more. Great.”

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  5. car check dvla

    We are a group of volunteers and opening a new scheme in our community.
    Your web site provided us with valuable information to work on.
    You have done an impressive job and our whole community will be thankful to you.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  6. Tyrone

    Hey there! Do you know if they make any plugins to protect
    against hackers? I’m kinda paranoid about losing everything I’ve
    worked hard on. Any suggestions?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  7. Pingback: louis vuitton outlet uk

  8. Tilly

    I drop a comment each time I especially enjoy a article on a website
    or I have something to valuable to contribute to
    the conversation. It is a result of the fire communicated in the article I read.
    And on this post HC-SR04 Demo For Arduino | ElecFreaks.
    I was excited enough to drop a thought ;-) I actually do have
    a couple of questions for you if you don’t mind. Could it be simply me or does it appear like a few of the remarks appear as if they are coming from brain dead folks? :-P And, if you are writing at additional social sites, I’d like to follow anything fresh you have to post.

    Would you list every one of your community pages
    like your Facebook page, twitter feed, or linkedin profile?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  9. Pingback: เรื่องราว เรื่องเล่า… จากกิจกรรม Arduino Camp (Innovation BUEN Camp 2013 (14 – 15 พฤษภาคม 2556)) | MyTop.Kullawattana

  10. Pingback: เรื่องราว เรื่องเล่า… จากกิจกรรม Arduino Camp (Innovation BUEN Camp 2013 (14 – 17 พฤษภาคม 2556)) | MyTop.Kullawattana

  11. ganesh kumar arya

    how to purchase this module HC-SR04. from which shop?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  12. Barrett

    Do you have a demo for Gadgeteer? That would be very useful!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  13. June

    Thanks for sharing your info. I truly appreciate your efforts and
    I will be waiting for your next write ups thanks once again.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  14. private krankenversicherung test

    It is not my first time to pay a quick visit this website, i am browsing this site dailly and take pleasant data from here every
    day.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
    Reply
  15. günstige pkv

    I have been exploring for a little bit for any high-quality articles or weblog posts in this kind of
    house . Exploring in Yahoo I finally stumbled upon this site.
    Studying this information So i am glad to exhibit that I have a very
    excellent uncanny feeling I came upon exactly what I needed.

    I most indisputably will make certain to do not overlook this website and give it a look
    regularly.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  16. gianmarco lorenzi

    I do think, so long as there exists a today, at this time is invariably the actual setting up range.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    Reply
  17. Pingback: Test Minute – Boulder Dash-XL « silencepc

  18. Elias835

    Hi Ho forum mitglieder

    unterschied private und gesetzliche krankenversicherung und gesetzliche krankenkasse vergleich
    pkv beamter
    rechner private krankenversicherung
    Elias

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -2 (from 2 votes)
    Reply
  19. Pingback: SDM-IO Non-Blind Area Ultrasonic Distance Sensor Module | ElecFreaks

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Go To Top