8.b. Controlling a servo with an Arduino Leonardo

It is possible to modify the Servo library that comes with the Arduino IDE to use Timer 3 instead of Timer 1 on the Arduino Leonardo. The modified Servo library does not interfere with the ZumoMotors library, making it possible to simultaneously control servos and the motors.

Warning: The modifications described here will affect any sketch for the Arduino Leonardo that uses the Servo library.

  1. First, you will need to locate the Arduino IDE’s Servo library, which is somewhere inside your Arduino installation. The exact location of the Servo library depends on what version of the Arduino IDE you have. For the 1.0.x versions, it should be in the top-level libraries folder (i.e. arduino-1.0.x\libraries\Servo). In later versions, the Servo library is in arduino-1.5.x\hardware\arduino\avr\libraries\Servo. If you are using Mac OS X, you will need to right-click on the Arduino IDE icon and select “Show Package Contents” to see the files inside.
  2. Inside the Servo folder, you should see a file named Servo.h.
  3. Open Servo.h in a text editor.
  4. Locate the following lines of code in Servo.h:
    #elif defined(__AVR_ATmega32U4__)  
    #define _useTimer1 
    typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ;
  5. The lower two lines of code specify that the library should use Timer 1. To use Timer 3 instead, just change _useTimer1 to _useTimer3 and _timer1 to _timer3.
  6. Save Servo.h.

The Arduino IDE will automatically incorporate your modifications to the Servo library. The next time you compile a sketch for the Arduino Leonardo that uses the Servo library, it will use Timer 3 instead of Timer 1.