Sketch runs when connected via usb, not battery

I’ve got an external battery pack that I want to power a simple arduino Duemilanove clone (Freetronics TwentyTen) robot.

I’ve developed the sketch and it works when I upload, and click the serial monitor.

I then disconnect the USB and connect the battery pack (Vin = 7v), the program doesn’t run.

 /*
  control an Ardumoto
 motor driver shield with a 5V Arduino controller board
 */

int sonarSensorPin = 5;
int dx = 100;

int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13

int lastLeftSpeed  = 200;
int lastRightSpeed = 200;

void setup()
{
 
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);

  forward();
   Serial.begin(9600);
  Serial.println("setup complete");
}

void loop()
{

  dx =  readSonar();
  if (dx < 20)
  {
    reverse();
     delay(1000);  
    left();
      Serial.println("set to STOP");

  }
  else
  {
    forward();
  }
  delay(1000);
}

void stop(void)
{
  digitalWrite(pwm_a, LOW);
  digitalWrite(pwm_b, LOW);
}

void forward()
{
  stop();
  digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
  digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

  go(lastLeftSpeed, lastRightSpeed);


  Serial.println("lines for dir a and b set LOW to go FORWARD");

  Serial.println("FORWARD");
}

void reverse()
{
  stop();
  digitalWrite(dir_a, HIGH);  //Reverse motor direction, 1 high, 2 low
  digitalWrite(dir_b, HIGH);  //Reverse motor direction, 3 low, 4 high

  go(lastLeftSpeed, lastRightSpeed);
  Serial.println("lines for dir a and b set HIGH to go in REVERSE ");
  Serial.println("REVERSE");
}

void left()
{
  stop();
  digitalWrite(dir_a, LOW);  //Reverse motor direction, 1 high, 2 low
  digitalWrite(dir_b, LOW);  //Reverse motor direction, 3 low, 4 high

  go(0, lastRightSpeed);
  Serial.println("turn left ");
  Serial.println("LEFT");
}

void go(char a, char b)
{
  analogWrite(pwm_a, a);  //set both motors to run
  analogWrite(pwm_b, b);
}

int readSonar()
{
  long value = 0;
  int cm = 0;
  int inches = 0;
  // Read the value from the sensor pin
  value = pulseIn(sonarSensorPin, HIGH);
  cm = value / 58;    // pulse width is 58 microsencs per cm
  inches = value / 147;     // which is 147 uSeconds per inch 
  Serial.print("cm : inches = ");
  Serial.print(cm);
  Serial.print(':');
  Serial.println(inches);

  return inches;
}

I thought that when I uploaded the program, it would be stored in the nvm and execute as soon as external power is applied.

Oddly, other sketches will run when I upload them and then disconnect USB and run from battery.

Why is this one different from all the others?

"void stop(void)"

Well...it's working now. The only thing I've done is to swap batteries. Same voltage.

Thanks for the reply.

"Doesn't run" doesn't really describe an issue. Does the power LED light up? Are there other devices in your circuit that are powered? Does it look like the bootloader is trying to run? What is the battery's voltage while under load?

The only thing I've done is to swap batteries. Same voltage.

Well there is always a reason and you will only learn by figuring it out. Did you measure the bad pack while it was trying to power the board? Measuring battery voltage under load is the only true test that it has an adequate charge or can supply the demanded current requirement. Does the 'bad' pack have different connectors or wiring that might be half broken causing high resistance under load? There might be other causes I can't think of at this time.

Reminds me of my serving in the Air Force and being assigned to a base in Montana. Us with cars parked in the dirt parking lot had to bring the car's battery into our barracks room as at -30F the battery just could not supply much current. But we kept them in our rooms and then brought them out to our cars when we wanted to go out it would normally work well enough to start the engines. It was a little hard on the hands clamping cables on and off at those temp. ;)

