Arduino Tutorial #6 – HC-SR04 Ultrasonic sensor

Video is ready, Click Here to View ×

Check out my new website:
Learn how to use the ever popular ultrasonic sensor, the HC-SR04. In this tutorial we learn how it works, how we can use it with arduino, and make a little contraption along the way. Join me in another tutorial!

Support me on Patreon:

Social Links



44 thoughts on “Arduino Tutorial #6 – HC-SR04 Ultrasonic sensor”

  1. Now it is getting the distance all the time, which is usefull for a robot of course. But how about measuring somelike waterlevel which I want to measure only every 10 minutes (saves a lot of energy too)? Thanks, this was already very helpfull!

  2. I can get your code to work with an uno but when I try to load it into an attiny85, using an Uno as the the ISP I get "Serial was not declared in this scope" at the -(serial print distance) line. Can you offer any ideas on a way to fix this problem. Ive tried every thing I can think of. You seem to be very familiar with code. I'm learning by failure. Can't get this one. Help! Please

  3. I have heard you can use these to detect strikes in 10 pin bowling . I made a half scale lane and was wondering if this might work . The pins are about a 20 inch triangle and 71/2 inches tall . I can't seem to find any information , or if anyone has tried this .

  4. Nice Tutorial, btw I have a question. Lets say, i plan to measure a depth of a Well, can the color of the water affect the measurement? or can the turbidity of the water affect the ultrasonic wave? I plan to use a JSN-B02 waterproof ultrasonic sensor and submerge the probe underwater.

  5. Lovely video, simple and straight to the point. If anyone is confused by why the duration is divided by 29.1 specifically, I have an ultrasonic sensor tutorial on my channel as well that addresses that.

  6. I'm working on a quadcopter project which will be the obstacles avoidance capabilities so for that i am using an ultrasonic sensors which are four in number i.e ( front , back, right, lift ) site ,,, so for that i just need an arduino programming code ,,,,,, kindly help me to provide the coding ///programming … thanks

  7. hi bro i have a project about this ultrasonic sensor, i'm confuse with the code of how to control more than one ultrasonic sensors simultaneously, so both if sensors generate ultrasonic wave in the same time., so if you dont mind could you please help me to solve my proble, because i just knew arduino. or anyone who might have an idea? thx before

  8. Hi I like your tutorial but after the speaker was added to the code, it will not compile in the ide. Not sure what I am missing. I could not find a link to code and the screen is very hard to read so maybe I am doing something wrong. Thanks in advance for any help.

  9. Hey, can I know why do you divide the distance by 2? I don't really understand that part. And, what is the unit for distance that the Arduino is using? Is it in inches as default?

  10. Hi:
    I am using the same Sensor. Is there a way to record/store the output data (Distance) ans save it as a variable.
     would like to use the certain distance to crate vertical movement powered by stepper.
    If I store the distance let's say from 10cm to 50cm (40cm length). The vertical movement will be generated as low as 10cm and as high as 50cm.

    Thank you.

  11. Does any1 know why when i change the objects in front of the sensor too fast it prints values like -196 -167 inbetween the normal ones , but when i change them slow , it prints normal values ? I sense that is has something to do with the pulse duration and how fast it sends and it receives the signal , and if you do it fast it doesnt have much time to response ?

  12. OK I did and it worked, except for the piezo , did strange stuff. I'll dig up a speaker some where. I'm happy because the basic project worked. I want to breadboard all the different devices and components for the robot before I try to make it so I have a good idea what I'm doing. This is a great start. THANK YOU!!!

  13. Hi, I'm doing an Arduino project with a Hc-Sr04 sensor, I've wired my arduino up and the LED is emitting light, and the audio jack is emitting sound at the same time, in a sequence, but I don't think the sensor's doing anything and it's just acting as a wire to complete the circuit. The aim is to have the audio jack and the LED on when things are near the sensor, and off when they're out of the sensors way. Can somebody help?

  14. hey i have orderd a  4 AA battery clip. but 1 AA battery is 1.5 volts. and 1.5 * 4= 6volts is that gonna break the arduino/ sensors? And do you really need a breadboard? cant you just put in a male cable in the arduino and female to the sensore?

  15. the beginning of the video was good but when started writing the code, the screen image of the code was so way too small to follow. it was totally unreadable and no longer engaging. I hope you'll correct this issue in future posts. thanks!

  16. I'm using the HC-SR04 to build an object avoiding robot. The idea is that when it is within say 5cm of an object it will pivot and continue going straight until it finds another object. 

    My problem is when there isn't another object within 30cm or so (out of range.. ping() returns 0) when the robot turns the sonar just keeps returning 0 and stops pinging. I have tried maxDistance at several levels, and tried changing the timeout. I have used the NewPing library and the from scratch method calling pulseIn. It just seems that whenever the sensor is told to return 0 it gets caught listening for a ping that will never be heard. I'm not sure how to tell it to continue issuing pings. 

Leave a Reply

Your email address will not be published. Required fields are marked *