Instructables
loading
loading
Picture of Simple Arduino and HC-SR04 Example
HC-SR04_LED_fz.jpg
top.JPG
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.
 
Remove these adsRemove these ads by Signing Up

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)

Step 2: Connect the components

Picture of Connect the components
HC-SR04_LED_fz.jpg
Connect the components and wires as shown in the two pictures.
1-40 of 143Next »
mergavul4 days ago

Where do i connect power source/battery on this circuit.

i AM a student and i haven't any experienc in this field

please give me detailed circuit


These are your 2 power sources. Note that the USB jack should only be 5 volts! The DC jack has a voltage leveler that will convert anything from +5 volts - 9 volts to 5 volts. Anything over 5 volts can fry your boards! So, be careful!

F1T3TQ2H994BXMB.LARGE.jpg

The power should be connected to the Arduino. You can power it through USB from your computer, or from a 5 volt - 9 volt power supply (or a 9 volt battery with a DC jack adapter) through the DC jack on the board. Good luck!

JesseD10 made it!5 days ago

Eventually I'm gonna mount one of these babies on the front and back of my arduino controlled RC car. Just now, though, I set it up in front of the microwave in the kitchen. My unsuspecting roommates will get a nice little buzz in the dark when they walk into the kitchen! lol. Already got 2 of them with it! haha!

IMAG0148.jpgIMAG0146.jpgIMAG0147.jpg

Hi!

Could anyone please explain why this line:
distance = (duration/2) / 29.1;
...is true?

And what is that 29.1 number?

I know that distance = duration*velocity and it doesn't seem to be represented here.

Thanks

Example, if Δt = 500 microseconds, we know it took 250 microseconds for the ping to hit an object and another 250 microseconds to come back.

The approximate speed of sound in dry air is given by the formula:
c = 331.5 + 0.6 * [air temperature in degrees Celsius]
At 20°C, c = 331.5 + 0.6 * 20 = 343.5 m/s
If we convert the speed in centimetres per microseconds we get:
c = 343.5 * 100 / 1000000 = 0.03435 cm/ss
The distance is therefore, D = (Δt/2) * c
or D = 250 * 0.03435 = 8.6 cm

Instead of using the Speed of Sound, we can also use the "Pace of Sound".
The Pace of Sound = 1 / Speed of Sound = 1 / 0.03435 = 29.1 ss/cm
In this case the equation to calculate the distance become: D = (Δt/2) / Pace of sound
and for the example above: D = 250 / 29.1 = 8.6 cm

Hope this helps

akedia419 days ago

Hey, This was great help. Thanks!


However, I have a question about the use of the pulseIn function here. Aduino Documentauin tells me that this function starts timing when the pin gets a high signal, and then stops timing when the pin gets a low signal. So in our context it will record the ‘duration’ as the time between when it started hearing the reflected sound, and when it stopped hearing the reflected sound. But I don't think that is what we want. That duration will always be 10 microsecond, since that was the length of our pulse (assuming that any obstacle is further than 5 sound-microseconds). Don't we want something that starts timing whn the signal is first sent, and then stops timing when the signal is first received? I may be wrong. The code works perfectly, but it doesn't make theoretical sense to me.

I had the same question and here's what I found. Apparently the Echo side of the sensor produces a HIGH signal until it is interrupted when it senses the pulse. So the duration of the HIGH signal is indeed twice the time it takes for the sound wave to bounce off the object and make its way back to the Echo sensor. The interruption of the sound is what lasts for 10 microseconds... Hope tis helps
masyitaha2 months ago

hi,why does my sensor keep on telling out of range even when i'm putting obstacles in front of it.I'm using your code and circuit.

