Powering an Arduino Car

Good afternoon everyone. I have a question. I am trying to power an Arduino Car with a battery pack of 9V to have it essesntially move on its own based around this code

// Turn on the Sensor to begin finding my location then turn
// on the servo motor to determine position
// Then turn on stepper motors to begin having the car move

#include <Servo.h>;
const int PingPin = 12;
const int ledPin = 13;
int pingpin1 = 8;
int pingpin2 = 9;
int pingpin3 = 10;
int pingpin4 = 11;

Servo myservo;    //create servo object to control a servo


int cm;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  myservo.attach(7);
  //myStepper.setSpeed(40);
}

void loop()
{
  //Declare this as the main loop of the entire program
  //Turn on the servo motor to determine location
  //Servo myservo;  //create servo object to control a servo
  int angle = 0;
  Serial.println("SendA");

  for (angle = 0; angle < 180; angle += 1);
  {
    myservo.write(angle);
    delay(10);
  }
  for (angle = 180; angle >= 1; angle -= 1)
  {
    myservo.write(angle);
    delay(10);
  }
  
  Serial.println("You have succeeded up to this point");


  Serial.println("Now on to the Sensor");


  int cm = ping(12);
  Serial.println(cm);
  digitalWrite(13, HIGH);
  delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 10);
  
  
// Activate stepper motors to move foward
// then begin looping if else statements
 if (cm <= 25) { 
   Serial.println("counterclockwise");
   digitalWrite(8, LOW);
   digitalWrite(9, HIGH);
   digitalWrite(10, LOW);
   digitalWrite(11, HIGH);
   delay(1000);
   digitalWrite(8,LOW);
   digitalWrite(9,LOW);
   digitalWrite(10,LOW);
   digitalWrite(11,LOW);
   delay(1000);
   digitalWrite(8,LOW);
   digitalWrite(9,HIGH);
   digitalWrite(10,HIGH);
   digitalWrite(11,LOW);
   delay(1500);
   Serial.println("STOP");
   digitalWrite(8,LOW);
   digitalWrite(9,LOW);
   digitalWrite(10,LOW);
   digitalWrite(11,LOW);
   delay(1000);
 }
 else
 {
   Serial.println("clockwise");
   digitalWrite(8, HIGH);
   digitalWrite(9, LOW);
   digitalWrite(10, HIGH);
   digitalWrite(11, LOW);
   delay(1500);
 }
  Serial.println("STOP");
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay(2000);
}

int ping(int PingPin)
{
  long duration, cm;

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ledPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ledPin, LOW);

  pinMode(PingPin, INPUT);
  duration = pulseIn(PingPin, HIGH);

  //convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  return cm;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Now when I run this code off of the USB port it seems to run just fine. However when I try to run it off the battery port, all the wheels ever seem to do is move backward

Now I have been told that wiring can change the way a device works dramatically. Just what am I missing that is supposed to make this car work... I honestly am not sure at this point

What sort of battery are you using?

If the code works properly when powered by USB it should also work with a battery. However if the battery is inadequate (like those little PP3 9v batteries) you will problems.

Also it is best to have a separate power supply for the Arduino rather than share the motor power supply. At least try two separate power supplies to see if that solves the problem.

...R

The motors are being powered by 6AA batteries.

And Im trying to power this car by with the 6 AA batteries.

for (angle = 0; angle < 180; angle += 1);

Lose the semicolon.

Well I took away the semicolon. All it did was make the servo motor turn slower.

Imagine that.

I am trying to power the car with a battery pack that contains 6 AA batteries outputting a total of 9V. When I power the Arduino with the USB cord it works perfectly fine. However when I try to power the Arduino with the batteries, they only seem to move backwards even though I am telling the code to move both ways. What is possibly wrong with the wiring?

Hi
Did you use the Vin input or the round plug at the front?

If you use the Vin you maybe interchanged the + and the - side of the batteries box.

If you use the plug your plug maybe is wrong: there are models with possitive centre and with negative centre

Where is the test program you wrote that just moves the stepper, and doesn't have all the servo and ultrasonics stuff?

TheOneReborn:
I am trying to power the car with a battery pack that contains 6 AA batteries outputting a total of 9V. When I power the Arduino with the USB cord it works perfectly fine. However when I try to power the Arduino with the batteries,

Did you try my suggestion, in Reply #2 to give the Arduino its own separate battery power?

...R

I agree with Robin2. I have just finished the obstacle avoidance phase an autonomous rover. One thing I have learned is that though the Arduino specs say that it can power small motors and such, it is better to supply devices like that with their own power source. It provides more stable power to both the Arduino and heavier loads of the other components of the project. Also, since the Arduino has a common ground, feedback can be an issue also. When in doubt, use a relay.

Take care.

Robin2:
Did you try my suggestion, in Reply #2 to give the Arduino its own separate battery power?

...R

Currently at the moment I have the Vin pin connected to the positive end of the where the battery pack is(the + sign half of the breadboard). The only other wire I have connected to that half if the VCC pin of the H-bridge. On the other end of the battery pack(the - sign half of the breadboard) I have a wire connected to Arduino GND and the GND port of the H-bridge.

And also AWOL the program that I wrote is just the example for stepper motors because I used to think these were steppers. My mothers fiance has a model that is almost exactly like this and his works perfectly fine running off of my code. SO that leads me to believe it is something to do with the wiring, whether it be I have a wire in the wrong place or I need an extra wire.

Hi,
Is the protoboard a long one.
Check for continuity of the red and blue/black rails down the sides.
Some have a break in the middle.

Tom.... :slight_smile:

Protoboards.jpg

Thank you. That did seem to help because my breadboard looks exactly like that...only red. Now I do have one final question. Whenever I run the code through a serial monitor, the first number it always outputs is a 0, even if there is nothing in front of it. Why would that be.

Also whenever I run this code

/* Ping))) Sensor
* prints distance and changes LED flash rate
* depending on distance from the Ping))) sensor
*/

const int PingPin = 12;
const int ledPin = 13; //pin connected to LED

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int cm = ping(12) ;
  Serial.println(cm);
  digitalWrite(13, HIGH);
  delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 10);
}

//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int PingPin)
{
  //establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;
  
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(PingPin, OUTPUT);
  digitalWrite(PingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(PingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(PingPin, LOW);
  
  pinMode(PingPin, INPUT);
  duration = pulseIn(PingPin, HIGH);
  
  //convert the time into a distance
  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 gives me an output and then immediately follows it with a 0 and then another output. so why would that be? Im honestly confused on the whole thing.

Update. I managed to get the car running off of the battery pack, and it moves forward just fine. However when it tries to move backward there is a giant delay. Would it have something to do with the wiring or the coding? When i run it off the USB plug it works perfectly fine moving both forward and backwards. I am honestly confused as to why..

TheOneReborn:
Update. I managed to get the car running off of the battery pack, and it moves forward just fine. However when it tries to move backward there is a giant delay. Would it have something to do with the wiring or the coding? When i run it off the USB plug it works perfectly fine moving both forward and backwards. I am honestly confused as to why..

Are you deliberately ignoring Reply #11 and Reply #2 ?

...R

Hi,
What drive do you have?
Can you post a picture of your project and can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..... :slight_smile:

Your attachment on where everything is connected is okay, but a circuit diagram will make your project easier to check.