PWMServo

Arduino 0016 introduced a spanking new Servo library that makes it possible to control a Servo from any pin. The old library used PWM, which limited its use to pins 9 or 10.

Unfortunately, the new library uses an interrupt-based technology which breaks NewSoftSerial. If you’re building a project that needs Servo and Soft serial, you might try this library instead. (I didn’t write it; this is really just the old library renamed.)

[August, 2011 update]
Paul Stoffregen recently updated the library to include support for Teensy, Teensy++, Sanguino, and the Mega varieties and Arduino 1.xx.  This is really a nice and much appreciated improvement. Because of the additional timers that the Mega and Teensy processors provide, these platforms can support a third simultaneous servo.

Thanks Paul!

The pins that are available for controller servos vary from controller to controller, so here’s a tidy table showing you which pins are available:

Board                     SERVO_PIN_A   SERVO_PIN_B   SERVO_PIN_C
-----                     -----------   -----------   -----------
Arduino Uno, Duemilanove       9            10          (none)
Arduino Mega                  11            12            13
Sanguino                      13            12          (none)
Teensy 1.0                    17            18            15
Teensy 2.0                    14            15             4
Teensy++ 1.0 or 2.0           25            26            27

Download

Download the original PWMServo library (as it was shipped with pre-0016 Arduino). (This is useful mostly only for historical reasons.)
Download Paul’s version 2 library with Teensy, Teensy++, Mega, and Sanguino support. This version also supports Arduino 1.xx.

Mikal

