HC-SR04 Ultrasonic Sensor Dies After 1.5 Min [SOLVED]

Hello lovely people of the interwebs,

Head-scratcher for you all: My distance serial readout from my HC-SR04's hang at 0 after working for about 1 min. They read distances perfectly fine then suddenly quit after about 1.5 min and always read 0; it doesn't matter if the object is close or far, static or dynamic. I also switch which pins are being used as trigger and echo on the same rail (Digital 22 to 53) and reuploading the script to no avail.

The only solution I found is unplugging my power source and USB to my computer and restarting the whole program. This is obviously unsustainable.

I have a Arduino Mega 2560 (made in UK) connected to 5 HC-SR04's (made in China). I've only been testing my sensors one at a time. Here is my code; I'm also attaching a picture with the schematic:

//////////////////////////////////////////////////////
// Vcc = 5V
int trigPin = 22;
int echoPin = 23;
long duration, cm, inches;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
duration = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH,20000);

//This is an attempt to get the echoPin to reset
if (duration == 0){
pinMode(echoPin, OUTPUT);
digitalWrite(echoPin, LOW);
delayMicroseconds(200);
pinMode(echoPin, INPUT);
}
cm = (duration/2) / 29.1;
Serial.println(cm);
delay(500);
}

//////////////////////////////////////////////

Troubleshooting methods I've tried:

  1. Did you turn it off and turn it back on (computer/Arduino/power source)? Yes. Yes I did :wink:

  2. Clap my hands. There were forums saying that the sensors could get stuck and a heavy echo event like hand clapping would reset it. Didn't work.

  3. TheRandomLab had two solutions here:
    TheRandomLab: Repair faulty HC-SR04 ultrasonic sensor (solve erratic no-readings problems)
    a. Connect a resistor between the trigger and the reacting pin of the transducer's receiver. Didn't work.
    b. Use code to toggle the echo pin from OUTPUT to INPUT then back to OUTPUT. Didn't work.

  4. Switch pins to different rail (like Digital 2 to 13). Did work temporarily, then fell back to the same problem.

  5. Yell at the sensors in frustration. Didn't work.

  6. Tap the sensors themselves. Forums were saying that helps the reset process as well. Didn't work.

  7. Test to make sure the Arduino rails were actually outputting LOW and HIGH signals in the echo and trigger pins when they're supposed to. This was confirmed using a DMM.

I'm pretty stumped, but the last troubleshooting attempt shows that there's nothing wrong with the Arduino itself, probably the code or the sensors.

If it's the sensors that could be the issue, I'd like to confirm that the code and schematic are okay before I go out and buy some higher quality HC-SR04's.

Thanks all!

Post a real picture of the setup, not a Fritzing mess.

The Fritzing can't work like that, because you haven't powered the Arduino,
and didn't connect sensor ground to Arduino ground.
There are no 5volt batteries either.
Leo..

Why are continuously setting the pin modes in the loop? Once should be enough and in the setup function.

Paul

ies5:
//This is an attempt to get the echoPin to reset
if (duration == 0){
pinMode(echoPin, OUTPUT);
digitalWrite(echoPin, LOW);
delayMicroseconds(200);
pinMode(echoPin, INPUT);
}

This seems to be a good way to kill or damage the hcsr04 and your arduino. If the hc echo pin is stuck high and you switch the arduino echo pin to output (default low), you've created a (short) short.

Wawa:
Post a real picture of the setup, not a Fritzing mess.

The Fritzing can't work like that, because you haven't powered the Arduino,
and didn't connect sensor ground to Arduino ground.
There are no 5volt batteries either.
Leo..

This isn't Fritzing. It's Paint. I didn't post a real picture because this circuitry is buried under a ton of electronic packaging that would be a pain to take out. But it's a pretty simple layout.

Arduino is powered via USB.

5 volt battery is a Anker 13000. They in fact do exist.

Are you saying I should connect sensor ground to only the Arduino?

ies5:
This isn't Fritzing. It's Paint.

Arduino is powered via USB.

5 volt battery is a Anker 13000. They in fact do exist.

Are you saying I should connect sensor ground to only the Arduino?

Same difference.

Ok.

That's not a battery. It's a powerbank.
A battery with a boost converter that automatically turns off below a certain power draw.
What else is powered from that powerbank.
And why didn't you power the sensor from the Mega's 5volt pin.
Is the sensor some distance from the Arduino (if so, how far, and what kind of wire).

Sensor ground must be connected to Arduino ground.
Your picture is not showing that.
Leo..

Wawa:
Same difference.

Ok.

That's not a battery. It's a powerbank.
A battery with a boost converter that automatically turns off below a certain power draw.
What else is powered from that powerbank.
And why didn't you power the sensor from the Mega's 5volt pin.
Is the sensor some distance from the Arduino (if so, how far, and what kind of wire).

Sensor ground must be connected to Arduino ground.
Your picture is not showing that.
Leo..

Only the sensors are connected to the Anker. I'm using the Anker because I want to try using an external power source.

The sensor is 2cm from the Arduino using 20 cm long jumper wires.

Connected battery negative and sensor GND to Arduino GND. No change in result.

OK, then there you have your answer.

After 1.5 minutes your power bank switches off for lack of current (even active sensors will be drawing too little for it to detect), sensors lose power, and the Arduino reads zero distance (as the pulseIn times out and returns 0).

Just connect the sensors to the Arduino for power. They don't draw much, not worth messing around with external power supplies for this.

Also, next time use a real power supply. Not a power bank.

wvmarle:
OK, then there you have your answer.

After 1.5 minutes your power bank switches off for lack of current (even active sensors will be drawing too little for it to detect), sensors lose power, and the Arduino reads zero distance (as the pulseIn times out and returns 0).

Just connect the sensors to the Arduino for power. They don't draw much, not worth messing around with external power supplies for this.

Also, next time use a real power supply. Not a power bank.

Yup, you're right. I measured the current drawn by each sensor and it's about 5mA each. I contacted Anker and they replied:

"Please note that the minimum current requirement (the lowest amount of power that a device needs to draw from the battery to make it not shut off) of this battery is 50 mA. If your device draws a power lower than 50 mA, then the battery will shut off. As far as I know, you can charge your device with 15mA and another device draws power higher than 50 mA like your phone from this battery at the same time (as there are 2 usb output ports) to stop the battery shuts off."

To confirm everything, I tested the sensors individually with a set of AAA batteries and they worked as intended. On the other hand, I plan on using the power bank for other things too so it'll draw a heavier current and thus won't shut down. Hope this helps anyone else who has a similar problem.

Thanks for the help all!