Overview

In this course we learn how to use the PCA9685 with the python library to control multiple servos.

Note: This tutorial assumes that users are familiar with the Raspberry Pi and have a general knowledge base of Python. If you have unfamiliar with these items, as a starting point, we suggest setting up your Raspberry Pi, familiarizing yourself with the graphics user interface (GUI), and then learning the basics of Python. Below are resources to get you started:

Preperation

Hardware

Software

Install the Adafruit PCA9685 Library on your RPi


sudo pip3 install adafruit-circuitpython-servokit

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Configure your Pi to use I2C devices

To learn more about how to setup I2C with Raspbian, please take a minor diversion to this tutoria.

When you are ready to continue, enter the following commands to add SMBus support (which includes I2C) to Python:


sudo apt-get install python-smbus
sudo apt-get install i2c-tools

i2c-tools isn’t strictly required, but it’s a useful package since you can use it to scan for any I2C or SMBus devices connected to your board. If you know something is connected, but you don’t know it’s 7-bit I2C address, this library has a great little tool to help you find it. python-smbus is required, it adds the I2C support for python!

Don’t forget you must add kernel support for I2C by following this tutorial!

You can then detect if the HAT is found on the #1 I2C port with:\


sudo i2cdetect -y 1

This will search  /dev/i2c-1 for all address, and if an Adafruit PWM/Servo HAT is properly connected and it’s set to its default address — meaning none of the 6 address solder jumpers at the top of the board have been soldered shut — it should show up at 0x40 (binary 1000000) as follows:

raspberry_pi_i2cdetect.png

Once both of these packages have been installed, and i2cdetect finds the 0x40 I2C address, you have everything you need to get started accessing I2C and SMBus devices in Python.

CONNECTING A SERVO

Most servos come with a standard 3-pin female connector that will plug directly into the headers on the Servo HAT headers. Be sure to align the plug with the ground wire (usually black or brown) with the bottom row and the signal wire (usually yellow or white) on the top.

Works with any servo that can be powered by 5V and take 3.3V logic level signals.

ADDING MORE SERVOS

Up to 16 servos can be attached to one board. If you need to control more than 16 servos, additional boards can be stacked as described on the next page.

Code Program


import time
from adafruit_servokit import ServoKit

# Set channels to the number of servo channels on your kit.
kit = ServoKit(channels=16)

kit.servo[0].angle = 180
kit.continuous_servo[1].throttle = 1
time.sleep(1)
kit.continuous_servo[1].throttle = -1
time.sleep(1)
kit.servo[0].angle = 0
kit.continuous_servo[1].throttle = 0

Save the above code to a py file and run it. You will see that this example rotates the continuous rotation servo on channel 1 forward for one second, then backward for one second, then stops the rotation; and rotates the servo on channel 0 to 180 degrees for one second, then returns to 0 degrees.

Control more Servos

In this example, we can connect a servo to each channel, set each channel to 180 and return to 0. Here we can control up to 16 servos at the same time, so you need to choose a suitable power supply.


import time
from adafruit_servokit import ServoKit

# Set channels to the number of servo channels on your kit.
kit = ServoKit(channels=16)

for i in range(len(kit.servo)): # pylint: disable=consider-using-enumerate
kit.servo[i].angle = 180
time.sleep(1)
for i in range(len(kit.servo)): # pylint: disable=consider-using-enumerate
kit.servo[i].angle = 0
time.sleep(1)

Parameters: