Ultrasonic Sensor HC-SR04 and Arduino

In this Arduino Tutorial we will learn how the HC-SR04 Ultrasonic Sensor works and how to use it with the Arduino Board. The following video shows the results of this project read the tutorial below for more details.

Overview

The Ultrasonic Sensor emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

The speed of sound is approximately 341 meters (1100 feet) per second in air. The ultrasonic sensor uses this information along with the time difference between sending and receiving the sound pulse to determine the distance to an object. It uses the following mathematical equation:

Distance = Time x Speed of Sound divided by 2

Time = the time between when an ultrasonic wave is transmitted and when it is received
You divide this number by 2 because the sound wave has to travel to the object and back.

When you have finished reading this tutorial, you will be familiar with:

  • Connecting and using HC-SR04 Ultrasonic Sensor
  • Connecting and using a I2C LCD

What You Will Need

We will need the following parts and libraries in this tutorial:

Parts:

Libraries:

The following libraries will be used in this tutorial. Please refer to this tutorial on how to add an Arduino library in eclipse

We will be using Eclipse for Arduino, if you haven’t setup please refer to my previous tutorial on eclipse for Arduino.

Circuit Schematics

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

Source Code

First we need to include the NewPing library which can be downloaded from here, then define the pin numbers to which the TRIGER and ECHO pins are connected and create a NewPing object. We also have to add the I2C library and initialize it in order to print the results. Using the ping_cm(function we will read the data from the sensor and put the values of the distance of the blocking object into the distance  variables. At the end we will print the distance value on the LCD.

 

42 thoughts on “Ultrasonic Sensor HC-SR04 and Arduino”

  1. Wow that was strange. I just wrote an extremely long comment
    but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say excellent blog!

  2. Hello there, just became aware of your blog through Google,
    and found that it is really informative. I am going to watch out for brussels.
    I’ll appreciate if you continue this in future.
    Many people will be benefited from your writing. Cheers!

  3. We are a group of volunteers and starting a brand new scheme in our
    community. Your site provided us with helpful info to work on. You’ve done a
    formidable task and our entire group shall be grateful to you.

  4. Hi, I do believe this is an excellent web site.
    I stumbledupon it 😉 I may revisit once again since I bookmarked
    it. Money and freedom is the best way to change, may you be rich
    and continue to help others.

  5. Hi there, i read your blog occasionally and i own a similar
    one and i was just wondering if you get a lot of spam responses?
    If so how do you protect against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any support is very
    much appreciated.

  6. Hello, I think your blog might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, great blog!

  7. First off I want to say excellent blog! I had
    a quick question which I’d like to ask if you don’t mind.
    I was interested to find out how you center yourself and clear your mind before writing.
    I have had a difficult time clearing my mind in getting my
    ideas out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes tend to be lost
    simply just trying to figure out how to begin. Any recommendations or tips?
    Thank you!

  8. Hello, Neat post. There is an issue along with your website in internet explorer, might check
    this? IE still is the market chief and a large component to other folks will
    pass over your wonderful writing due to this problem.

  9. I wanted to thank you for this good read!! I certainly enjoyed every bit of it.

    I have got you bookmarked to check out new things you post…

  10. I know this website gives quality depending articles or reviews and extra
    information, is there any other web site which provides these things in quality?

  11. Aw, this was an incredibly good post. Taking the time and
    actual effort to make a superb article… but what can I say…
    I hesitate a lot and don’t seem to get nearly anything done.

  12. Terrific post however , I was wondering if you could write a
    litte more on this topic? I’d be very thankful if you could elaborate a little bit further.
    Appreciate it!

  13. Good day! I could have sworn I?ve visited this web site
    before but after looking at some of the posts I realized it?s new to me.
    Nonetheless, I?m certainly pleased I discovered it and I?ll be book-marking it and checking back frequently!

  14. Write more, thats all I have to say. Literally,
    it seems as though you relied on the video to make
    your point. You obviously know what youre talking about,
    why throw away your intelligence on just posting videos to your weblog when you
    could be giving us something enlightening to read?

  15. Hello there, just became aware of your blog through Google, and found that it is
    truly informative. I am going to watch out for brussels.
    I will be grateful if you continue this in future.
    Lots of people will be benefited from your writing.
    Cheers!

  16. Hey there! I know this is knd of off-topic howeverr I had to
    ask. Does running a well-established blog such as yours
    take a lot of work? I’m completely new to blogging but I do write in my journal every day.
    I’d like to start a blog so I can easily share my personal experience
    and feelings online. Please let me know if you have any recommendations or
    tips for brand new aspiring blog owners. Thankyou!

  17. What’s up colleagues, how is all, and what you want to say regarding this
    article, in my view its in fact amazing in favor of me.

  18. Usually I do not read post on blogs, however I would like to
    say that this write-up very pressured me to take a look
    at and do it! Your writing style has been amazed me. Thank you, very nice
    article.

  19. Hi there to all, the contents existing at this web site are truly remarkable for people experience, well, keep up the good work fellows.

Leave a Reply

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