PWM Accuracy for Hobby Servos [Solved]

I’d like to precisely control a hobby servo not using the servo library, but through control of the PWM peripheral (it happens to be a nano). I thought it might be a good project for trying to learn at a lower level, or at least at the directly changing registers level.

So far (from googling) I know that;

  • Standard servos work on a 50Hz (20ms) PWM
  • They (roughly) operate on 1ms pulses = 0deg and a 2ms pulse = 180deg

I’d like to control it to the nearest degree like the servo library, or better.

Now to the question:
If you set the PWM period to 20ms and have 8 bit PWM duty cycle resolution, because you’re only using a small range of the duty cycle (a 20th) you end up with something like 3-4 bit resolution. About 14 deg precision according to my maths. I’d be better off setting my own timer interrupts!

Am I missing something?? My C isn’t that great, is someone able to explain how the inbuilt servo library does it?

Thanks

Solved: It uses 16 bit timers

Am I missing something??

You're missing the Servo library, the source of which you are in possession of.

What would 12 bit PWM give you?
16 bit?

To start learning about using the hardware, have the data sheet handy and study some properly functioning code.

The Arduino servo library is open source, so consult it to learn how the timers are used.

Thanks for the replies

Looks like (Arduino Playground - MegaServo) it uses a 16 bit timer (not sure if that's hardware PWM or interupt), but I looked through the ATmega328 datasheet and it has 16 bit PWM! Not sure why arduino page lists the nano as having 8bit PWM, but I guess that answers my question.

Thank you again

Eager2Learn:
If you set the PWM period to 20ms and have 8 bit PWM duty cycle resolution, because you're only using a small range of the duty cycle (a 20th) you end up with something like 3-4 bit resolution. About 14 deg precision according to my maths.

But... the servo libary doesn't use PWM (not like the PWM pins).
Leo..

Wawa:
But... the servo libary doesn't use PWM (not like the PWM pins).
Leo..

...but it does use PWM.

the servo libary doesn't use PWM (not like the PWM pins).

That is not a very helpful thing to say, considering that the relevant hobby servos are controlled by PWM.

I think OP is comparing servo PWM with normal PWM pins, and somehow mixing up resolution.
I see a [solved] has been added to his post.
Leo...

Wawa:
I think OP is comparing servo PWM with normal PWM pins, and somehow mixing up resolution.
I see a [solved] has been added to his post.
Leo...

I was planning to use the inbuilt PWM functionality according to the ATmega328 datasheet by writing to registers to set the 50hz frequency and 1-2ms duty cycle (not analogwrite). I hadn't read the datasheet yet, I just got the 8bit pwm resolution from the arduino nano page and was worried i was barking up the wrong tree with trying to use the inbuilt pwm peripheral. Turns out it's 16bit, so it should work.

I don't know how or if the servo library uses the inbuilt peripheral, as I said my C isn't great and nobody seems to be in the mood to explain. But I got the answer of how you get 1 deg accuracy from 8 bit - you don't, you use 16 bit, so I added [solved]

Thanks all

https://www.arduino.cc/en/Reference/ServoWriteMicroseconds

Eager2Learn:
I'd like to precisely control a hobby servo not using the servo library

Hobby servos are not precision devices, you will be dissappointed unless you find
some decent spec'd (digital?) robotics servos that have repeatable and accurate performance.