skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries Servo

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library

Servo Library

The Servo or PWMServo library allows you to connect servo motors, commonly used in remote control airplanes and other vehicles.

Download: Servo is included with Arduino (modified for Teensy)

Hardware Requirements

Servo can control up to 12 or 24 individual motors, using any pins.

PWMServo can control up to 3 individual motors, using specific pins.

BoardServo LibraryPWMServo Library
1-12 Motors:
13-24 Motors:
1-3 Motors:
Teensy 1.0Any15, 17, 18-15, 17, 1815, 17, 18
Teensy 2.0Any94, 9, 14, 154, 14, 154, 14, 15
Teensy++ 1.0Any14, 15, 1614, 15, 16, 25, 26, 2725, 26, 2725, 26, 27
Teensy++ 2.0Any14, 15, 1614, 15, 16, 25, 26, 2725, 26, 2725, 26, 27

Both Servo and PWMServo use hardware timers, causing specific PWM pins to become unusable with analogWrite().

PWMServo is much more tolerant of interrupt latency that Servo. Libraries which disable interrupts, like NewSoftSerial, are incompatible with Servo, but can be used with PWMServo.

Most Servo motors require +5 volt power, at considerable current. Usually the black wire connects to ground and the red wire to +5 volts. Please check the specifications for your motors to be sure.

A few very small motors, like the ones above, can run from USB power. Direct connection to a PC or Mac or powered USB hub can provide 500 mA of current. Please be careful not to connect too many motors which could have a combined usage higher than 500 mA.

Larger motors will need a separate power supply. Usually the motor power supply ground much connect to the Teensy/USB ground pin, but the +5 volt lines remain separate, so the motors get their power from the external power supply, not USB.

Basic Usage

Servo myservo;

Create an instance of the Servo library. You will need to create an instance for each motor, giving each motor a unique name of your choice.


Specify the pin number where the motor's control signal is connected.

Position Control


Move the servo motor to a new angle, between 0 to 180 degrees. Some servo motors are not capable of a full 180 degree travel or have stability problems at the far ends of this range.

Example Program

The sweep example program slowly moves a servo back and forth. This program can be opened from the File -> Examples -> Servo menu. incremented, and written back.

// Sweep
// by BARRAGAN <> 

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 
void setup() 
  myservo.attach(20);  // attaches the servo on pin 20 
void loop() 
  for(pos = 10; pos < 170; pos += 1)  // goes from 10 degrees to 170 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 

Timer Usage

The Servo library uses a timer. Actually, a timer is used for each 12 motors. When the Servo library takes control of a timer, the PWM pins normally controlled by that timer will no longer function correctly.

This table can help you avoid using PWM pins which require the timers used by Servo.

Board1st to 12th Servo13th to 24th Servo
Timer UsedPWM Pins AffectedTimer UsedPWM Pins Affected
Teensy 1.0Timer 115, 17, 18-
Teensy 2.0Timer 39Timer 14, 14, 15
Teensy++ 1.0Timer 314, 15, 16Timer 125, 26, 27
Teensy++ 2.0Timer 314, 15, 16Timer 125, 26, 27

Interrupt Latency

Servo uses precisely timed interrupts to generate the control signals to many servo motors. Normally you do not need to be concerned about these interrupts. However, if your program disables interrupts, or you use a library which disables interrupts for substantial periods of time, Servo's signal accuracy can be degraded. Typical problems involve servo motors making small, unintended movements at infrequent, seemingly random times.

Libraries which disable interrupts for long times, likely to cause substantial problems:

  • SoftwareSerial
  • NewSoftSerial
Libraries which disable interrupts for short times, likely to cause only small errors:
  • OneWire
PWMServo can be used together with libraries that diable interrupts. If Servo is unreliable, you may need to use PWMServo.

Arduino Issue #146

Teensyduino contains interrupt-safe implementation of digitalWrite() and pinMode(), so use of these functions or libraries such as LiquidCrystal that many heavy use of these functions should not impact Servo on Teensy.

However, some libraries directly manipulate pins without disabling interrupts, which is likely to interfere with Servo. If you discover another library which interferes with Servo, please contact paul AT pjrc DOT com.

More Details

Please refer to the official Servo library documentation for more details.