loading
After buying a HC-SR04 from Amazon, I could not get it to work out of the box. Not wanting to concede I had a DOA sensor on my hands, I searched for a simple example setup. After spending far too long on this than I felt I needed to, I decided to make this instructable to help other emerging tinkerers get their project off the ground.

I admit this example is more than bare-bones in that it has LEDs, but this lets me test it without needing a PC to show distance and check the accuracy of the sensor.

Step 1: Parts List

Arduino UNO R3 (I use the Adafruit mount)
One (1) HC-SR04 Ultrasonic Sensor
One (1) Red LED
One (1) Green LED
Two (2) 560 ohm (Green, Blue, Brown, Gold) Resistors
Half Breadboard
Eight (8) Male/Male hookup wires
A ruler that measures centimeters (or use the serial monitor)

Advertisement

<p>Thank you ,this project is very easy to build and understand</p>
<p>if want use ultrasonic sensor double. how do you do?<br>ทำไงงงงงงะ</p>
<p>Gr8 I'ble! I was trying to make an obtacle avoiding bot,but i had no idea how to code the HC-SR04 ultrasonic sensor.You saved my day!!</p>
<p>Can you please link us to that thing on which you have kept the arduino and the breadboard ?</p>
<p>Here is one: <a href="http://www.instructables.com/id/Acrylic-Arduino-Prototyping-Stand/" rel="nofollow">http://www.instructables.com/id/Acrylic-Arduino-Pr...</a></p><p>Or try searching for &quot;arduino holder&quot;.</p>
<p>can somebody explain me the pulseIn() function. In the Arduino site they say pulseIn() function 'Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing'. So as they say it doesn't start timing when trigger signal is sent. rather it start timing when echo signal is initially get a signal. And stop timing when echo signal stop receiving waves. From that time measure can we calculate the distance as above equation?</p>
<p>Works great!</p>
can anybody help me... im am really not good at this... i have a project and i really really need someones help.... please email me... science.140702487@gmail.com :`(
<p>Just hooked up my sensor following your tutorial, slightly modified. (No LEDs) And combined it with the &quot;Graph&quot; sketch to plot distance in Processing. Super simple! Thanks for a great and easily understandable tutorial!</p>
<p>I triple-checked everything and I don't know what's wrong. I put in the code and built the circuit but when I run the program, it keeps displaying &quot;Out of range.&quot; Does anyone know why?</p>
Even i had same problem,Use an external 5v supply for ultrasnic sensor dont use arduino supply
<p>Hi,</p><p>How do I arrange an external supply. I am getting the same error</p>
<p>I made this instructable and work's good </p>
<p>Hi, I followed and checked all connectins. But getting &quot;out of range&quot; repeatedly</p>
<p>i built this on an arduino mega.</p><p>i used, as recommended by some, a lab power supply on 3 and 5 volt, but it did not give good readings (out of range, wildly varying distances)</p><p>i had to exchange echo and trig. </p><p>i connected gnd and vcc of the hc-sr04 again to the arduino gnd and 5v.</p><p>now it works fine</p><p>thanks. </p>
<p>echo and trig pin arent declared in this scope</p>
<p>I'm new to Arduinos, but have done other hardware projects. I followed the instructions, but I am always seeing &quot;Out of range&quot;. I modified the code to see what distance it thought it was seeing, and it is always zero. Any suggestions on how to debug this?</p>
Even i had same problem,Use an external 5v supply for ultrasnic sensor dont use arduino supply
<p>dude you need to make the arduino and the sensor communicate with each other, add the required driver and library updates to make that happen :) good luck </p>
<p>Sorry, but that doesn't make any sense to me. The HC-SR04 doesn't have a driver. </p><p>In the end, I just switched to the PING sensor. More expensive, but it works totally reliably.</p>
I have seen this same issue with mine. Constantly seeing zero. Make sure all your inputs are defined and are landed on the correct pins. Also make sure your trigger and your Echo are not switched around. I have code for what I did because I kept getting it to stick on zero and needed to unplug the sensor for it to work unless I put my finger on the left upper corner of the hc Hr04. So I ended up making and input that when it senses 0 it will set this pin to high then low. I will upload more later for people having problems. My way seemed to fix it getting stuck on 0
<p>Be sure the Echo and Trig are connected correctly. In the .txt file the header says one way but the actual pin definitions in the next section are backwards (12 and 13 are reversed)</p>
<p>Also check the specs on the HC-SR04 for which voltage is appropriate to use - I find mine works better on 3V instead of 5V</p>
<p>Thanks for the suggestions, Patrick. I verified that the Echo and Trig were correct. The spec says it takes 5V. I just tried it with an RFduino which puts out 3V, and still only saw zeros returning from the pulseIn function. I have gotten it to work with 5V, but I had to constantly disconnect and reconnect to reset it. I'm surprised more people haven't seen this -- I tried it with two different devices and had the same problem. I have had much better reliability with the PING sensor. I wish I could use it at 3V with the RFduino, but it doesn't seem to work reliably at that voltage. </p><p>Let me know if you have any other ideas.</p>
<p>I also got it to work by unplugging the sensor and plugging it back in again. Which pin did you set to high and then low to get it to work?</p>
<p>Thnx a llot !!</p>
<p>Great work, I did this with my Nano and works like a charm thanks.</p><p>Had to switch Echo and Trig cables around thou.</p>
<p>The distance formula can be explained if you look up the data sheet of the HC-SR04 where it says :</p><p>&quot;This popular ultrasonic distance sensor provides stable and accurate distance measurements from 2cm to 450cm. It has a focus of less than 15 degrees and an accuracy of about 2mm.</p><p>This sensor uses ultrasonic sound to measure distance just like bats and dolphins do. Ultrasonic sound has such a high pitch that humans cannot hear it. This particular sensor sends out an ultrasonic sound that has a frequency of about 40 kHz. The sensor has two main parts: a transducer that creates an ultrasonic sound and another that listens for its echo. To use this sensor to measure distance, the robot's brain must measure the amount of time it takes for the ultrasonic sound to travel.</p><p><strong>Sound travels at approximately 340 meters per second. This corresponds to about 29.412&micro;s (microseconds) per centimeter. To measure the distance the sound has travelled we use the formula: Distance = (Time x SpeedOfSound) / 2. The &quot;2&quot; is in the formula because the sound has to travel back and forth. First the sound travels away from the sensor, and then it bounces off of a surface and returns back. The easy way to read the distance as centimeters is to use the formula: Centimeters = ((Microseconds / 2) / 29). For example, if it takes 100&micro;s (microseconds) for the ultrasonic sound to bounce back, then the distance is ((100 / 2) / 29) centimeters or about 1.7 centimeters.&quot;</strong></p><p><strong><br></strong></p><p>Source: https://www.bananarobotics.com/shop/HC-SR04-Ultrasonic-Distance-Sensor</p>
<p>distance = (duration/2) / 29.1;</p><p>would you like to explain this line, thanks!!</p>
<p>From what I can <br>get, &quot;duration&quot; in the code is a number showing the milliseconds needed <br>for the signal to go and return. This is why duration is then divided by<br> 2, to find out the time needed for the signal one-way. We know that <br>sound speed on air is 344 m/sec. So, we have the time and speed and <br>using the formula S=U*t (Distance = Speed * time) we can calculate the <br>distance. If you change this line in code with:</p><p>distance = (duration/2) * 0.0344 </p><p>you'll get the same result, because 1/29.1 = 0.0344!</p><p>I hope I helped! It all has to do with sound speed anyway...</p>
<p>I'll add my thanks for the clear explanation on this converstion factor. Much appreciated. </p>
<p>Thanks for answering the questions, @gtricho. I had the same question as @MohammedE74. If the duration is given in milliseconds then the speed of sound would also have to be expressed in milliseconds. Using dimensional analysis, this can be done by dividing the speed of sound by 1000 milliseconds. The result is 0.343 m/ms. 1 divided by 0.343 is 2.91, whereas 1 divided 29.1 equals 0.0344.</p><p>speed of sound = 343 meters per second (343 m/s)</p><p>343 m/s * (1 second/1000 milliseconds) = 0.343 meters per millisecond</p>
<p>This was super easy and simple. Thanks for sharing!! -- ArduinoNewbie</p>
<p>HC - SR04</p>
<p>HC - SR04</p>
great instruction! Can't wait to use it
<p>This worked very well! I have one question though: </p><p>Where does this line come from in the code -</p><p> distance = (duration/2) / 29.1;</p><p>I get that the duration is divided by two to account for the echo. I can't figure out where the 29.1 comes from though. Any thoughts?</p>
<p>From what I can get, &quot;duration&quot; in the code is a number showing the milliseconds needed for the signal to go and return. This is why duration is then divided by 2, to find out the time needed for the signal one-way. We know that sound speed on air is 344 m/sec. So, we have the time and speed and using the formula S=U*t (Distance = Speed * time) we can calculate the distance. If you change this line in code with:</p><p>distance = (duration/2) * 0.0344 </p><p>you'll get the same result, because 1/29.1 = 0.0344!</p><p>I hope I helped! It all has to do with sound speed anyway...</p>
<p>Dividing it by 29.1 makes the distance value equal to centimeters instead of whatever it would originally be. Hope this helps. :D</p>
<p>I am working on a project ( designing of quad copter with obstacles avoidance ) so i am using an four ultrasonic sensor hc sr 04 for ( front , back, right and left) .... so i need help of coding in arduino......thanks</p>
<p>Try the library http://www.ardublog.com/library-for-arduino-ultrasonic-ranging-hc-sr04/ very easy to use and supports more than one sensors.</p>
<p>So what is your question?</p>
<p>Excellent example project, thank you for sharing. </p>
<p>Thanks for sharing the project. I was just wondering if you used </p><p>if (distance &gt;= 200 || distance &lt;= 0){<br> Serial.println(&quot;Out of range&quot;);</p><p>as an arbitrary number or is that a limitation on the sensor?</p>
Glad to! I haven't checked this in a while and so my apologies to all of the comments I have missed.<br><br>Yes, 200 was completely arbitrary to test the sensor. However, if I remember correctly the sensor tends to lose accuracy around 3-4 feet.<br><br>Thanks for the comment!
<p>Thanks for sharing the project. I was just wondering if you used </p><p>if (distance &gt;= 200 || distance &lt;= 0){<br> Serial.println(&quot;Out of range&quot;);</p><p>as an arbitrary number or is that a limitation on the sensor?</p>
Works but the sensor is a little in acurate
<p>this sensor is supposed to be the most accurate one, but obviously there will be fluctuaions in the sound and it may be a little in accurate. that should not cause too many prolems.</p>
Works but the sensor is a little in acurate
<p>Thanks! It worked! </p>

About This Instructable

799,690views

293favorites

License:

More by jsvester:Simple Arduino and HC-SR04 Example 
Add instructable to: