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

preview_player
Показать описание
Learn everything you need to know to use the HC-SR04 Ultrasonic Distance Sensor with an Arduino.

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.

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

As always please comment below and please subscribe!
Рекомендации по теме
Комментарии
Автор

2:50 pins
4:20 calculating distance
8:30 sensor to arduino connection

clementinedebeauvoir
Автор

Sir, you are a gifted teacher. I watch a ton of YT instructional videos on various subjects, and you are by far the best teacher out there. You speak slowly enough for novices to follow you, and you explain things clearly and completely. Thanks for your videos!

tzampini
Автор

Hands down this has to be the best tutorial regarding the Ultrasonic sensor relating to the Arduino. I have never before seen a better explained video tutorial regarding the matter.

engineered.mechanized
Автор

Thanks for this excellent tutorial. The best I found about the HC-SR04. Your explanations covered litteraly everything I needed to complete this project I had in mind and made my life so much easier.
I especially liked the way you cover every step of the process, the inbetween sketchs to confirm the hardware runs properly (ie.DHT22), every code lines are properly detailed and explained, slow enough to understand where you going but fast enough so it's still enternaining and the viewer doesnt feel he's considered a dummy like I saw on some other channels.
To sum it up I felt that was the perfect pace and detail level.
Thanks again for your great work. Please keep it up !

vincentdesjardins
Автор

"metrically impaired" - I died ;-)

zyghom
Автор

this is the best program available with maximum accuracy that I have got on internet it has highest accuracy with up to two decimle places, Thankyou sir for providing such an effective program.

YASHRAJ-ioqh
Автор

A perfect profesional tutorial. Clear language, easy to understand even with other mother tongue. And No stupid background music.This motivate to try it.
I could learn also a lot of your clean lab :-)
Kind regards, Otto

ottokueng
Автор

This channel has less subscribes that the ones that should had
I want to thank you because through your very detailed video tutorials you have helped me to learn more about programming

your videos are very well edited, scripted and relaxing so it is all the time pleasure to re watch them .

caslor
Автор

This lecture is just like being in engineering school again, I love it. Thanks for putting in all the work

zeel
Автор

The sensor also has a 15-degree wave sending range. If you hold the wooden board too close to the center of where the sensor is pointing, the wave also reflects off your fingers and this can also distort the results. Additionally, changing the angle at which the sensor and the board are positioned affects the results

Miszixx
Автор

All I can say is wow. I have been beating my head in spec. sheets and trying to put things together, lots of learning and burning. lol, Thank you for your non-judgemental casual approach.

cfsmithjr
Автор

Great vid! Loved the 'metrically impaired' statement.

trisooma
Автор

You, sir, are like a superhero to me. Keep it on!

gustavarium
Автор

You just saved me more than 6 meters of wire with the three wire setup. Thanks for the tutorial!

benedictrehiesi
Автор

I'm presently building a basic octopod spider robot. Once assembled, it'll measure close to 1 metre in diameter, and stand approximately 20cm - 25cm high. I'm thinking of installing (2) HC-SR04 proximity sensors on my robot, one at the front, and another at the back. These will be mounted on 180 degree servos to get as large a sensor range as possible. I'd like to include AI in my robot eventually, but I am not at that level yet. I love your topics and rely on your videos and expertise for my robot applications. Your topics are great and your website is absolutely gorgeous. You're a real pro and I'd love to be able to do what you do! Thanks a lot and keep up the good work.

henricoderre
Автор

Man, your simple yet effective demonstrations trigger the "creative juices" in the brain. I just simply started to think about the application of these sensors.... apart from the regular robo apps. I really admire you and I wish you continue this wonderful journey of enlightening. 🙏🙏🙏

prasadadavi
Автор

I have zero idea on how any of this works but I feel a whole step closer to understanding how to apply one of these on a separate Z axis controller for a CNC/laser. Thanks for taking the time to explain part by part.

emccat
Автор

As a fellow lover of the colour blue, your workshop is amazing. Very calming. Thank you for sharing your knowledge with us!

jon_raymond
Автор

Pink Floyd and electronics - an excellent way to begin the day!

cw
Автор

Lol, I spent three quarters of the vid looking at your jittery positioning, and the box level and 2 squares behind you. Perfect vid! Seriously, clear, concise, no great insertion of hipness, direct and to the point. Oodles of kudos. Granted the jittery paws fixture illustrates accuracy depends on stability of "lock" between horns and board, but I think your sketch improvements and evolutionary gains in accuracy would be better illustrated by just blocking the board square to the meter stick face. Only critique possible, lol. Points added for Pink Floyd reference, of course. Subbed!

ludditeneaderthal