Page last updated on October 15, 2012 at 9:30 am
23 Responses → “PWMServo”

  1. Kevin

    3 years ago

    Mikal,
    Can you explain how the old PWM works? If it isn’t interrupt based is it reliable? Are there any reasons to not use it?
    I am trying to use it in a very simple example sketch to drive an RC car through and ESC and it is very erratic. I can’t seem to identify what the RC car needs to drive it reliably. I can get the wheels to turn but not in a controllable, methodical way. I learned that this library doesn’t have the writeMicroseconds method available.
    Your thoughts?


  2. Mikal

    3 years ago

    Kevin,

    The old servo library is very reliable. It coexists with other libraries like tone and NewSoftSerial a lot better. The only drawback is that it only supports a total of 2 servos and these must be on pins 9 and 10.

    Mikal


  3. Desmond

    3 years ago

    desmond,

    Is PWMservo the new or OLD servo library? Because i want to be able to drive a single servo while using newsoftserial.


  4. Mikal

    3 years ago

    PWMServo is the library you want if you want to use NewSoftSerial at the same time.

    Mikal


  5. Desmond

    3 years ago

    Mikal,

    COuld you please tell me where i would find information on this library. Because i need to be able to produce a PWM that i can adjust the duty cycle to drive a normal motor.
    And
    i need to be able to turn a normal motor 90degress in both direction.

    How do i use this library to do that.


  6. Mikal

    3 years ago

    Desmond, this library has (mostly) the same interface as the current Servo library. (See the documentation in arduino.cc for that.) Neither of them will allow you to drive a normal motor. That’s a different technology.

    Mikal


  7. Charles

    3 years ago

    Mikal,

    Is it possible to rework the PWMServo library to work on pins other than 9 and 10? I am working on a project with a duemilanove ATmega328 with a sdmicro shield using pin 10, and I have two servos that I need to control. I also have several serial devices which require the use of newsoftserial.

    Thanks for any help you can offer!

    -Charles


  8. Nikko

    2 years ago

    This library does not seem to have been updated to function with arduino 1.0, can we expect a fix any time soon?


  9. Mikal

    2 years ago

    @Nikko,

    I believe the version 2 PWMServo that Paul Stoffregen built (posted here), works correctly in Arduino 1.0.


  10. Antonio Siqueira

    2 years ago

    Hi,

    I need control 16 servos and use USB/serial port default on Arduino Uno or 2009.

    Can this library help me?

    Thank you,
    AC


  11. Mikal

    2 years ago

    @Antonio,

    No, PWMServo is the old Arduino servo library. It can only support 2 servos and these must live on pin 9 or 10.


  12. Mark Grass

    1 year ago

    I’m trying to use the PWMservo library but the neither of the examples compile. I get the following errors:
    Knob.pde:-1: error: ‘PWMServo’ does not name a type
    Knob.cpp: In function ‘void setup()’:
    Knob.pde:-1: error: ‘myservo’ was not declared in this scope
    Knob.pde:-1: error: ‘SERVO_PIN_A’ was not declared in this scope
    Knob.cpp: In function ‘void loop()’:
    Knob.pde:-1: error: ‘myservo’ was not declared in this scope

    Any idea on what’s wrong?


  13. Mikal

    1 year ago

    @Mark,

    What processor are you trying to compile for? This is pretty old code that probably doesn’t support it.


  14. Lazaros

    1 year ago

    I have the same problem with Mark Grass but instead, I have the issue with SoftwareServo library from http://playground.arduino.cc/ComponentLib/Servo.
    I want to control up to 8 servos with arduino UNO -> ATMEGA328P-PU so I need this functionality for non-PWM pins. Any alternative suggestion?


  15. Ben

    1 year ago

    Hi,
    I need to use only one pin to control a servo but I also need to use every single other PWM pin on my Arduino Uno. The new servo library disables PWM on pins 9 and 10, therefore I am 2 pins short on my project. I tried to use “Paul’s version 2 library”, however whenever I used the attach method onto pin 9, the PWM on pin 10 stopped working. Is their a way to control one servo and to still have PWM capabilities on ALL other PWM capable pins using this library? or is their another way to make it work?


  16. Mikal

    1 year ago

    @Ben,

    I’m sorry to say that I can’t think of a better solution than to migrate to the Mega. Sorry!


  17. aleem

    10 months ago

    hello guys
    i have the same problem , when i attached to the motor driver shield to arduini uno , the easyvr stopped listening to my voices commands .
    and you suggested using PWMServo.h instead of Servo.h but i am using AFMotor.h . is there any other library which offers same flexibility as PWMServor , for motors ???


  18. F6AGV

    3 months ago

    Hi Mikal,
    Best regards from France, with BRAF : Ballons Radioamateurs France
    First ballooning association.
    See also : http://www.radioamateurs-france.com/

    Alan F6AGV – BRAF


  19. Touchstone

    3 months ago

    Getting the following errors when I try and compile:

    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp: In static member function ‘static void PWMServo::seizeTimer1()’:
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:40: error: ‘clockCyclesPerMicrosecond’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp: In member function ‘uint8_t PWMServo::attach(int, int, int)’:
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:72: error: ‘LOW’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:72: error: ‘digitalWrite’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:73: error: ‘OUTPUT’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:73: error: ‘pinMode’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp: In member function ‘void PWMServo::detach()’:
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:95: error: ‘INPUT’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:95: error: ‘pinMode’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:101: error: ‘INPUT’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:101: error: ‘pinMode’ was not declared in this scope
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp: In member function ‘void PWMServo::write(int)’:
    C:\Users\Mike\Documents\Arduino\libraries\PWMServo\PWMServo.cpp:118: error: ‘clockCyclesPerMicrosecond’ was not declared in this scope

    Any suggestions?


  20. Mikal

    2 months ago

    @F6AGV, fantastic. Ballooning is FUN!


  21. Mikal

    2 months ago

    @Touchstone, Try rebuilding with a fresh copy of PWMServo and a new version of Arduino. I think there is some kind of version mismatch here.


  22. Michael

    4 weeks ago

    I am trying to use this with an Arduino Mega and AdaFruit Motorshield v.2 but do not seem to get it working. I am attaching 2 servos to pin 9 and 10 or 11 and 12 but have no response.

    Is there anything I am missing out ?

1 Trackbacks For This Post
  1. My Proposal Box, Part 3: Software

    [...] the Arduino IDE installed already, click here to download it. You will also need to download the PWMServo library (version 2) and the TinyGPS library written by Mikal Hart. While you are at his site, thank him for [...]

Leave a Reply