Introduction: How to Use the HC-SR04 Ultrasonic Sensor - Arduino Tutorial

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm (or 1” to 13 feet). It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

In this tutorial you will learn how to use this sensor with the Arduino uno and print the distance from an object to the serial monitor.

Step 1: What You Will Need

For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • HC-SR04 Ultrasonic Sensor

Step 2: The Circuit

The connections are pretty easy, see the image above with the breadboard circuit schematic.

Step 3: The Code

Here's the code, embedded using codebender!

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino board with this sketch. And that's it, you've programmed your Arduino with this sketch.

  • ultrasonic.Ranging(CM) will return distance from an object to centimeters
  • ultrasonic.Ranging(INC) will return distance from an object to inches
  • ultrasonic.Timing() will return the time (ms) where the signal took to return from the object

Press connect button below to start serial communication.

Step 4: Well Done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use the HC-SR04 Ultrasonic Sensor with Arduino uno.

I hope you liked this, let me know in the comments. There will be more of them, so make sure to click Follow button!

Comments

author
DiegoS168 made it! (author)2017-04-15

i have this problem, i dont know what it is... can anyone help me?

Here is an image of the problem

789.png
author
KatiaA5 made it! (author)2016-11-17

Thanks for showing this. It works!

IMG_5437.JPG
author
Ryan tan (author)2016-09-14

What is the code to make it move to the object? Anyone teach please.thx.

author
GabrielC115 (author)2016-08-24

mine doesnt work, please help

author
KapishG made it! (author)2016-07-26

straight forward tutorial. thanks

temp_-1677940419.jpg
author
esilky made it! (author)2016-05-02

Thank you for the straightforward project.

What has your general luck been with the HC-SR04? I have three and only one works reliably. Of the other two, one reports values all over the place and the other works okay for about 10 seconds and then just reports 0 until you tap it with your finger.

I know these are pretty inexpensive devices, but not so much if you have to buy three to get one that works.

Just trying to figure out if other people have had the same experience, or should I buy another batch and see how they work?

Arduino Sonar project running - 4.jpg
author
dbalduini made it! (author)2016-04-06

Working great, thanks

7615c5cb-2213-4ae1-81e0-79e2c428ec7a.jpeg
author
Mennov1 (author)2016-03-21

It keeps spamming 0CM in my console :(

author
DonEng (author)2015-12-16

very nice and useable, but what is the thing on top of the arduino on the photo? and why do you use it?

hope you can help me out!

thanks,
Don

author
AdamW5 (author)2015-11-20

Any idea how to waterproof this idea/project?

Basically, sense the depth of a tank of water or bottom of a lake using an arduino + ultrasonic sensor.

author
pruthvi.monarch (author)2015-09-06

IMADE

author
vaibhavshinde (author)2015-07-26

effective in understanding

author

Thank you!

author
royal_robot (author)2015-07-23

Completed. :)

author

Nice ;)

About This Instructable

14,346views

269favorites

License:

Bio: Arduino Tutorials by Codebender.cc Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: