Go Down

Topic: Arduino Motor Shield R3: Problems driving both DC motors simultaneously (Read 4753 times) previous topic - next topic

KenyanX

I'm extremely new to this and this is my first post, so please forgive me if I'm doing something really stupid.

I have an Arduino Uno 3 connected to the Arduino Revision 3 Motor Shield.

Here is the issue.

I can drive motor A or Motor B, but not both at the same time.

Here is my code:

Code: [Select]

/*
Motor Test Drive
*/

//Motor A Config
const int
 PWM_A = 3,
 DIR_A = 12,
 BRAKE_A = 9;

//Motor A Config
const int
 PWM_B = 11,
 DIR_B = 13,
 BRAKE_B = 8;

void setup()
{
  pinMode(PWM_A, OUTPUT);
  pinMode(BRAKE_A, OUTPUT);
  pinMode(DIR_A, OUTPUT);       //Correction to Code based on OldSteve's Comments
  pinMode(PWM_B, OUTPUT);
  pinMode(BRAKE_B, OUTPUT);
  pinMode(DIR_B, OUTPUT);        //Correction to Code based on OldSteve's Comments


 
 Serial.begin(9600);
 while (! Serial);
 Serial.println("Speed 0 to 255");
}


void loop()
{
 digitalWrite(BRAKE_A, LOW);  // setting brake LOW disable motor brake
 digitalWrite(DIR_A, HIGH);   // setting direction to HIGH the motor will spin forward
 digitalWrite(BRAKE_B, LOW);  // setting brake LOW disable motor brake
 digitalWrite(DIR_B, HIGH);   // setting direction to HIGH the motor will spin forward
 
 if (Serial.available())
 {
   int speed = Serial.parseInt();
   if (speed >= 0 && speed <= 255)
   {
     analogWrite(PWM_A, speed);
     analogWrite(PWM_B, speed);
   }
 }
}


If I leave the analog writes to the PWM as is - Motor A drives fine, but B does nothing.
If I comment out B, A drives fine (As expected)
If I comment out A, B drives will work

I thought maybe it was a timing thing so I put a delay in between the the two writes and only only A worked, but if I change the speed back to 0 A stops, then B starts for a few second and then stops.

Any ideas on why I can't get them both to run at the same time?


DuaneDegn

Can you take a picture of your wiring?

A link to the shield you are using would also be helpful.

What is your power supply?

What you describe sure sounds like a wiring problem.

Does the working motor start as soon as you turn on the power? Did you type anything into the terminal?

How did you "change the speed back to 0"?

KenyanX

Hi Duane,

Thanks for trying to help out with this. Here are the answers to your questions.


Link to the board
https://www.arduino.cc/en/Main/ArduinoMotorShieldR3


The wiring is very simple:
I have a 9volt wired to the external power in on the shield.
I have one motor wired to A input and one wired to B input.  (these are Direct connected)
There is nothing else connected on the board.




I tried putting the photos inline but it doesn't seem to work so here are the links.

https://www.flickr.com/photos/kenrowephoto/shares/6g48s3
https://flic.kr/p/A4Zmg1

I checked the solder points and bias on the motors using a battery.  I've also checked the voltage on the motor terminals when the sketch is running.  There is no voltage on B if A is running.  If I comment out A in the sketch B runs fine.

What I mean by 0, is the sketch is eventually meant to take feedback form a sensor to adjust speed.  Right now I have it taking int values from the terminal, which works fine though only with one motor at a time.

Thanks again,
Ken

KenyanX

I made a video along with posting new test code to go with it to show what is happening.

Everything can be found here:

http://kennethrowe.com/Blog/arduino-motor-shield-issue/

DuaneDegn

The dreaded 9V battery! Those little batteries are the cause of many hobby robotic problems.

You need a better power supply.

AA NiMH cells should work well for that size of robot but the combined voltage of the batteries should be between 7.5V and about 12V (I'm not sure of the upper limit for your shield).

The datasheet of the L298P used by your shield states the motor voltage should be 2.5V higher than the logic voltage. Since you're likely using 5V logic the motor voltage should to be 7.5V or more (though it will likely work with a bit less).

The 9V battery you're using has a good voltage value but it can't supply nearly enough current for this application.

KenyanX

Thanks Duane,

I will give that a try.  I just had the robot on the floor and if I give it a little push the second track would move.  It must have been getting stuck in a stall without enough current.  I have a 6V and 3 volt pack.  I'll run a test with them in series and report back.

UPDATE:  My other batteries didn't seem to do the trick, but I have 7.2 2200 mAh at work,  I'll need to wait a few to grab that.  Do you thin that would work for testing or should I pull the trigger and buy a bigger 8.4?

Thanks again,
Ken

OldSteve

In setup(), you don't make 'DIR_A' or 'DIR_B' outputs. That can't be helping. :)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

DuaneDegn

In setup(), you don't make 'DIR_A' or 'DIR_B' outputs. That can't be helping. :)
Yes. Good catch.

