Overview

The examples in this guide are no longer supported. Check out the PCA9685 driver guide for CircuitPython and Python usage: https://learn.adafruit.com/16-channel-pwm-servo-driver
Note the video above was made showing the MicroPython version of this library. Follow the guide to see both CircuitPython and MicroPython versions of the PCA9685 library.

Servo motors are one way to make projects come to life with exciting movements like steering robots, flipping switches, and more.  To control a servo you need to generate a special PWM, or pulse-width modulation, signal.  Most development boards can generate a few basic PWM signals, but what if you need to control a lot of servos or don't have a board with PWM support?  The PCA9685 PWM driver board comes to the rescue!  This driver board can generate up to 16-channels (or 8-channels in FeatherWing form) of PWM signals--perfect for driving many servos!

This guide explores how to use the PCA9685 PWM & Servo driver with CircuitPython and MicroPython.  You'll learn how to connect the PCA9685 to a board and use it to control LED brightness and move servos from Python code.

For more background check out these guides on servo motors:

Hardware

The examples in this guide are no longer supported. Check out the PCA9685 driver guide for CircuitPython and Python usage: https://learn.adafruit.com/16-channel-pwm-servo-driver

Parts

You'll need the following parts to follow this guide:

CircuitPython board.  This guide focuses on the ESP8266 and Feather M0/SAMD21-based boards, but any CircuitPython board that supports I2C should work.

 

If your board doesn't come with CircuitPython running on it already then check out your board's guide for how to load CircuitPython firmware.  For example the Feather M0 express guide is a good reference.

 

If you're using a Feather board and FeatherWing you probably want a Feather female header set or Feather stacking female header set.

PCA9685 PWM & Servo Driver Board.  If you're using a Feather the 8-channel PWM & Servo FeatherWing is the perfect option.  If you need more channels or aren't using a Feather grab the 16-channel PCA9685 breakout board or even 16-channel PCA9685 Arduino shield.

5V Power Supply.  To power servos you almost always need an external 5V power supply.  Servos can pull a lot of power and could damage your board if powered directly from it!  A simple power option is a 4x AA battery pack or a 5V power supply and barrel jack screw terminal connector.

Servos or LEDs.  You'll want something to drive with the PCA9685, like servos you can move or LEDs you can dim.  Remember the PCA9685 only drives servos, not stepper or other motors.

Breadboard and jumper wires.  If you aren't using a Feather and FeatherWing you'll need a breadboard and jumper wires to connect the components.

 

Soldering tools.  You'll need to solder headers to the boards  Check out the guide to excellent soldering if you're new to soldering.

Make sure to follow the board and PCA9685 driver board product guides to assemble and verify they work before continuing.

Wiring

If you're using a Feather and the PCA9685 FeatherWing just slide the wing into the Feather's headers and you're done!  The wing will use an I2C connection to talk to the Feather board.

If you're using a PCA9685 breakout you'll need to connect its I2C and power pins as follows:

  • Board SCL / I2C clock to PCA9685 SCL.
  • Board SDA / I2C data to PCA9685 SDA.
  • Board 3.3V power to PCA9685 VCC.
  • Board GND / ground to PCA9685 GND / ground.

Once your board is wired to the PCA9685 continue on to learn how to use a MicroPython module to control servos and LEDs!

CircuitPython

The examples in this guide are no longer supported. Check out the PCA9685 driver guide for CircuitPython and Python usage: https://learn.adafruit.com/16-channel-pwm-servo-driver

Adafruit CircuitPython Module Install

To use the PCA9685 with your Adafruit CircuitPython board you'll need to install the Adafruit_CircuitPython_PCA9685 module on your board. Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

First make sure you are running the latest version of Adafruit CircuitPython for your board.  Next you'll need to install the necessary libraries to use the hardware--read below and carefully follow the referenced steps to find and install these libraries from Adafruit's CircuitPython library bundle with a version 20180110 or newer. 

Bundle Install

For express boards that have extra flash storage, like the Feather/Metro M0 express and Circuit Playground express, you can easily install the necessary libraries with Adafruit's CircuitPython bundle.  This is an all-in-one package that includes the necessary libraries to use the PCA9685 with CircuitPython.  To install the bundle follow the steps in your board's guide, like these steps for the Feather M0 express board.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_pca9685
  • adafruit_bus_device
  • adafruit_register
  • adafruit_motor

