Problem Controlling 2 Motors with a PWM output.

Hi there, Here is my code:

 /* Simple PWM output code- Current issue, both wheels dont run at max speed (255) 100% duty 
at the same time, although when one is set to 255 and the other to a lower value i.e. 1 it runs at max speed. */ 


void setup() {

   
   pinMode(6, OUTPUT);   //Sets the left PWM as an output
   pinMode(3, OUTPUT);   //Sets the Right PWM as an output
}

void loop() {
  analogWrite(6, 255);      //Output a PWM motor speed for the left motor- when both 255 doesnt work 
  analogWrite(3, 255);      //Output a PWM motor speed for the right motor - when ones at 1 and other is at 255 it runs at full speed.
}

[Code] 


I am trying to control 2 DC motors using 2 PWM outputs, I noticed a problem when I try to make both motors go full speed at the same time. It works when I want one motor to go full speed and the other a lower speed ie 1. But when both motors are 100% they don't move at all. I have uploaded a photo of my circuit diagram. I am using the PWM to control the flow to ground using a TIP120 transistor. 


[img]http://tinypic.com/r/znsz6t/9[/img]


Many Thanks.  :)

Do you have a common ground between the motor supply the tip120 emitters and the arduino?

The circuit should work.

If it doesn't work at all with both at 255: The problem you are describing makes sense without a common ground, when you write one pin low, you are effectively creating a connection between arduino ground through the equivalent resistance of the base-emmiter (looks like 8kOhms from the datasheet). When the pin is high, the grounds are no longer connected.

Just a guess.

If you mean it just runs slower with two motors at 255 I would suspect the battery.

No im not using a common ground between the motor supply and the arduino, when previously connecting in this configuration the arduino kept resetting. But I will try it again. How would you suggest connecting the grounds? just connect the the negative terminal of the motor supply to the ground of the arduino as well? I dont think its the battery as they both run fine in a simple circuit. Thanks for your help.

A common ground is required. Just connect a line from the ground of the arduino to the negative of the battery. This should also be the same point as where the tip120 emitters are connected.

If you are using the batteries to power the arduino and the arduino resets when driving the motors, The batteries might not be able to supply the current needed for two motors. The voltage will droop and the arduino will reset when it reaches its brown out detection level.

If you are powering the arduino from a separate power supply it could be stray inductance causing a reset.

What kind of battery are you using?

A cap of a few hundred uf should be used on the supply rail and and decoupling caps should be used to reduce spikes and electrical noise across the motor terminals.

Grumpy Mike put together a good writeup on decoupling .

http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

(although he shows a cap stamped 103 when talking about 0.1uf cap. This should be 104.)

Ok I will try that, im using a 9V battery to power the arduino and 4 AA's to power the motor circuit. I will look into the de-coupling. Thanks for the link.

Thanks alka, I used a common ground and works fine now. Thanks for your help. I haven't had any issues with resetting this time round compared to when I built it in the breadboard.

You probably had poor layout on the breadboard, causing noise to be injected back into the Arduino.

Using good layout is standard practice when high current loads are driven near logic circuitry.