# 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. leonore_ulm says:

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. phyllisstarr says:

Thankfulness to my father who stated to me concerning this blog, this website is in fact awesome.

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!

4. Hurrah, thɑt’s what I was searching for, what а іnformation! existing here at this web site, tһanks admin оf this website.

5. It’s very trouble-free to find out any topic on web as
compared to books, as I found this piece of writing at this web page.

6. 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.

7. I like this web blog so much, saved to my bookmarks.

8. You got a very superb website, Glad I observed it through yahoo.

9. 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.

10. Hi there i am kavin, its my first time to commenting anyplace, when i read this piece of writing i thought i could
also make comment due to this good paragraph.

11. User says:

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.

12. User says:

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!

13. 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!

14. Hi it’s me, I am also visiting this site daily, this web site
is really fastidious and the users are in fact sharing
fastidious thoughts.

15. Lovely just what I was searching for. Thanks to the author for taking his time on this one.

16. 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.

17. Hey very interesting blog!

18. 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…

19. 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?

20. I think the admin of this web page is actually working hard for his web site,
for the reason that here every information is quality based data.

21. Hello to every body, it’s my first pay a visit of this weblog; this website contains remarkable and truly good data
designed for visitors.

22. 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.

23. Hello, just wanted to say, I loved thijs article. It was inspiring.
Keep on posting!

24. I think the admin of this web page is genuinely working hard in favor of his web page, for the reason that here every material is
quality based material.

25. 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!

26. 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!

27. Anonymous says:

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

28. These are genuinely enormous ideas in about blogging.
You have touched some fastidious things here. Any way keep up
wrinting.

29. I’ve recentl started a web site, the information you offer
onn this web site has helped me tremendously.
Thanks for all of your time & work.

30. You made some nice points there. I looked on the internet for the subject and found most guys will go

31. I conceive other website proprietors should take this site as an model, very clean and superb user genial design and style.

32. 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!

33. I pay a quick visit day-to-day a few web pages and sites to read articles, but this website offers quality based content.

34. I like this site very much, Its a real nice position to read and obtaiin info.

36. 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!

37. 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.

38. 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.

39. Hi there mates, nice article and nice arguments
commented at this place, I am truly enjoying
by these.

40. Thank you for sharing your thoughts. I really appreciate your efforts
and I am waiting for your next write ups thank you once
again.

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