I think @retrolefty is spot on with the comment about voltage dropping under load. Both fully-charged and partially-charged batteries may show very similar terminal voltages when unloaded, but then the internal resistance of a partially-discharged battery can cause its output voltage to drop dramatically when loaded up.

By the way, there's a rule that says any post that mentions a robot should include a photo.

Well, that rule doesn't actually exist. But it should!

Jon Arduino Shield List: http://shieldlist.org

You're right. When it was failing I did measured the Vout of the battery and got 7.3v. The pack is the same: I just put different NiMH AA batteries. The set of batteries swapped in measured 6.9v.

Now after running it for awhile the battery has worn down a bit and I'm reading Vin = 5.1 v and Vcc is reading 4.9v pretty steady even under load.

Still don't know what the problem was. I'm having no more problems with it not running.

One of the things is that all the batteries are a little different. Some with differing Ah ratings, and different brands etc.

Next time I see the problem, I'll see if I can diagnose the difference. Could it be one battery in the set is bad and is introducing battery resistance?

BTW, I grew up in Alaska and worked as an elex tech in the oilfield up on the North Slope. I have a few cold weather stories of my own!

We had head bolt heaters and battery warmers, and oil circulating pumps, etc. Up on the slope most of the buildings have big arrays of 110 plugs to plug into. Even still, much of the time up on the slope, we just left the vehicles running 24/7.

Good times.

Could it be one battery in the set is bad and is introducing battery resistance?

Definitely. That's a fairly common scenario. You see it with any multi-cell battery, such as a car battery: if any one cell in the battery develops a leak or other fault, the whole battery will be rendered incapable of delivering the rated current / voltage.

Jon

Hey Jon, if you look close, you can see a bit of your TwentyTen under the motor shield.

It's a pretty basic little bot. As basic as I can get since I'm an Arduino noob. Nothing but a DFRobot 2W chassis, Freetronics TwentyTen, Sparkfun Ardomoto board, and a Maxbotix LV-MaxSonar EZ-1.

Uploaded with ImageShack.us

When I figure out how to attach a photo of it I'll do it

You can't attach pics here directly. You need to find a hosting site to upload the pics and then posts the links here. http://www.imageshack.us/ is free and pretty simple to use.

Lefty

One of the things is that all the batteries are a little different. Some with differing Ah ratings, and different brands etc.

This could be a problem and potentially dangerous. You don't want to mix different battery technologies. For example NiMH (rechargeables) and Alkaline.

You don't want to mix different battery technologies. For example NiMH (rechargeables) and Alkaline.

They are all NiMH. The only difference is the brands and Ah ratings. I don't think that's dangerous is it?

I figured out how to use the imageshack (thanks for the tip) and I added it to the post above.

Uploaded with ImageShack.us

If that is a 5 cell pack, 5 x 1.25 = 6.25. The Arduino external power is rated at 7.5 volt minimum, so you are kind of operating at below specs so the on board automatic voltage selector switch may not operate reliably and that is why you my be seeing the problem.

Lefty

The Arduino external power is rated at 7.5 volt minimum, so you are kind of operating at below specs

Thanks for the tip. That's good to know.

Beyond the bootloader(?) wierdness, which has gone underground, I'm seeing a lot of false positives with the MAX Sonar EZ-1.

I'll take another shot at it tomorrow with freshly charged batteries and see what I find.

Thanks for all the feedback!

(from @retrolefty)

If that is a 5 cell pack, 5 x 1.25 = 6.25. The Arduino external power is rated at 7.5 volt minimum

I think it may actually be even worse than that: NiMH AA cells I’ve seen are rated at only 1.2V, so 6V total for the set of 5 when fully charged and experiencing no terminal voltage drop due to load. Even with the cells fully charged it’s going to be very borderline.

@EddyTheJekyll: Probably best to find yourself a 6-cell battery pack if you can: it’ll still be marginal, but not as marginal as the current setup.

By the way, do you have a blog post or similar about this project that I can point to from the Freetronics site?

Jon