HC-SR04 no readings when powered by battery


I’m building a wheeled robot. Two key components of the robot are an Arduino Nano and an HC-SR04 distance sensor. The issue: I get distance readings when the Arduino is powered by USB, but not when powered by battery.

To troubleshoot the issue in its simplest form, I reduced to a simple project that contains just the battery source (6 x 1.5V), Arduino Nano and HC-SR04. Schematic is attached to this post. Sketch is below. The project will light the on board LED for 100ms * the distance read in cm. It also prints the readings out to serial.

#include <NewPing.h>

const int ECHO_PIN = 4;
const int TRIGGER_PIN = 7;
const int LED_PIN = 13;
const int MAX_DISTANCE = 200;


void setup() {

  pinMode(LED_PIN, OUTPUT);

void loop() {

  unsigned int cm = sonar.ping_cm();

  digitalWrite(LED_PIN, HIGH);
  delay(cm * 100);
  digitalWrite(LED_PIN, LOW);

When the Arduino is powered by USB, the project works correctly. The LED lights up for the expected duration and the readings printed to serial look correct. When the Arduino is powered by battery, the LED does not noticeably illuminate.

I have checked the voltage at the VCC pin on the sensor in each of the cases above. When powered by USB the voltage is 4.49V. When powered by battery, the voltage is 5.00V. Some interesting scenarios.

  1. When I start the Arduino powering it by battery, then connect the USB cable to the Arduino, the distance suddenly starts correctly being read upon connecting the cable. I confirmed the voltage at the sensor is still 5.00V so the Arduino is still being powered by battery.

  2. Further to #1, when I disconnect the USB cable, the distance continues to be read correctly.

  3. Further to #2, if I press the reset button on the Arduino, it restarts and then continues to read the distance correctly. However, if I instead switch off the power from the battery then switch it on, the distance is no longer read correctly again as in #1.

At this point my only thought is a faulty sensor or Arduino. Not sure how to tell which one.

Does anyone have any ideas why I’m seeing this behavior?


All I can think off is that the batteries can't deliver the power for the "ping".

During the ping, the buffer cap on the Vin rail has to supply that "power burst". The UNO/Mega has 47uF, the Nano has only 4.7uF.

Try an electrolytic capacitor from Vin to ground. 100uF or more. Leo..

Thanks for the suggestion. I gave the 100uF electrolytic capacitor and also some other sizes a try. Unfortunately the problem remained. Even so, I was still curious about the comment on whether the batteries are able to deliver enough power. I tried replacing the batteries with a 5V power source. Everything worked correctly with that power source (even when connected to VIN). It seems the problem is with the batteries as a power source or possibly the Arduino voltage regulator. I'm thinking to try using an Uno instead and see if that makes any difference.

"When the Arduino is powered by battery, the LED does not noticeably illuminate. " We assume the LED means the one on dio 13.

Just for testing, maybe try increasing the value of your delay(s) by about 3 times. The ping has its own delays you know. Does the increased delay times make any difference?

Yes, I'm using the LED on dio 13. Increasing the delay did not make any difference.

I replaced the Arduino Nano with an Uno in case the issue was Nano specific, or a faulty Arduino. Interestingly, the problem remains even with the Uno.

I took a different approach to power the Arduino. The L298N board I'm planning to use has a voltage regulator on board with 5V output. When I power the L298N board from the same batteries (1.5V x 6) and connect the Arduino to the 5V output of the L298N board, the HC-SR04 sensor works correctly! I will continue with this setup.

I'm still unsure why I wasn't able to drive the Arduino Nano with sensor directly from the batteries. It does seem to be a problem related to the output from the batteries. The regulator or other component on the L298N is able to overcome it.

Hi Gary, Why are you using a library?? It just needs a bit of simple code. The battery might say 5V but give it a job to do and that will drop, and under 5V you will be in trouble.. and the Arduino could be resetting itself, etc..

Never had a problem with the HC-SR04, even the cheap ones from China!

Here's my code might give you an idea!

Hope it helps, regards.


long scanner(long cm)
    const int pingPin=7, EchoPin=8;
    long duration;

    // The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
    // Give a short LOW pulse before to ensure a clean HIGH pulse:
    pinMode(pingPin, OUTPUT);
    pinMode(EchoPin, INPUT);  

    digitalWrite(pingPin, LOW);
    digitalWrite(pingPin, HIGH);
    digitalWrite(pingPin, LOW); 
    duration = pulseIn(EchoPin, HIGH);

    // convert the time into a distance
    // inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);
    return (cm);
long microsecondsToCentimeters(long microseconds)
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.
    // The ping travels out and back, so to find the distance of the
    // object we take half of the distance travelled.
    return microseconds / 29 / 2;

It's not the battery. The sr04 (with a 4MHz and TL074 amp) fails when arduino run 5.0V Usb-power @ 4.8..4.9 ca work.

strange..., but i have experieced this with a lot of them.. (The variant with a 8MHz x-tal works OK)