Arduino resets when powered by 9v battery

Hi! Im trying to make a small bot that turns around when ever it hits something.. I made it out of cardboard and the sensor that detects a collision is two pieces of aluminum foil that acts as a button. (With a 5kohm pulldown resister between the pin and ground)

The thing is run by two servos (one acts as the "motor" the other one is for steering)

When i power the bot through USB everything works fine, but when i try to power it by a 9v battery the arduino seems to reset when the bot goes from backwards direction to forwards.

the 9 V battery is attached to Vin & gnd

Here is my code:

#include <Servo.h>

int frontSensorPin = 12;
int motorServoPin = 3;
int turnServoPin = 5;

int initialized = 0;
Servo motorServo;
Servo turnServo;

void setup(){
  turnServo.attach(turnServoPin);
  pinMode(frontSensorPin, INPUT);
  pinMode(13, OUTPUT);

  randomSeed(analogRead(0));
  turnServo.write(79); //go straight
}

void loop(){
  if(initialized != 0){
   motorServo.write(110); //Go forward
   turnServo.write(79); //go straight
   if(digitalRead(frontSensorPin) == HIGH){
   //Turn in a random direction and go backwards if sensor is activated
     if(random(10) > 5){
       turnServo.write(60);
     } else {
       turnServo.write(100); 
     }
     motorServo.write(0);
     delay(3000); //Arduino seems to reset after this delay when powered by 9V
   }
  
  } else {
     delay(1000);
     digitalWrite(13, HIGH);
     delay(1000);
     digitalWrite(13, LOW);
     if(digitalRead(frontSensorPin) == HIGH){
       initialized = 1; 
       motorServo.attach(motorServoPin);
     }     
  }
}

The servos are attached according to the servo tutorials..

Is the servos drawing too much power, causing the arduino to reset, or what exactly is going on here?

Okay so i experimented a bit and tried to power the arduino by a solarcharged USB battery (through the arduino USB port) and it works fine.. But im still clueless to why it resets when its on a 9v

I did a little further troubleshooting and the sketch seems to run fine if i disconnect the "motor" (causing the bot to stand still and only turn when a "collision" occours.

I also tried making it go backwards with motorServo.write(50) instead of setting it to 0 degrees, but that was a nogo..

Is there any chance of me getting this to work with a 9v battery?

Have you tried stopping the motor and putting a delay between going forward and going into reverse? (and vice versa)

If you mean a little rectangular 9V "transistor" battery, I doubt it. These don't supply much current, and I suspect you are seeing the system reset due to a voltage drop when the motor tries to draw a lot of current.

Switch to a heftier battery pack, like 6 AAs.

-j

Okay problem solved.. It was just a bad battery, went down to the store and bought a new one, and problem solved...

Thanks for the replies

Put a large capacitor across the supply something like a 470uF, that will help it cope with sudden demands and maybe rescue your 'bad' battery.

Thanks.. I will try that.

Im quite the newbie, so a quick guide on how to do it would be greatly appreciated :slight_smile:

Putting a cap across a battery? I'm pretty sure all you need to do is get a capacitor, connect the + to the + on the battery and the - to the -. Then carry on using the battery as if the capacitor wasn't there.