The TiCoServo Library

If you just want to download and use the library, that’s totally okay. Please just be aware of the following limitations:

  • This only works on certain Arduino-compatible boards. All the most common hardware with an 8-bit AVR microcontroller should be fine (Arduino Uno, Duemilanove, Leonardo, Mega, Pro Trinket, Teensy 2, etc.). “Cutting edge” boards using other microcontrollers (Arduino Due, Teensy 3, etc.) are right out.
  • On Trinket and Gemma, only about eight distinct servo positions are possible. (Pro Trinket has full resolution.)
  • Servos only work on very specific pins:

Board

Pins

Arduino Uno, Duemilanove, Diecimila, Adafruit Pro Trinket, Boarduino, Menta (anything w/ATmega328P or ATmega168)

9, 10

Arduino Leonardo, Micro

5, 9, 10, 11

Adafruit FLORA

D9, D10

PJRC Teensy 2.0 (not Teensy+ or 3.X)

4, 9, 14, 15

Arduino Mega

2, 3, 5, 6, 7, 8, 11, 12, 13, 44, 45, 46

Adafruit Trinket

1, 4

Adafruit Gemma

D1

Library installation is a common sticking point for beginners…our All About Arduino Libraries guide explains how this is done.

After installing the libraries, restart the Arduino IDE.

There are two simple examples that mix servos and NeoPixels. One will work on an Adafruit Gemma or Trinket, the other on an Arduino Uno or most other mainstream boards (Leonardo, etc.). You might need to change some pin numbers (NeoPixel pin #, etc.).

The library is modeled after the official Arduino Servo library…all functions and arguments are identical, and you can simply refer to the Arduino site for reference. Except for the pin-number limitations, most Arduino Servo sketches should be nearly drop-in compatible with just a few changes:

Instead of:

#include <Servo.h>

use:

#include <Adafruit_TiCoServo.h>

The servo declaration changes from:

Servo myservo; // create servo object to control a servo

to:

Adafruit_TiCoServo myservo; // create servo object to control a servo

The attach(), write() and other functions then operate identically to the standard Servo library counterparts…unless you’re using a Trinket or Gemma…

Special Considerations for Trinket and Gemma

Since they’re based on the diminutive ATtiny85 microcontroller, these boards work a little differently.

First, one extra #include line is needed at the top of the code:

#include <avr/power.h>

Then add the following to the setup() function. It’s important that this appear before calling servo.attach()!

#if (F_CPU == 16000000L)
  clock_prescale_set(clock_div_1);
#endif

Unlike the “big boy” code that works with either degrees or microseconds, the “tiny” version can only specify servo positions in raw “ticks,” where each tick is equal to about 128 microseconds. Given that most servos nominally want a timing pulse between 1,000 and 2,000 microseconds, that means values from 8 to 15 ticks are a reasonable range. Every servo is a little different though…some have more or less range, so you might be okay adjusting these values slightly.

This isn’t the big downer it might seem. Many projects only require two servo positions (e.g. a gate, flag or valve switching between open and closed positions).

With such coarse granularity, degree values are faily meaningless and thus unsupported. You can use the Arduino map() function if you really want to, but whole ranges will just map to a single value. Better to use raw ticks, since you know every value represents a distinct position, even if they don’t intuitively relate to a simple degree multiplier.

This guide was first published on Nov 21, 2014. It was last updated on Sep 20, 2018. This page (The TiCoServo Library) was last updated on May 04, 2015.