So I bought 4 of the HC-SR04 units from a place called robotshop.com, when I got them I opened one package and used a transducer and everything seemed okay. Today I used the exact same code and hooked up a second transducer and was getting some weird results; if either the emitter or receiver was occluded I wouldn't get any echo signal even after the obstruction was removed, if the unit did recover after the obstruction was removed it would take anywhere to a minute to start "working". It would also only "work" for a few seconds before not sending out an echo signal even if no obstruction had been introduced. BLARP!

I found a guy with a similar problem,

http://therandomlab.blogspot.com/2015/05/repair-and-solve-faulty-hc-sr04.html#comment-form

in the top photo he has the exact two flavors of the transducer I was sent; note the silk screen. The bottom transducer in the top image is the bad one ("Y1" on the silk screen under the crystal oscillator); I just happened to get two of each of the bad and good transducers.

I emailed robot shop and asked them to verify my findings and discontinue selling the faulty sensors. I also tried the resistor trick mentioned in the link I provided, but it didn't work very well. The sensor does immediately recover after an occluded emitter or receiver, but the range is now limited to only a few feet.

Hi

If you're sure from your wire connections & from non damage distance sensor you have, Try to make a rapid knock in front of the pulse source while it's working.
billtodd17 days ago

So I bought 4 of the HC-SR04 units from a place called robotshop.com, when I got them I opened one package and used a transducer and everything seemed okay. Today I used the exact same code and hooked up a second transducer and was getting some weird results; if either the emitter or receiver was occluded I wouldn't get any echo signal even after the obstruction was removed, if the unit did recover after the obstruction was removed it would take anywhere to a minute to start "working". It would also only "work" for a few seconds before not sending out an echo signal even if no obstruction had been introduced. BLARP!

I found a guy with a similar problem,

http://therandomlab.blogspot.com/2015/05/repair-and-solve-faulty-hc-sr04.html#comment-form

in the top photo he has the exact two flavors of the transducer I was sent; note the silk screen. The bottom transducer in the top image is the bad one ("Y1" on the silk screen under the crystal oscillator); I just happened to get two of each of the bad and good transducers.

I emailed robot shop and asked them to verify my findings and discontinue selling the faulty sensors. I also tried the resistor trick mentioned in the link I provided, but it didn't work very well. The sensor does immediately recover after an occluded emitter or receiver, but the range is now limited to only a few feet.

siluxmedia made it!1 year ago

Done it!
I've tested it comparing the measurements with millimeter paper, and i found that the value to divide is 36.9 instead 29.1 . That might depend on the sensor precision, I think.
The HC-SR04 can indeed be accurate between 2cm and 400cm, with a 0.3cm resolution over 1m and 1cm between 1m and 2m.
With less than 3cm the sensor gives a value over 3700, same if the two cilinders get covered.
It works perfectly against flat surfaces like walls, but lose precision over grass or other irregular surfaces.
Thanks for the tutorial!

IMG_20140506_153514.jpg

Thanks! I got 35 (roughly) indeed with measurement tape til 250cm. I've got 10 pieces, that yield very similar results, so there doesn't seem to be a big variance between devices.

Hopefully jsvestor can update the code example.

D6equj52 months ago

Re this HC-SR04 could anyone tell me what the two missing components are from my module? They are just behind the crystal and are stopping this module from working.

Thanks

D6

Image1.jpg

resisters

Nope, on my module these are two caps with 22pF, I think

BurakTutal2 months ago

Nice Project. I have made a smilar one. But mine is talking according to distance :) You can check my project. :)

http://www.instructables.com/id/Arduino-wtv020-Sd-...

D6equj5 made it!2 months ago

Nice little thing. The basics of a very useful tool for all sorts of projects.

2015-04-16 19.16.08.jpg

if the resistor i change to 330 ohm, will be trouble?

LostboyTNT3 months ago

/*I modded this for 1 LED, on pin 11 where it strobes faster or slower depending on distance*/

#define trigPin 13
#define echoPin 12
#define led 11
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 400 || distance <= 0){
Serial.println("Out of range");
distance = 400;
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(distance*8);
digitalWrite (led,HIGH);
delay(distance*8);
digitalWrite (led, LOW);
}
}

