The Adafruit PWM/Servo Driver is the perfect solution for any project that requires a lot of servos.
Page last edited October 10, 2012
Text editor powered by tinymce.
Page last edited October 10, 2012
Text editor powered by tinymce.
There are two sets of control input pins on either side. Both sides of the pins are identical! Use whichever side you like, you can also easily chain by connecting up two side-by-side
There are 16 output ports. Each port has 3 pins: V+, GND and the PWM output. Each PWM runs completely independently but they must all have the same PWM frequency. That is, for LEDs you probably want 1.0 KHz but servos need 60 Hz - so you cannot use half for LEDs @ 1.0 KHz and half @ 60 Hz.
They're set up for servos but you can use them for LEDs! Max current per pin is 25mA.
There are 220 ohm resistors in series with all PWM Pins and the output logic is the same as VCC so keep that in mind if using LEDs.
Page last edited October 10, 2012
Text editor powered by tinymce.
Page last edited October 10, 2012
Text editor powered by tinymce.
Most servos are designed to run on about 5 or 6v. Keep in mind that a lot of servos moving at the same time (particularly large powerful ones) will need a lot of current. Even micro servos will draw several hundred mA when moving. Some High-torque servos will draw more than 1A each under load.
Good power choices are:
Page last edited October 10, 2012
Text editor powered by tinymce.
Board 0: Address = 0x40 Offset = binary 00000 (no jumpers required)
Board 1: Address = 0x41 Offset = binary 00001 (bridge A0 as in the photo above)
Board 2: Address = 0x42 Offset = binary 00010 (bridge A1)
Board 3: Address = 0x43 Offset = binary 00011 (bridge A0 & A1)
Board 4: Address = 0x44 Offset = binary 00100 (bridge A2)
etc.
In your sketch, you'll need to declare a separate pobject for each board. Call begin on each object, and control each servo through the object it's attached to. For example:
#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40); Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41); void setup() { Serial.begin(9600); Serial.println("16 channel PWM test!"); pwm1.begin(); pwm1.setPWMFreq(1600); // This is the maximum PWM frequency pwm2.begin(); pwm2.setPWMFreq(1600); // This is the maximum PWM frequency }
#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40); Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41); void setup() { Serial.begin(9600); Serial.println("16 channel PWM test!"); pwm1.begin(); pwm1.setPWMFreq(1600); // This is the maximum PWM frequency pwm2.begin(); pwm2.setPWMFreq(1600); // This is the maximum PWM frequency }
Page last edited October 10, 2012
Text editor powered by tinymce.
Since the PWM Servo Driver is controlled over I2C, its super easy to use with any microcontroller or microcomputer. In this demo we'll show using it with the Arduino IDE but the C++ code can be ported easily
To begin reading sensor data, you will need to install the Adafruit_PWMServo library (code on our github repository). It is available from the Arduino library manager so we recommend using that.
From the IDE open up the library manager...
And type in adafruit pwm to locate the library. Click Install
We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
First make sure all copies of the Arduino IDE are closed.
Next open the Arduino IDE and select File->Examples->Adafruit_PWMServoDriver->Servo. This will open the example file in an IDE window.
Connect the driver board and servo as shown on the previous page. Don't forget to provide power to both Vin (3-5V logic level) and V+ (5V servo power). Check the green LED is lit!
Plug the shield into your Arduino. Don't forget you will also have to provide 5V to the V+ terminal block. Both red and green LEDs must be lit.
Plug the FeatherWing into your Feather. Don't forget you will also have to provide 5V to the V+ terminal block. Check the green LED is lit!
A single servo should be plugged into the PWM #0 port, the first port. You should see the servo sweep back and forth over approximately 180 degrees.
Servo pulse timing varies between different brands and models. Since it is an analog control circuit, there is often some variation between samples of the same brand and model. For precise position control, you will want to calibrate the minumum and maximum pulse-widths in your code to match known positions of the servo.
Find the Minimum:
Using the example code, edit SERVOMIN until the low-point of the sweep reaches the minimum range of travel. It is best to approach this gradually and stop before the physical limit of travel is reached.
Find the Maximum:
Again using the example code, edit SERVOMAX until the high-point of the sweep reaches the maximum range of travel. Again, is best to approach this gradually and stop before the physical limit of travel is reached.
pulselength = map(degrees, 0, 180, SERVOMIN, SERVOMAX);
pulselength = map(degrees, 0, 180, SERVOMIN, SERVOMAX);
Page last edited October 10, 2012
Text editor powered by tinymce.
This function can be used to adjust the PWM frequency, which determines how many full 'pulses' per second are generated by the IC. Stated differently, the frequency determines how 'long' each pulse is in duration from start to finish, taking into account both the high and low segments of the pulse.
Frequency is important in PWM, since setting the frequency too high with a very small duty cycle can cause problems, since the 'rise time' of the signal (the time it takes to go from 0V to VCC) may be longer than the time the signal is active, and the PWM output will appear smoothed out and may not even reach VCC, potentially causing a number of problems.
The following code will set the PWM frequency to 1000Hz:
This function sets the start (on) and end (off) of the high segment of the PWM pulse on a specific channel. You specify the 'tick' value between 0..4095 when the signal will turn on, and when it will turn off. Channel indicates which of the 16 PWM outputs should be updated with the new values.
The following example will cause channel 15 to start low, go high around 25% into the pulse (tick 1024 out of 4096), transition back to low 75% into the pulse (tick 3072), and remain low for the last 25% of the pulse:
Page last edited October 10, 2012
Text editor powered by tinymce.
It's easy to use the PCA9685 driver with Python or CircuitPython and the Adafruit CircuitPython PCA9685 module. This module allows you to easily write Python code that control servos and PWM with this breakout.
You can use this driver board with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
First wire up a PCA9685 to your board exactly as shown on the previous pages for Arduino. Here's an example of wiring a Feather M0 to the driver board with I2C:
Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired with I2C:
You'll need to install the Adafruit CircuitPython PCA9685 library on your CircuitPython board.
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--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. Our CircuitPython starter guide has a great page on how to install the library bundle.
For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:
Before continuing make sure your board's lib folder or root filesystem has the adafruit_pca9685.mpy, adafruit_register, adafruit_servokit.mpy, adafruit_motor and adafruit_bus_device files and folders copied over.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
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 commands:
sudo pip3 install adafruit-circuitpython-pca9685
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!
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.
Run the following code to import the necessary modules and initialize the I2C connection with the driver board:
import board import busio import adafruit_pca9685 i2c = busio.I2C(board.SCL, board.SDA) pca = adafruit_pca9685.PCA9685(i2c)
import board import busio import adafruit_pca9685 i2c = busio.I2C(board.SCL, board.SDA) pca = adafruit_pca9685.PCA9685(i2c)
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:
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:
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.
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:
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:
Try an in-between value like 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:
# 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
# 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.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Outputs a 50% duty cycle PWM single on the 0th channel. # Connect an LED and resistor in series to the pin # to visualize duty cycle changes and its impact on brightness. import board from adafruit_pca9685 import PCA9685 # Create the I2C bus interface. i2c = board.I2C() # uses board.SCL and board.SDA # i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040 # Create a simple PCA9685 class instance. pca = PCA9685(i2c) # Set the PWM frequency to 60hz. pca.frequency = 60 # Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects # but the PCA9685 will only actually give 12 bits of resolution. pca.channels[0].duty_cycle = 0x7FFF
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Outputs a 50% duty cycle PWM single on the 0th channel. # Connect an LED and resistor in series to the pin # to visualize duty cycle changes and its impact on brightness. import board from adafruit_pca9685 import PCA9685 # Create the I2C bus interface. i2c = board.I2C() # uses board.SCL and board.SDA # i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040 # Create a simple PCA9685 class instance. pca = PCA9685(i2c) # Set the PWM frequency to 60hz. pca.frequency = 60 # Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects # but the PCA9685 will only actually give 12 bits of resolution. pca.channels[0].duty_cycle = 0x7FFF
We've written a handy CircuitPython library for the various PWM/Servo kits 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-channel breakout.
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 channel 0 on the PCA9685. Here is an example of a servo connected to channel 0:
Check your servo data sheet to verify how to connect it!
Be sure you've turned on or plugged in the external 5V power supply to the PCA9685 board too!
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
.
from adafruit_servokit import ServoKit kit = ServoKit(channels=16)
from adafruit_servokit import ServoKit kit = ServoKit(channels=16)
Now you're ready to control both standard and continuous rotation 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.
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.
import adafruit_motor.servo servo = adafruit_motor.servo.Servo(servo_channel)
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:
To return the servo to 0
degrees:
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:
servokit.servo[0].actuation_range = 160
servokit.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:
kit.servo[0].set_pulse_width_range(1000, 2000)
kit.servo[0].set_pulse_width_range(1000, 2000)
That's all there is to controlling standard servos with the PCA9685 breakout, Python and ServoKit
!
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:
To start the continuous rotation servo connected to channel 1
to full reverse throttle:
To set half throttle, use a decimal:
kit.continuous_servo[1].throttle = 0.5
kit.continuous_servo[1].throttle = 0.5
And, to stop continuous rotation servo movement set throttle
to 0
:
That's all there is to controlling continuous rotation servos with the the PCA9685 16-channel breakout, Python and ServoKit
!
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """Simple test for a standard servo on channel 0 and a continuous rotation servo on channel 1.""" import time from adafruit_servokit import ServoKit # Set channels to the number of servo channels on your kit. # 8 for FeatherWing, 16 for Shield/HAT/Bonnet. kit = ServoKit(channels=8) 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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """Simple test for a standard servo on channel 0 and a continuous rotation servo on channel 1.""" import time from adafruit_servokit import ServoKit # Set channels to the number of servo channels on your kit. # 8 for FeatherWing, 16 for Shield/HAT/Bonnet. kit = ServoKit(channels=8) 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
Page last edited October 10, 2012
Text editor powered by tinymce.
Holes are 2.5mm diameter
Page last edited October 10, 2012
Text editor powered by tinymce.
The PCA9865 chip has an "All Call" address of 0x70. This is in addition to the configured address. Set the backpacks to address 0x71 or anything other than the default 0x70 to make the issue go away.
Page last edited October 10, 2012
Text editor powered by tinymce.