Using the HC-SR04 U...
 
Notifications
Clear all

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

3 Posts
3 Users
2 Likes
2,955 Views
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1051
Topic starter  

Learn everything you need to know to use the HC-SR04 Ultrasonic Distance Sensor with an Arduino.

Get more info and all the sketches at https://dbot.ws/hcsr04ar

The HC-SR04 Ultrasonic sensor is a 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 microcontrollers 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 test arrangement I set up on my workbench and we’ll use that to measure the accuracy of the sensor.

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 its full potential. Because of this, it’s pretty long, so here is a breakdown of the contents if you’d like to skip ahead:

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

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.

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
YurkshireLad reacted
Quote
Sumanta
(@sumanta)
Member
Joined: 3 years ago
Posts: 197
 

With one arrow, you have hit two sparrows. 😉 

How to use ultrasonic sensor along with digital temperature and humidity sensor!!!! 😀 


   
lazzybot reacted
ReplyQuote
lazzybot
(@lazzybot)
Member
Joined: 3 years ago
Posts: 1
 

Sir this video and techniques are seems to be very help full in my current project . Thank you so much for explaining everything is such a simple way. You are a very good teacher sir .

Sir the given links 👇

Are not working anymore.

 

So please help for the codes and pin diagram.


   
ReplyQuote