This is a simple guide on how to make a distance detector using an Arduino, a HC-SRO4 Ultrasonic Sensor, a Buzzer, and some LED's.  The ultimate goal of this tutorial is to use the buzzer and LED's to display how far the object is from the ultrasonic sensor.

Step 1: Materials Needed

The Materials needed for this project are:

(1x) Arduino Uno
(1x) Breadboard
(1x) HC-SRO4 Ultrasonic Sensor
(1x) Buzzer
(2x) Green LEDs
(2x) Yellow LEDs
(2x) Red LEDs
(7x) 330 ohm Resistors
A lot of jumper wires
<p>I added some lines of code to use a 10 LED bar, also I changed the range to 2m. @flynn_munroe I assume this line: /* distance = (duration/2) / 29.1; */ is for setting the measure to centimeters? I am surprised with the range and &quot;precision&quot; of this cheap module.</p><p>thanks for the great instructions!</p>
<p>can i get the code for replacing seial moniter to 16*2 lcd display </p>
<p>The present code is working very well to me</p>
<p>Be careful, check the breadboard/protoboard you have. If the horizontal line where you connect the ground pin and the corresponding one where you connect voltage are divided in the middle, then you re having a different breadboard than the one this guy has. Make sure to connect everything on the same section</p>
<p>i did it the code was a little incorrect and made a little corrections</p>
<p>the code is not compiling</p>
Not getting output LEDs are continuosly ON it is not detecting the distance
<p>The same is happening to me. It's not working, all leds on, no sound and 'out of range' on serial monitor.</p><p>Any clue on whats going on?? I've just realized that falilures are from last few weeks. Maybe some change on the firmware, or the compilator or something like that??? (Sorry, just newbie in this world)</p>
<p>We have the same bug here. Despite the clear instructions, all we get are all led lamps on, no sound, and nothing else happens. We tried several times. We are beginners, and only managed some blink and melody sketches. But this one does not work unfortunately. We don't know now how to proceed. Any hint or advice would be welcome. </p>
<p>Me too. It is failing to detect the distance. All the LEDs are continously ON, and the serial monitor says out of range all the time.</p>
<p>can i fix this without resistors</p>
<p>My first &quot;instructable&quot;, worked perfectly. Very easy to follow directions.</p>
<p>Thanks brah!</p>
<p>how can i put lcd on it?</p>
<p>Works just as promised </p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/xF7It_x4F20" width="500"></iframe></p><p>Link for Video</p>
<p>is it possible to have it with lcd?</p>
<p>Where can I get a piezoelectric, buzzer or speaker capable of producing 80kHz?</p>
Hi just wondering but I'm in need of a circuit that used an ultrasonic sensor but turns on LEDs when something is NOT in front of it and turns off when something IS in front of it how would I do this btw all the LEDs need to turn on at once
<p>You can achieve that by simply changing the low/high values of LEDs</p>
<p>Works great but the loop ends when the closest distance in the code triggered. I don't understand why it happens. It freezes with all LEDs on but without sound. And also I didn't use resistors, too. </p>
<p>Hi, can you tell me the steps that i have to do to connect a LCD to display the measured distance?? Where do i need to modify the code ?</p>
<p>Its great! Tanks! It works very well! I din't use the resistors and buzzer.</p><p><a href="https://youtu.be/uH0Fi2rMEyE" rel="nofollow">https://youtu.be/uH0Fi2rMEyE</a></p>
<p>just the sensor is capeable of 450 cm</p>
<p>is there a way to increase the range?</p>
<p>Thanks mate this is really works.</p>
<p>Thanks mate this is really works.</p>
<p>thank you very much!!!!!!!!!!!!1 it&acute;s great and fantastic!!!!!!!!!!! =)</p>
<p>Really interesting project. Worked exactly as promised! Thanks. :)</p>
Easy and useful project :)
<p>This was my first project from this site so thank you very much! </p>
worked perfectly , just what i wanted thanks a lot!
<p>Hi, I am going to build a robot with four of these distance sensors.I like the simplicity of the code compared to others, but there is one flaw. I have already modified the void setup for my sketch with no problems, but I was wondering about the &quot;if&quot; statements. Is there any way to differenciate between the sensors, so that I can tell which sensor I am getting a reading from? I have included the following piece of your code to show you what I mean:</p><p><br> if (distance &lt;= 30) {<br> digitalWrite(led, HIGH);<br> sound = 250;</p><p>I want to know which sensor I am getting this information from.</p>Other than that, I really like this piece of code and if <em>I can get this cleared up, I will definitely use it.</em><p>(Sorry for the bold lettering, I accidentally highlighted it and made it bold and don't know how to get it off.)</p><p>Thanks </p>
<p>Sorry for the late response. The distance variable (along with the duration variable) are all based on the pulseIn() command, that reads data from echoPin. That echoPin is connected to the sensor. In your setup, you are going to need multiple trigPins, pulsePins, durations, distances, etc. Essentially you will need an entire copy of the loop routine for every sensor you have.</p>
<p>its works like charm man, thanks. but if want to measure from far distance, don't forget to change the code.</p>
<p>Could you please tell me how to modify this circuit and code for a 10 led bar graph display, while using a Parallax PING))) sensor and buzzer. Thank you.</p>

About This Instructable




More by flynn_munroe:Arduino Distance Detector with a Buzzer and LED's 
Add instructable to: