Motors aren't working? Voltage probem?

Hi I'm new to Arduino motors and currently can't get my motors to work when when my Mega is not plugged into the compter. It works fine when plugged in though. My set up looks roughly like this.

Thought the motor shield is actually a Velleman and my gear motors are these.

I've also tried using a 9V to power the motor shield.

Am I just not using enough power?

EDIT: There are AA batteries NOT AAA.

#define FORWARD 0
#define LEFT 1
#define RIGHT 2
#define BACKWARD 3
#define STOP 4

#define STOP_SPD 0
#define TOP_SPD 255


int pwn_left = 3;  //PWM control for motor outputs 1 and 2 
int pwn_right = 9;  //PWM control for motor outputs 3 and 4 
int dir_left = 2;  //direction control for motor outputs 1 and 2 
int dir_right = 8;  //direction control for motor outputs 3 and 4 

int cur_cmd, last_cmd;

void setup()
{
 Serial.begin(9600);
 pinMode(pwn_left, OUTPUT);  //Set control pins to be outputs
 pinMode(pwn_right, OUTPUT);
 pinMode(dir_left, OUTPUT);
 pinMode(dir_right, OUTPUT);
 
 analogWrite(pwn_left, 100);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
 analogWrite(pwn_right, 100);

 last_cmd = 0;
 cur_cmd = 0;
 
}

void loop()
{

 

 motorHub(FORWARD);
 delay(5000);
 
 motorHub(LEFT);
 delay(5000);

 motorHub(RIGHT);
 delay(5000);

 motorHub(BACKWARD);
 delay(5000);

 
}



void motorHub(int command){
 switch (command){
   case FORWARD:
     Serial.println("Forward");
     digitalWrite(dir_left, HIGH);
     digitalWrite(dir_right, HIGH);
     analogWrite(pwn_left, TOP_SPD);
     analogWrite(pwn_right, TOP_SPD);
     break;

   case LEFT:
     Serial.println("Left");
     digitalWrite(dir_right, HIGH);
     analogWrite(pwn_left, STOP_SPD);
     analogWrite(pwn_right, TOP_SPD);
     break;
   
   case RIGHT:
     Serial.println("Right");
     digitalWrite(dir_left, HIGH);
     analogWrite(pwn_right, STOP_SPD);
     analogWrite(pwn_left, TOP_SPD);
     break;

   case BACKWARD:
     Serial.println("Backward");
     analogWrite(pwn_right, STOP_SPD);
     analogWrite(pwn_left, STOP_SPD);
     delay(500);
     digitalWrite(dir_left, HIGH);
     digitalWrite(dir_right, HIGH);
     Serial.println("Stop");
     analogWrite(pwn_left, TOP_SPD);
     analogWrite(pwn_right, TOP_SPD);
     break;
    
   default:
     Serial.println("Stop");
     analogWrite(pwn_right, STOP_SPD);
     analogWrite(pwn_left, STOP_SPD);
     delay(500);
     break;
 }
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

currently can't get my motors to work when when my Mega is not plugged into the shield. It works fine when plugged in though

That is exactly what I would expect.

How do you imagine the motors would work if the Mega is disconnected?

...R

Fixed it thanks. When I connect the batteries directly to the motors, they work just fine. I realised I made a big error on my initial post. I mean that when my computer is plugged into my Mega 2560, it works fine, but when I have batteries attached to the motor shield and the other batteries attached to the Mega, the motors don't work even though it seems as if the Mega and the motor shield have power.

Your Reply #2 is still a bit confusing.

Do you mean that the motors work properly when the Arduino is powered from the PC with the USB cable and the AA batteries are connected to the motor shield?

And, do you mean that the motors do not work when you disconnect the USB cable and power the Arduino with a separate battery?

Your diagram shows a PP3 type 9v battery - that probably provides insufficient current for the Arduino. Try a pack of 6 AA batteries.

Include some code to flash an LED so that you can tell if the Arduino is working even if the motors are not. It may help to isolate where the problem lies. Test the Arduino on the battery power with the shield disconnected.

...R

You probably don't know this but the 298 chip on your motor board loses 2V as a matter of course, and that loss goes up to almost 5V (iirc) at high currents. Startup counts as high current....

It might be that you have too little oomph left to get the motors started.

The 298 is old inefficient technology, and in the long run it may behoove you to look at something less lossy like Pololu has in this list.

JimboZA:
The 298 is old inefficient technology

While I agree in general with this statement, the actual problem is multi-faceted.

The L298 is fine for what it was originally designed for: As a bipolar stepper motor driver (coupled with the L297 as the controller), it works well for larger voltage steppers where the power supply isn't a battery, and adequate heat-sinking of the L298 is maintained.

Alternatively, its use as a DC motor h-bridge, for motors found in automobiles and similar (windows, seats, etc) where the voltages used are between 12 and 24 volts (generally) - when it is used to control a pair of motors or a single motor - and again, adequate heat-sinking of the L298 is maintained, it can function very well.

The problem, though, is companies creating shields or modules using the L298, and not adequately pointing out the the L298 has the 2 volts or larger drop and to plan for it (where in a car, that kind of a drop won't effect the usage of the motors application much if at all, due to the battery's "at-rest" voltage being a volt or two above its rated voltage, and the fact that it can easily supply the current needed). Further, they rarely detail the fact that the L298 has a minimum voltage it can work it (and what that voltage is), as well as how to plan the battery size vs the motor needs. Finally, they rarely heatsink the L298 properly, if at all.

In an automobile, it is very easy to heatsink the L298 - there are plenty of large metal surfaces to attach the thing to - plus it will most likely be placed inside a metal enclosure and use it as the heatsink as well. Alternatively, in a "hobby use" situation, if more air-flow is needed a small fan can be added to help with the heat generated.

Finally - using it to control small DC motors that only need 3-6 volts isn't really what it is spec'd for; instead, it should be used to control 12 vdc motors where the power supply (battery or otherwise) can supply around 14 volts.

Even so - there are better alternatives today instead of the L298; there's isn't even a good argument about it being a "thru-hole" device, given its wacky pin-spacing requiring at best an adaptor PCB (ok - maybe not so wacky, just not friendly to a standard grid layout). Instead, one of the low cost MOSFET-based solutions would be a better fit for almost every application, unless you have a bunch of the L298 sitting around...

JimboZA:
It might be that you have too little oomph left to get the motors started.

If the motors work when the Arduino is powered from the PC and the shield is powered from the batteries then the L298 is not the problem.

...R

Okay motors are working consistently now. I never did figure out was problem. Though it seems like I'm going to have to reduce friction as the bot slows and stops when it gets caught on minor things.

It should be noted that I've got minimal options as I need this done for a school project so I can't just order something more appropriate online as this would only arrive after my due date came along.

Yes I agree. Probably not a good idea to use a L298 chip with just one typical 9 Volt DC battery. Need to use a more substantial battery, that at least has much higher charge capacity.

Okay I'll go give that a shot. You guys are awesome. :slight_smile:

I feel so stupid. All I had to do was check the charge on the batteries. New batteries and it works perfectly. facepalm

LaRuth:
I feel so stupid. All I had to do was check the charge on the batteries.

Welcome to the club :slight_smile:

...R