analogWrite vs. servo.write

Hi, I am wondering is there a different between analogWrite and Servo.write when I use the servo library? Arn't they both PWM?

Yes, yes.

Technically, while they are both PWM, servos use a particular kind of PWM - called PPM, or "Pulse Position Modulation".

It's a form of PWM in which the length of the pulses determine the angle of the servo itself. Center being a 1.5 ms (millisecond) pulse, with each end-point extreme being represented by a 1 or 2 ms pulse - with all of the channels repeated within a 20 millisecond "window" of time (IIRC). For example, one servo:

|500x350

...and multiple servos (or channels):

|500x234

It is basically the range of duty cycle. With PWM the duty cycle is adjustable over the full range from 0 to 100%. When you are driving a servo you only have a very tiny range of duty cycle to play with.

Is it not true though Mike, that for a servo it's the absolute on-time that defines the position, not the ratio of on to off? (With the ratio being what's important in producing the pseudo analog voltage between 0 and 5V.)

I say that since the total cycle can be changed from the default 20ms. If that was changed to say 10ms, the "full-left" pulse would still be an absolute 1ms, but now being twice as long relative to 10ms as it was to the original 20ms.

Ok I over simplified it my not mentioning the absolute time as well as the duty cycle, but the argument is still the same. A 12 bit PWM generator can be made to drive a servo and an dimming LED on different channels if you get the numbers right.

Grumpy_Mike:
Ok I over simplified it

Have to keep you honest…

By the way, for OP's background, the initial starting position on servo.attach(), and the frequency of the servo pulse, can be changed from their defaults by editing these lines in servo.h:

#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached
#define REFRESH_INTERVAL    20000     // minumim time to refresh servos in microseconds

Thank you all for your replies.

JimboZA: By the way, for OP's background, the initial starting position on servo.attach(), and the frequency of the servo pulse, can be changed from their defaults by editing these lines in servo.h:

#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached
#define REFRESH_INTERVAL    20000     // minumim time to refresh servos in microseconds

@ JimboZA, my trouble is the initial state of the servos. In my code, I set all servos to 90 degrees as initial position. However, eery time I power up th servos, they go crazy for a while, until the code kicks in to set every thing into 90 degrees than they get stabilized.. What I like to know is how I can avoid the craziness when the servos are powered up.

What I like to know is how I can avoid the craziness when the servos are powered up.

You could try a pull down resistor on the control line. This would not stop any movement of the servos but would stop the "crazy" movement that you get when the control input is floating.

Ok I over simplified it my not mentioning the absolute time as well as the duty cycle, but the argument is still the same. A 12 bit PWM generator can be made to drive a servo and an dimming LED on different channels if you get the numbers right.

The resolution is totally different . Servo library gives you much more resolution of the position than analogWrite.

The resolution is totally different .

No it is not. You can move a servo quite well with just 12 bits.

AnalogWrite is 0 - 255, (8-bits)

So is there any advantage to directly manipulate the timing of the duty cycle to drive a servo rather then using Servo.write? Would I get more precision servo angle?

I don't know but I doubt it. If you could I think we would already know about it and be doing it.