If your board supports USB mass storage, like the M0-based boards, then simply drag the files to the board's file system. Note on boards without external SPI flash, like a Feather M0 or Trinket/Gemma M0, you might run into issues on Mac OSX with hidden files taking up too much space when drag and drop copying, see this page for a workaround.

If your board doesn't support USB mass storage, like the ESP8266, then use a tool like ampy to copy the file to the board. You can use the latest version of ampy and its new directory copy command to easily move module directories to the board.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_pca9685adafruit_bus_device, adafruit_register and adafruit_motor folders/modules copied over.

This guide is for version 3.0.0 of the PCA9685 library. Make sure to use a bundle from 20180110 or later.

Usage

The following section will show how to control the PCA9685 from the board's Python prompt / REPL.  You'll learn how to interactively control servos and dim LEDs by typing in the code below. 

First connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

I2C Initialization

First you'll need to initialize the I2C bus for your board. First import the necessary modules:

Download: file
import board
import busio

Now for either board run this command to create the I2C instance using the default SCL and SDA pins (which will be marked on the boards pins if using a Feather or similar Adafruit board):

Download: file
i2c = busio.I2C(board.SCL, board.SDA)

After initializing the I2C interface you need to import the PCA9685 module to use it in your own code:

Download: file
import adafruit_pca9685

Dimming LED's

Each channel of the PCA9685 can be used to control the brightness of an LED.  The PCA9685 generates a high-speed PWM signal which turns the LED on and off very quickly.  If the LED is turned on longer than turned off it will appear brighter to your eyes.

First wire a LED to the board as follows.  Note you don't need to use a resistor to limit current through the LED as the PCA9685 will limit the current to around 10mA:

  • LED cathode / shorter leg to PCA9685 channel GND / ground.
  • LED anode / longer leg to PCA9685 channel PWM.

Now in the Python REPL you can create an instance of the basic PCA9685 class which provides low-level PWM control of the board's channels:

Download: file
pca = adafruit_pca9685.PCA9685(i2c)

The PCA9685 class provides control of the PWM frequency and each channel's duty cycle.  Check out the PCA9685 class documentation for more details.

For dimming LEDs you typically don't need to use a fast PWM signal frequency and can set the board's PWM frequency to 60hz by setting the frequency attribute:

Download: file
pca.frequency = 60

The PCA9685 supports 16 separate channels that share a frequency but can have independent duty cycles. That way you could dim 16 LEDs separately!

The PCA9685 object has a channels attribute which has an object for each channel that can control the duty cycle. To get the individual channel use the [] to index into channels.

Download: file
led_channel = pca.channels[0]

Now control the LED brightness by controlling the duty cycle of the channel connected to the LED. The duty cycle value should be a 16-bit value, i.e. 0 to 0xffff, which represents what percent of the time the signal is on vs. off.  A value of 0xffff is 100% brightness, 0 is 0% brightness, and in-between values go from 0% to 100% brightness.

For example set the LED completely on with a duty cycle of 0xffff:

Download: file
led_channel.duty_cycle = 0xffff

After running the command above you should see the LED light up at full brightness!

Now turn the LED off with a duty cycle of 0:

Download: file
led_channel.duty_cycle = 0

Try an in-between value like 1000:

Download: file
led_channel.duty_cycle = 1000

You should see the LED dimly lit.  Try experimenting with other duty cycle values to see how the LED changes brightness!

For example make the LED glow on and off by setting duty_cycle in a loop:

Download: file
# Increase brightness:
for i in range(0xffff):
    led_channel.duty_cycle = i

# Decrease brightness:
for i in range(0xffff, 0, -1):
    led_channel.duty_cycle = i

These for loops take a while because 16-bits is a lot of numbers. CTRL-C to stop the loop from running and return to the REPL.

Control Servos

Servo motors use a PWM signal to control the position of their arm or horn.  Using the PCA9685 and the Motor library you can easily plug in servos and control them with Python.  If you aren't familiar with servos be sure to first read this intro to servos page and this in-depth servo guide page.

First connect the servo to a channel on the PCA9685.  Be sure to plug the servo connector in the correct way!  Check your servo's datasheet to be sure, but typically the brown wire is connected to ground, the red wire is connected to 5V power, and the yellow pin is connected to PWM:

Be sure you've turned on or plugged in the external 5V power supply to the PCA9685 board too!

Now in the Python REPL as above with the led, save a variable for its channel:

Download: file
servo_channel = pca.channels[0]

Servos typically operate at a frequency of 50 hz so update pca accordingly.

Download: file
pca.frequency = 50

Now that the PCA9685 is set up for servos lets make a Servo object so that we can adjust the servo based on angle instead of duty_cycle.

By default the Servo class will use actuation range, minimum pulse-width, and maximum pulse-width values that should work for most servos.  However check the Servo class documentation for more details on extra parameters to customize the signal generated for your servos.

Download: file
import adafruit_motor.servo
servo = adafruit_motor.servo.Servo(servo_channel)

With Servo, you specify a position as an angle.  The angle will always be between 0 and the actuation range given when Servo was created.  The default is 180 degrees but your servo might have a smaller sweep--change the total angle by specifying the actuation_angle parameter in the Servo class initializer above.

Now set the angle to 180, one extreme of the range:

Download: file
servo.angle = 180

Or to sweep back to the minimum 0 degree position:

Download: file
servo.angle = 0

Often the range an individual servo recognizes varies a bit from other servos. If the servo didn't sweep the full expected range, then try adjusting min_pulse and max_pulse. Lower min_pulse until the servo stops moving or moves irregularly when angle is changed to 0. Raise max_pulse util the servo stops moving or moves irregularly when angle is change to the actuation range.

Download: file
servo = adafruit_motor.servo.Servo(servo_channel, min_pulse=800, max_pulse=2200)

That's all there is to controlling servos with the PCA9685 and CircuitPython!  Using the angle attribute you can sweep and move servos in any way.  This is perfect for building robots, actuating switches, or other fun mechanical projects!

MicroPython

The examples in this guide are no longer supported. Check out the PCA9685 driver guide for CircuitPython and Python usage: https://learn.adafruit.com/16-channel-pwm-servo-driver
Note this page describes how to use a MicroPython.org version of this library with MicroPython boards. Skip back to the previous page if you're using a CircuitPython board like the Feather M0 express!

In addition to CircuitPython there's an older MicroPython version of the PCA9685 library that you can use with some MicroPython boards.  Before you get started it will help to be familiar with these guides for working with MicroPython:

See all the MicroPython guides in the learning system for more information.

MicroPython Module Install

To use the PCA9685 with your MicroPython board you'll need to install the micropython-adafruit-pca9685 MicroPython module on your board.  Remember this module is for MicroPython.org firmware and not Adafruit CircuitPython!

First make sure you are running the latest version of MicroPython for your board.  If you're using the ESP8266 MicroPython port you must be running version 1.8.5 or higher as earlier versions do not support using .mpy modules as shown in this guide.  

Next download the latest pca9685.mpy, servo.mpy, motor.mpy, and stepper.mpy file from the releases page of the micropython-adafruit-pca9685 GitHub repository.  You'll need to copy all of the files to your MicroPython board's file system and can use a tool like ampy to copy the files to the board.

Usage

The following section will show how to control the PCA9685 from the board's Python prompt / REPL.   First connect to the board's serial REPL so you are at the MicroPython >>> prompt.

I2C Initialization

First you'll need to initialize the I2C bus for your board.  On MicroPython.org firmware which uses the machine API you can initialize I2C like the MicroPython I2C guide mentions.  For example on a board like the ESP8266 you can run (assuming you're using the default SDA gpio #4 and SCL gpio #5 pins like on a Feather & PCA9685 FeatherWing):

Download: file
import machine
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

Then import the PCA9685 module as follows:

Download: file
import pca9685

LED & Servo Control

At this point you're ready to use the PCA9685 module to dim LEDs and controls servos.  Using the module with MicroPython is exactly the same as with CircuitPython so check out the CircuitPython usage section to see details on using the board.

This guide was first published on Oct 25, 2016. It was last updated on Oct 25, 2016.