Go Down

Topic: HC-SR04 no readings when powered by battery (Read 141 times) previous topic - next topic

gary65536

Aug 26, 2015, 07:14 am Last Edit: Aug 26, 2015, 07:19 am by gary65536
Hello,

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. 

Code: [Select]

#include <NewPing.h>

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

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {

  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Started"); 
}

void loop() {

  delay(200); 
  unsigned int cm = sonar.ping_cm();

  Serial.println(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?

Thanks
 




Wawa

#1
Aug 26, 2015, 09:18 am Last Edit: Aug 26, 2015, 09:19 am by Wawa
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..

gary65536

#2
Aug 27, 2015, 11:11 pm Last Edit: Aug 27, 2015, 11:13 pm by gary65536
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.

jack wp

"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?

gary65536

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.

Cactusface

#5
Aug 30, 2015, 08:59 pm Last Edit: Aug 30, 2015, 09:01 pm by Cactusface
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.

Mel.

Code: [Select]

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);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);

// 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;
}
//------------------------------------------------------------------
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

Go Up