Motors not spinning at the same speed

Hi, I recently purchased 2 for https://www.pololu.com/product/2275 and I used an arduino along with a shield to control them. When I send the same signal (md.setM1Speed(400) and md.setM2Speed(400)) the two motors don’t spin at the same rate. I wonder if there is a fix for this both in the code or in the hardware to make sure both spin at the same rate. Thank you for your help.

Just to add, I created some codes that made sure the total rotations of both motors match each other but when I put the car on the ground, it still steers a lot.

Motors of the same type are nevertheless slightly different from each other. In order to have two motors spin at the same speed, you need shaft encoders to measure that speed, and use PID control.

Sometimes it works just to adjust the PWM values slightly, but that is unlikely to be effective over a range of speeds.

1 Like

Thank you for your help. So with the encoders that I have right now and an arduino do you think I can measure the speed of two motors at the same time. I read that I may have to use interrupt a lot. Thank you.

The Pololu software libraries support the encoders, but you will need to study the documentation carefully. You have several options.

One is to use the encoders to determine speed and PID control to force the motors to run at the same speed.

Another is to use the encoders to measure distance and have the motors travel the same distance in the same period of time. That amounts to the same thing in terms of speed, but the first option does not allow you to easily set a precise distance goal.

As well as differences due to manufacturing tolerances, the brush offsets on many DC motors are ‘timed’ such that they operate most efficiently for a given speed in one direction only. In a lot of applications this is what you want, but on a differential drive robot you need one of the motors operating in reverse in order to drive in a straight line and this causes problems.

You’ll be unlikely to find CW/CCW variants for most motors, but sometimes if you examine the rear of the motor (or take the back right off) you can see that the manufacturer has provided the means for it. Some have different indentations in the plastic backplate (so bend up the tabs of the metal can, rotate the plastic housing slightly, bend them down again) and some have moulded slots for holding the brushes, so you remove the brushes and replace them in the alternative positions.

I’d definitely recommend the PID route (with speed or distance input) but ALSO scale the PWM output from the PID according to your own empirical measurements, ideally taken at a few different speed points and in both directions and factoring in the battery voltage. This helps to linearise the overall loop and will give the best response from the PID.