Using the Adafruit Library

It's easy to control servos with the Adafruit 16-channel servo driver. There are multiple CircuitPython libraries available to work with the different features of this board including Adafruit CircuitPython PCA9685, and Adafruit CircuitPython ServoKit. These libraries make it easy to write Python code to control servo motors.

You can use this breakout with your Raspberry Pi and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

Python Installation of ServoKit Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-servokit

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

Controlling Servos

We've written a handy CircuitPython library for the various PWM/Servo boards called Adafruit CircuitPython ServoKit that handles all the complicated setup for you. All you need to do is import the appropriate class from the library, and then all the features of that class are available for use. We're going to show you how to import the ServoKit class and use it to control servo motors with the Adafruit 16-chanel servo driver breakout.

First you'll need to import and initialize the ServoKit class. You must specify the number of channels available on your board. The breakout has 16 channels, so when you create the class object, you will specify 16.

Download: file
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

Now you're ready to control both standard and continuous rotation servos.

Standard Servos

To control a standard servo, you need to specify the channel the servo is connected to. You can then control movement by setting angle to the number of degrees.

For example to move the servo connected to channel 0 to 180 degrees:

Download: file
kit.servo[0].angle = 180

To return the servo to 0 degrees:

Download: file
kit.servo[0].angle = 0

With a standard servo, you specify the position as an angle. The angle will always be between 0 and the actuation range. The default is 180 degrees but your servo may have a smaller sweep. You can change the total angle by setting actuation_range.

For example, to set the actuation range to 160 degrees:

Download: file
kit.servo[0].actuation_range = 160

Often the range an individual servo recognises varies a bit from other servos. If the servo didn't sweep the full expected range, then try adjusting the minimum and maximum pulse widths using set_pulse_width_range(min_pulse, max_pulse).

To set the pulse width range to a minimum of 1000 and a maximum of 2000:

Download: file
kit.servo[0].set_pulse_width_range(1000, 2000)

That's all there is to controlling standard servos with the PWM/Servo HAT or Bonnet, Python and ServoKit!

Continuous Rotation Servos

To control a continuous rotation servo, you must specify the channel the servo is on. Then you can control movement using throttle.

For example, to start the continuous rotation servo connected to channel 1 to full throttle forwards:

Download: file
kit.continuous_servo[1].throttle = 1

To start the continuous rotation servo connected to channel 1 to full reverse throttle:

Download: file
kit.continuous_servo[1].throttle = -1

To set half throttle, use a decimal:

Download: file
kit.continuous_servo[1].throttle = 0.5

And, to stop continuous rotation servo movement set throttle to 0:

Download: file
kit.continuous_servo[1].throttle = 0

That's all there is to controlling continuous rotation servos with the PWM/Servo breakout, Python and ServoKit!

This guide was first published on Aug 16, 2012. It was last updated on 2019-01-17 16:07:16 -0500. This page (Using the Adafruit Library) was last updated on Jan 17, 2019.