Which version of servo.h is able to use any digital pin?

Hi,

I'm starting to use servomotors in my projects.

www.c2o.pro.br/hackaguas/figuras/servo_torneira_3_vias.jpg

(From: THE PULSAR Engineering)

From what I understood the first versions of the Servo.h library allowed the control of a servomotor only by the digital pins 9 and 10.

But according to the Arduino Cookbook* (page 264) recent versions of Servo.h allow the use of any digital pin to control a servo.

*[Arduino Cookbook] Michael Margolis. Copyright © 2011. 978-0-596-80247-9. O'Reilly Media, Inc.. Arduino Cookbok.

I want to find some more recent reference to know exactly from which version of Servo.h this happened to be possible.

Thank you,
Markos

What version of the Arduino IDE are you using?

It has been a long time since the standard servo library could only deal with pins 9 and 10.

This is the sort of thing that you can answer for yourself with a short test program much more quickly than asking on a Forum. The Arduino system is great for learning-by-doing.

...R

Hi Robin,

I'm using the IDE version 1.8.5.

I agree with you about "learning-by-doing".

But I tried to find some any updated documentation before doing more tests with the servo on the board.

Thank you,
Markos

You can always look inside a library to find out how it works and read the documentation in the .h and .cpp files.

I followed your advice and found the Servo.h library in the folder:

arduino-1.8.5/libraries/Servo/src

And found the information:

"Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached."

And I found at Timer Interrupts and PWM Pins - Programming Questions - Arduino Forum that in UNO there are 3 timers (Timer 0, Timer 1 and Timer 2).

They control the following pins:

  • Pins 5 and 6: controlled by Timer 0
  • Pins 9 and 10: controlled by timer 1
  • Pins 11 and 3: controlled by timer 2

So if I want to control two servomotors, and not waste other digital pins to be able to use them with conventional PWM control I should only use the pairs: 5 and 6, 9 and 10, 11 and 3.

That's right?

Thanks,
Markos

Akvo:
They control the following pins:

  • Pins 5 and 6: controlled by Timer 0
  • Pins 9 and 10: controlled by timer 1
  • Pins 11 and 3: controlled by timer 2

So if I want to control two servomotors, and not waste other digital pins to be able to use them with conventional PWM control I should only use the pairs: 5 and 6, 9 and 10, 11 and 3.

That's right?

No.

The Servo library only uses Timer1 which means that it only prevents PWM being used on pins 9 and 10. You can operate servos on any pins - they don't have to be PWM pins. And you can use PWM on pins 3,5,6 and 11 while using servos on other pins.

Note that the PWM produced by analogWrite() on the PWM pins is not the same sort of pwm that is used to control a servo. It is unfortunate that the same acronym is used for both.

...R

Servo library only uses 16 bit timers, the only 16 bit timer on the Uno is timer1.
On the mega there are 4 16 bit timers, timer1/3/4/5.

I discovered this by reading the code of the library, as this is how to understand any library in detail.