Ultrasonic sensor not working properly.

I am using a HC-SR 04 Ultrasonic Sensor and it is not responding as expected. It gives a few readings at first and then does not respond at all. I am using the NewPing Example code.

Please tell me is that a problem in the code or the sensor itself.

Thanks in advance.

How are you testing it? What readings do you expect?

Paul

Paul_KD7HB: How are you testing it? What readings do you expect?

Paul

I am using a serial monitor(PUTTY) and by connecting the sensor to my arduino nano and i expect to read the distance of the nearest object in front of the sensor.

I found this instructable to be very helpful:–

Vulcan666: Hi, I have used and tested an ultrasonic sensor before on my Arudino Uno. It should work fine for you on your board. I found this instructable to be very helpful:--

http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/

Try playing around with the code to get different results, such as letting it "see" further. Also, it might be easier if you didn't do the LED stuff and just connected your pins up, copied the code, took out the LED code, and ran it.

If you are still having issues don't hesitate to ask and I'll try to help you.

The only output I get is NOT IN RANGE.

Hmm. Ok. Ima little busy right now... get back to you this afternoon with some code I'll write.

Okay, I tested it and got it working. I'll provide a new tutorial link, but don't listen to their code, but wire it up like they did. These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something that problem will go away.

New tutorial link:-- https://www.youtube.com/watch?v=MrkyIOtG6ok

Modified Code:--

/* HC-SR04 Ping distance sensor VCC to Arduino 5V GND to Arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 More info at: http://goo.gl/kJ8Gl Original code improvements to the Ping sketch sourced from Trollmaker.com Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar Modified by Tolson Winters (Aug 27, 2014) for simplified serial monitor reading. */

define trigPin 12

define echoPin 13

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

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 >= 2000 ) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }

Vulcan666: Okay, I tested it and got it working. I'll provide a new tutorial link, but don't listen to their code, but wire it up like they did. These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something, like my RC Car for example, the problem will go away. Hope this helps! Sorry about the delay!

New tutorial link:-- https://www.youtube.com/watch?v=MrkyIOtG6ok

Modified Code:--

/* HC-SR04 Ping distance sensor VCC to Arduino 5V GND to Arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 More info at: http://goo.gl/kJ8Gl Original code improvements to the Ping sketch sourced from Trollmaker.com Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar Modified by Tolson Winters (Aug 27, 2014) for simplified serial monitor reading. */

define trigPin 12

define echoPin 13

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

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 >= 2000 ) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }

Play around with it, try moving the cord, and changing the out of range values!

Let me know if it does or doesn't work! It should work, though, because it worked for me.

It started working properly with te code in the tutorial link you provided.I am still not able to figure out what was wrong with that sensor but I think that it was the high current draw which caused the arduino to reset.It still gives the constant OUT OF RANGE output when the objects in front of it are moving too fast.

So,thanks for your help.

P.S:and sorry for the late reply.

Np, glad it's working for you!

Hi Thanks for this.

These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something, like my RC Car for example, the problem will go away.

I'd had a project that had been working fine, then the sensor started giving nonsense readings in a very small range.

I'd tried everything in the code etc.

Unplugged the jumper cords, plugged them back in again and it's working fine again!

shadow0000: It started working properly with te code in the tutorial link you provided.I am still not able to figure out what was wrong with that sensor but I think that it was the high current draw which caused the arduino to reset.It still gives the constant OUT OF RANGE output when the objects in front of it are moving too fast.

So,thanks for your help.

P.S:and sorry for the late reply.

hey, i have the same problem, i get is 0CM all the time . i try many code. anther thing, before i up-lode the code to the arduino the gnd shortens the circle. i used in the sensor one year ago and its work , but now no...

i use arduino mega 2560

put delay about 50ms after each measurement to reduce echo

check my library - https://github.com/enjoyneering/HCSR04

Features:

Read distance from 4cm to 250mm Compensate speed of sound according to the ambient temperature Pass distance thought median filter Set maximum distance to speed up measurement

Vulcan666: Okay, I tested it and got it working. I'll provide a new tutorial link, but don't listen to their code, but wire it up like they did. These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something, like my RC Car for example, the problem will go away. Hope this helps! Sorry about the delay!

New tutorial link:-- https://www.youtube.com/watch?v=MrkyIOtG6ok

Modified Code:--

/* HC-SR04 Ping distance sensor VCC to Arduino 5V GND to Arduino GND Echo to Arduino pin 13 Trig to Arduino pin 12 More info at: http://goo.gl/kJ8Gl Original code improvements to the Ping sketch sourced from Trollmaker.com Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar Modified by Tolson Winters (Aug 27, 2014) for simplified serial monitor reading. */

define trigPin 12

define echoPin 13

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

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 >= 2000 ) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); }

Play around with it, try moving the cord, and changing the out of range values!

Let me know if it does or doesn't work! It should work, though, because it worked for me.

Just wanted you to know that you saved me so hard with this! Because of that delay at the end it's working for me now!