I see a 9V battery and I automatically think it's the source of the trouble. (I hate those things.)

@KenyanX, You might want to try the 6V with modified code.

I didn't link to it this time, but I often mention my tutorial on L298N boards. The board I used in the video uses two direction pins per motor instead of one. My suggestion to try the h-bridge without the microcontroller in the circuit still applies to your h-bridge. It's always nice to test the hardware independent of the software whenever possible.

BTW, Even if you don't watch the video, skip to the end to see the cool Mecanum wheeled robot.

KenyanX

thank you very much Duane and Steve for your help.

I made OldSteve's correction to the code.   I updated the code in my original post with comments to to reflect the changes in case anyone else ever makes the same mistake as me :).

The problem seemed to be two parts.  I did need the better battery and I also needed to fix the code to initialize the other pins.  I think when I first did the code I wasn't worried about direction and forgot it.  The board was unpredictable without it though.

As of now - I think the problem is solved other than the motors not going at the same speed but that isn't unexpected without encoders and using cheap motors.

I also watched all of Duane's video which was really great.  I think I'm going to try and learn more about that drive.  Maybe my second project :) 

It left me with a few questions though.  I chose the Arduino motor Shield because I though it would be easy, but I have had issues finding some basic information on it.  Maybe someone here knows?

Arduino Motor Shield R3 Questions:

1.  With the shield mounted, I have separate power supplies to the Arduino and the shield at different voltages.  Does the shield and board together force a common ground between the two?

2.  The Arduino Motor Shield has LED lights on the Motor outs.  Based of my experiments I believe one is power and the other maybe is direction?  Can anyone confirm?

3.  How can I tell what voltage the Motor shield is driving the motors at.  Is this controllable?  I've seem different motors with different requirements.  Is the max output dependent on the input voltage?

Thanks,
Ken

DuaneDegn

thank you very much Duane and Steve for your help.
You're very welcome. I'm glad Steve was around to catch the code error.

As of now - I think the problem is solved other than the motors not going at the same speed but that isn't unexpected without encoders and using cheap motors.
Even expensive motors can turn at different speeds forward than reverse so when a pair is used on a robot, one will likely be going in reverse. Trying to control speed without encoders is a bit of trial and error.

1.  With the shield mounted, I have separate power supplies to the Arduino and the shield at different voltages.  Does the shield and board together force a common ground between the two?
I haven't looked at the schematic but I've got to believe the people designing the shield knew enough to link the ground lines. I think it's safe to assume the grounds are linked.
Not only do the grounds need to be connected, they need to be connected so the return path of the motor current doesn't pass through the microcontroller. The people designing the shield would know how to do this so I doubt you need to worry about it.

2.  The Arduino Motor Shield has LED lights on the Motor outs.  Based of my experiments I believe one is power and the other maybe is direction?  Can anyone confirm?
Again, I didn't look at the schematic, but I think your assessment is correct.

3.  How can I tell what voltage the Motor shield is driving the motors at.  Is this controllable?  I've seem different motors with different requirements.  Is the max output dependent on the input voltage?
The output voltage will be a couple volts less than the input voltage (on a L298 h-bridge). If the battery voltage is 12V and the output from the h-bridge is 10V and you drive the h-bridge with a 50% duty PWM signal, you can kind of pretend the output is 5V but of course it's not really. It's switching between 10V and 0V at the same PWM frequency as the input signal.

The motors of the Rover 5 are rated at a bit more than 7V. I regularly drive these with a 12V battery but I try to reduce the PWM so the average voltage is closer to 7V. I don't think the motors really treat a lower duty cycle as a lower input voltage so I wouldn't try this with expensive motors (unless someone who knows more about this than I do says it's okay).

I've had my Rover 5 bots for several years. I don't drive them very often but when I do, it's with a 12V battery (a 3S LiPo I also use with quadcopters). The motors are still working fine. This of course doesn't mean they're not aging faster than if I used a lower voltage battery.

I also watched all of Duane's video which was really great.  I think I'm going to try and learn more about that drive.  Maybe my second project :) 
Did you find information about the Mecanum wheels? Here's a link to my Hackaday.io project about the Mecanum wheeled robot. I'd like to replicate the control software on the Arduino but I'm pretty sure it will require a Mega to monitor all those inputs while also controlling the motors.

KenyanX

To bring everything full circle.  The new battery arrived and that with the code correction has fixed the issue.

Diablange

Hi,

I think I have the same issue, only difference in my project is I use 4 AA batteries in order to supply the motor shield and the 5V output of the motor shield to power my Arduino Uno.

A funny thing I have noticed is the behaviour is not constant : if I hold the robot in my hand, the motors do more than if I let the robot on the ground.

In my code I defined the outputs properly but it didn't change anything.

I'll try to connect a more powerful battery in order to solve the problem and I'll come back here in order to confirm the solution.

Brgds,

Sebastien

MarkT

Can the motor shield output 5V with only 6V input?   Won't there be drop-outs on the 6V anyway from
the motors?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up