In your delay(distance*8)...what is the 8 and where did it come from.

Thanks

I don't remember exactly, I think it may have originally had something to do with the distance measure in centimeters (or similar), or it may have just been a number multiplier I picked that looked like a good delay. Sorry, wish I remembered.

Haha, no worries was still a big help and it worked so thanks.
rkasemi10 months ago

i need to make a obstacle avoiding robot and i have a HC-SR04 sensor, can anyone help me to modify this code because i need to put servo motors instead of lights, plzzz i need your help!!!

HI, i developed this with sensor and hub-ee wheels. Here is a video and all my code. Have fun :)

IMG_0332.MOV.mov(402x714) 24 KB

We are making something very similar but were wondering how you stabilized the sensor on the front of the cart

EdizonBlu3 months ago

Hello sir, i added this code and applied it with servo motor. I mounted the ultrasonic sensor on the servo motor. But i have a problem, I would like to ask on how to stabilize my ultrasonic sensor mounted in a servo motor. It is because everytime an "Out of Range" displays in the serial monitor it gives current to the servo motor causing it to sweep. The possibility of sweeping the servo left or right only is when there is an object being detected.

.

Here is my code://

#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10

#include <Servo.h>

int pos = 0; // variable to store the servo position

int defPOS = 0;

Servo myservo;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(led2, OUTPUT);

myservo.attach(9);

myservo.write(0);

}

void loop() {

long duration, distance;

digitalWrite(trigPin, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);

// delayMicroseconds(1000); - Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

//0 Left

//1 Center

//2 Right

int buttonState1 = digitalRead(led); //red

int buttonState2 = digitalRead(led2); //green

if( (buttonState1 == 0 && buttonState2 == 1) && (defPOS != 1))

{

delay(2000);

if(defPOS == 2)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

}

else if(defPOS == 0)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

}

defPOS = 1;

}

if (distance < 30) { // This is where the LED On/Off happens

digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off

digitalWrite(led2,LOW);

if(defPOS == 0)

{

for(pos = 90; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 2;

}

else if(defPOS == 2)

{

for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 1;

}

else if(defPOS == 1)

{

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees

{ // in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(2); // waits 15ms for the servo to reach the position

}

defPOS = 0;

}

//myservo.write(180);

// delay(1000);

//myservo.write(0);

}

else {

digitalWrite(led,LOW);

digitalWrite(led2,HIGH);

}

if (distance >= 400 || distance <= 0){

Serial.println("Out of range");

}

else {

Serial.print(distance);

Serial.println(" cm");

}

delay(500);

}

Shubham Sahu3 months ago

How can I add a motor which will start rotating if the distance b/w the sensor and any object is 'x' and rotate until the distance b/w them between them becomes 'y' ?

hai..i tried your code for 2 leds (red and green). but while compiling i got an error message saying "pulseln() was not declared in the scope". what should i do?

Hai..i tried your code for 2 leds (red and green). but while compiling i got an error message saying "pulseln() was not declared in the scope". what should i do?

hai..i tried your code for 2 leds (red and green). but while compiling i got an error message saying "pulseln() was not declared in the scope".

what should i do?

JohnC333 months ago

This turned out to be very easy. The instructions were very clear and the code is simple enough. I'm now incorporating this into a robot that will avoid obstacles. Thanks so much for helping me get started.

John

majd.kasawat made it!3 months ago

working in less than 5 minutes

IMG_20150312_005910.jpg
Jose E.G3 months ago

I Made it!!! Thanks . I was thinking the sensor was damage. Really appreciate your time to teach a beginner. Very helpful and easy

abdul.sami.753 months ago

the green led just blink wid the tx led

abdul.sami.753 months ago

it didnt work for me

abdul.sami.753 months ago

it doesnt work for me y?

1-40 of 143Next »