follow on Twitter

SparkFun Servo pHAT for the Raspberry Pi

Python module for the PCA9685 PWM controller, which is part of the SparkFun Servo pHAT for Raspberry Pi and SparkFun Pi Servo HAT

This package should be used in conjunction with the overall SparkFun qwiic Python Package. New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.


Supported Platforms

The qwiic PCA9685 Python package current supports the following platforms:


This package depends on the qwiic I2C driver: Qwiic_I2C_Py


The SparkFun qwiic PCA9685 module documentation is hosted at ReadTheDocs


PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-pca9685 package. On systems that support PyPi installation via pip, this library is installed using the following commands

For all users (note: the user must have sudo privileges):

sudo pip install sparkfun-qwiic-pca9685

For the current user:

pip install sparkfun-qwiic-pca9685

Local Installation

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line:

python setup.py install

To build a package for use with pip:

python setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.

cd dist
pip install sparkfun_qwiic_pca9685-<version>.tar.gz

Example Use

See the examples directory for more detailed use examples.

import qwiic_pca9685
import time
import math
import sys

def runExample():

    print("\nSparkFun PCA9685 Example 1\n")
    mySensor = qwiic_pca9685.QwiicPCA9685()

    if mySensor.isConnected() == False:
        print("The Qwiic PCA9685 device isn't connected to the system. Please check your connection", \


    # Sets PWM Frequency to 50 Hz

    # Sets start time of PWM pulse on Channel 0 to 0s
    mySensor.set_channel_word(0, 1, 0)

    while True:
        # Increments start time of PWM pulse on Channel 0 to i (1ms to 2ms)
        for i in range(205, 410):
            fun.set_channel_word(0, 0, i)
            # Delay .05 s

        # Decrements start time of PWM pulse on Channel 0 to i (2ms to 1ms)
        for i in range(410, 205, -1):
            fun.set_channel_word(0, 0, i)
            # Delay .05 s


SparkFun - Start Something

Table of Contents