Let's Make Robots!

PWM for servo and motor speed

So I've looked around and see that Arduino motor shield board uses timer1  (pins 9 and 10) for hardware PWM control of motor speed. There are Arduino libraries that use timer1 for hardware PWM control of servos.

Timer1 gives you 16 bit resolution. The other timers (timer0 and timer2) have only 8 bit resolution.

I can see needing 16 bit resolution for the servo. The servo control pulse rate is 50 Hz or 20 ms period. To move the servo through its range you need to vary the pulse width from 1.0ms to 2.0ms. Dividing the 20ms period by 65536 increments (2^16) you get 3276 increments in 1ms.  Doing the same thing with 8 bit resolution gets you 256 / 20 ms or 12.8 increments in that 1ms time. Since more is better (3276 vs 12.8) you'd control the servo with 16 bit resolution timer1 rather than 8 bit resolution timer0 or timer2. 

You've also got the choice of 16-bit or 8-bit resolution for your motor speed control. That's 65536 speed settings versus 256 speed settings. I think the robot would be happy with just 256 speed settings.

I'm building a simple robot with a Sharp IR sensor mounted on a servo and a pair of Solarbotics GM9 motors. The Arduino Motor Shield uses timer1 for the motor control, leaving me with timer0 or timer0 for 8 bit servo positioning. I think I could live with the 12.8 increments in servo positioning since the robot just needs to look left, right, and forward for obstacles. Alternatively I could go with software PWM on the servo to get more resolution in positioning.

Maybe it's me but I think the Arduini motor shield designer is wasting 16-bit PWM on motor control. How would other Arduino users set up motor and servo control?



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ya im retarded. We have so may different measurement systems we all cant just use one. We have to mix and match all the time to be consistent.

most servos have a range of @210 degrees. 8 bits will allow you 256 steps which is 0.82 degrees per step. You couldnt notice that with your naked eye unless the peripheral is about 3 feet long and then it would only move a couple cm's. I think 256 bits is more than enough. Also servos use potentiometers for feedback positioning, Potentiometers are probably one of the most in accurate devices from one to another evn among same brand and lot #'s. Most potentiometers are a +/- 20% accuracy so from one servo to another you will have a good deal of inaccuaracy.

No one on earth needs .0032 degrees per step you woulnt even be able to tell it changed.

If you look at the servo lib source code you don't get all that resolution for servo movement. You have to time out 20ms with the counter bits, then you get 1/20th of that for servo movement. So that is why I say you get 3276 steps for the 16 bit timer and only 12.8 for the 8 bit timer. The 1/20th is from the amount you have to vary the pwm pulse width to sweep the servo through its range.

But yeah, you are right, even 256 bits of control for the servo position is excessive for panning an IR sensor around.

The position resolution is hidden by the servo lib since you pass in the desired postion in degrees, and the lib converts that to PWM values.


"... unless the peripheral is about 3 feet long and then it would only move a couple cm's."

I love the mix of units in that statement. You may live in New Jersey, but that's so British.


Hello ChipsAhoy.

I don't use a motor shield but just configure a pin for PWM - and driving large motors.  In my context 65K positions is ludicrous , 256 is too many to decern perceptible change from one increment to another.


i am using this one from ladyada

so far i like how it performs

That looks like a pretty good motor shield... two servos off the 16 bit pwm timer, up to 4 dc motors off the 8 bit pwm timers. Lools like it has space for connectors so that you could drive additional servos with software pwm out the analog pins. I like it better that the one that I have.... dang!


it really versitile..not only that you could replace the H-bridge with the bigger SN754410 for more power and if you add the extended headers you will have access to the unused pins

which motor sheild are you useing

because the one i have only uses those 16 bit pins for the servos


This one: http://www.arduino.cc/en/Main/ArduinoMotorShield

Schematic: http://webzone.k3.mah.se/k3dacu/arduino/shields/motor_control/motor_control_v1_300dpi.png

Tracing the pins back I get this:

pin      motor shield signal 

8         E1
9         PWMB
10       PWMA
11       E2
12       DIRA
13       DIRB
AREF   Unused

We can ignore the E1 and E2 pins since they are for the encoder circuits. 

Which one are you using?