Using the HC-SR04 Ultrasonic Distance Sensor with Arduino – Everything you need to know!

Get more info and all the sketches at https://dbot.ws/hcsr04ar
More projects and tutorials at https://dronebotworkshop.com

The HC-SR04 Ultrasonic sensor is versatile device that has become a staple in robotics projects. This inexpensive component measures the distance between itself and the nearest solid object using pulses of ultrasonic sound. It has reasonable accuracy “out of the box” and can be made even more accurate with one additional component.

In this tutorial we’ll learn how to use the HC-SR04 with an Arduino. As it turns out there is more than one way to do this, so we’ll examine them all. You can apply the same knowledge to use the sensor with other microcontroller or with a microcomputer like the Raspberry Pi.

We’ll start by learning how the HC-SR04 works and how its input and outputs function.

Next we’ll connect it to an Arduino and write a simple sketch to make it work. I’ll show you the htest arrangement I set up on my workbench and we’ll use that to measure the sensors accuracy.

Then we’ll move on to some improved code using a “code library” called “NewPing”. If you are new to the Arduino and have not used a code library before then this is an excellent opportunity to learn how to increase your programming expertise. Actually we will end up using three code libraries by the time the video has finished and I’ll show you two different methods of doing this.

While the sensor is reasonably accurate there is room for improvement, as our bench testing will confirm. I’ll show you how you can add an inexpensive temperature and humidity sensor to increase the accuracy of the HC-SR04 by compensating for the change in the speed of sound due to the ambient temperature and humidity.

We will also see how the HC-SR04 can be used in “3-Wire Mode”, eliminating one connection to the Arduino without sacrificing performance. This is great information if you need to save an Arduino I/O pin for another application.

And finally we will see how we can use multiple HC-SR04’s in the same circuit, a common requirement when building a robot.

This video (and it’s accompanying article at http://dbot.ws/hc-sr04) has pretty well everything that you’d ever need to know to use the HC-SR04 at it’s full potential. Because of this it’s pretty long, so here is a breakdown of the contents if you’d like to skip ahead:

How the HC-SR04 Works – 1:46
Basic Arduino Hookup – 8:21
Sketch 1 – Basic Hookup Demo – 8:53
NewPing Code Library – 14:03
Sketch 2 – First NewPing Test – 14:21
Sketch 3 – NewPing with Duration – 19:20
Sketch 4 – NewPing with Iterations – 21:56
Getting Improved Accuracy – 24:37
Using the DHT22 – 26:47
Sketch 5 – DHT22 Test – 28:31
Sketch 6 – Improved HC-SR04 with DHT22 – 33:06
The HC-SR04 in 3-Wire Mode – 38:03
Sketch 7 – HC-SR04 in 3-Wire Mode – 38:54
Using Multiple HC-SR04 Sensors – 39:51
Sketch 8 – Multiple HC-SR04 Test – 41:08

Remember, you can get all the sketches and more information on the website at https://dbot.ws/hcsr04info . And while you are on the DroneBot Workshop website be sure to check out all the other articles. I’ve added a “Resources” section to all of the articles to give you downloads and handy links, hopefully you’ll find this useful.

As always please comment below and please subscribe!

source

You might be interested in