will be read-only starting December 31st, 2018. For more info pleae look at this Forum Post

MegaServo Hardware Servo library

This library allows an Arduino board to control one to twelve RC (hobby) servo motors on a standard Arduino board or up to 48 servos on an Arduino Mega. Each servo can be attached to any unused digital pin.

The library (renamed to Servo) is distributed with Arduino releases from 0017, see the Arduino servo library reference for details. You already have this code if you are using the servo library from release 0017 or later.

The library has the following features:

  • Controls up to 48 servos (Arduino Mega only, other boards support up to 12 servos)
  • Any digital pin can be used with any servo
  • Pulse widths can be written and read in degrees or microseconds.

This library uses a 16 bit timer for each group of 12 servos so PWM output with analogWrite() for pins associated with these timers are disabled when the first servo is attached to the timer. For example on a standard Arduino board, Timer1 is used, so once you attach a servo, analogWrite on pins 9 and 10 are disabled.

Here is a table of PWM pin usage on the Mega board:

Servos analogWrite PinsTimers used
1 - 12not pins 44,45,46Timer 5
13 - 24not pins 11,12,44,45,46Timers 1 & 5
24 - 36not pins 6,7,8,11,12,44,45,46Timers 1,4 & 5
37 - 48not pins 2,3,5,6,7,8,11,12,44,45,46Timers 1,3,4 & 5

New version updated 8 Jun:

 - supports boards with 8MHz clock. 
 - read_us() renamed to readMicroseconds()
 - writeMicroseconds() method added 


Servo motors have three wires: power, ground, and signal. The power wire is typically red, and can be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to the pins attached in your sketch. You probably need to use an external power supply for more than one or two servos, don’t forget to connect the ground of the power supply to Arduino and servo grounds.


  • attach()
  • write()
  • writeMicroseconds() - new function takes parameters in Microseconds
  • read() - returns the angle in degrees)
  • readMicroseconds() - returns angle in uS, renamed from read_us
  • attached()
  • detach()

Note that write() expects parameters as an angle from 0 to 180 writeMicroseconds() expects values as microseconds.


The standard Arduino servo examples will work unchanged with this library. The following code demonstrates how to position 12 servos according to the voltage on potPin:

#include <MegaServo.h>
#define NBR_SERVOS 12  // the number of servos, up to 48 for Mega, 12 for other boards
#define FIRST_SERVO_PIN 2 

MegaServo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

int pos = 0;      // variable to store the servo position 
int potPin = 0;   // connect a pot to this pin.

void setup()
  for( int i =0; i < NBR_SERVOS; i++)
    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);
void loop()
  pos = analogRead(potPin);   // read a value from 0 to 1023
  for( int i =0; i <NBR_SERVOS; i++) 
    Servos[i].write( map(pos, 0,1023,0,180));