HC-SR04 sensor stops working and reads 0

hello,

I am having a problem with this sensor, i have tried several different versions of this code and it still does the same thing, i was reading another thread and basically the sensor does not have a time out feature on it and if it does not read the ping it will hang on a value (0), i am unsure on how to implement a timeout on it. i have tried different methods but i cant get them to work.

any help would be greatly appreciated thanks.

here is the code

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 

 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
  if (distance == -1)
    digitalWrite(echoPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(echoPin, LOW);
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

PulseIn() takes a third parameter, timeout. So pulseIn(echoPin, HIGH, 30000); will timeout after 30 milliseconds

i just tried that out and it still gets stuck on 0. dont suppose you have any ideas?
thanks

After setting your echo pin to input, do nothing else with it apart from pass it to pulsein. You are using it with digitalWrite.

pYro_65:
After setting your echo pin to input, do nothing else with it apart from pass it to pulsein. You are using it with digitalWrite.

i have removed all of that, I still cant get it to work. It still gets stuck on 0 when it does not receive a ping

If there is nothing reflecting the ultrasound, then the result is 0.

You logic needs to interpret 0 as "too far away", and not as "very close".

michinyon:
If there is nothing reflecting the ultrasound, then the result is 0.

You logic needs to interpret 0 as "too far away", and not as "very close".

no it works for a bit but if i doesn't receive a ping it goes to zero and stays there i have to tap on it to get it working again

ryisnelly10:
no it works for a bit but if i doesn't receive a ping it goes to zero and stays there i have to tap on it to get it working again.

i have just received several hc-sr04 with same bug. when it comes to out of range it returns 0 and set 1 on echo. senseor powered from 5V from arduino and GND to arduino's GND. thist code helps for me. it resets the sensor.
if (duration == 0) {
pinMode(echoPin,OUTPUT);
digitalWrite(echoPin,LOW);
delay(1);
pinMode(echoPin,INPUT);
}

1 Like