How to Use the HC-SR04 Ultrasonic Sensor - Arduino Tutorial
by codebender_cc in Circuits > Arduino
23642 Views, 273 Favorites, 0 Comments
How to Use the HC-SR04 Ultrasonic Sensor - Arduino Tutorial
![IMG_20150723_124456.jpg](/proxy/?url=https://content.instructables.com/F59/KNLQ/ICFKPOGH/F59KNLQICFKPOGH.jpg&filename=IMG_20150723_124456.jpg)
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.
What You Will Need
![Χωρίς τίτλο.png](/proxy/?url=https://content.instructables.com/FI3/QKBH/ICFKPOMT/FI3QKBHICFKPOMT.png&filename=Χωρίς τίτλο.png)
For this tutorial you will need:
- Arduino uno
- Breadboard
- HC-SR04 Ultrasonic Sensor
The Circuit
![6221889_orig.png](/proxy/?url=https://content.instructables.com/FFD/SWYS/ICFKPOPD/FFDSWYSICFKPOPD.png&filename=6221889_orig.png)
The connections are pretty easy, see the image above with the breadboard circuit schematic.
The Code
![Screenshot_3.jpg](/proxy/?url=https://content.instructables.com/FY2/LW71/ICFKPORL/FY2LW71ICFKPORL.jpg&filename=Screenshot_3.jpg)
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.
Well Done!
![IMG_20150723_124721.jpg](/proxy/?url=https://content.instructables.com/FPU/F7V2/ICFKPP20/FPUF7V2ICFKPP20.jpg&filename=IMG_20150723_124721.jpg)
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!