It's easy to control PWM or servos with the Adafruit 8-Channel PWM or Servo FeatherWing. 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 PWM and servo motors.
CircuitPython Microcontroller Wiring
First assemble the FeatherWing exactly as shown in the previous pages. There's no wiring needed to connect the FeatherWing to the Feather. The example below shows the FeatherWing attached to a Feather using the stacking assembly method, and wiring a barrel jack to the power terminal to attach an appropriate external power source to the FeatherWing. The FeatherWing will not power servos without an external power source!
- Connect the positive side of the power terminal to the positive side of the barrel jack.
- Connect the negative side of the power terminal to the negative side of the barrel jack.
To dim an LED, wire it to the board as follows. Note: you don't need to use a resistor to limit current through the LED as the FeatherWing will limit the current to around 10mA.
- Connect LED cathode / shorter leg to FeatherWing channel GND / ground.
- Connect LED anode / longer leg to FeatherWing channel PWM.
External power is not necessary to PWM an LED.
To control a servo, wire it to the board as follows.
Check your servo datasheet to be certain.
Typically:
- Connect servo brown wire to the FeatherWing channel GND
- Connect servo red wire to the FeatherWing channel V+
- Connect servo yellow wire to the FeatherWing channel PWM
- Connect the positive side of the power terminal to the positive side of the barrel jack.
- Connect the negative side of the power terminal to the negative side of the barrel jack.
You must have an external power source to run servos!
CircuitPython Installation of ServoKit and Necessary Libraries
You'll need to install a few libraries on your Feather 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.
If you choose, you can manually install the libraries individually on your board:
- adafruit_pca9685
- adafruit_bus_device
- adafruit_register
- adafruit_motor
- adafruit_servokit
Before continuing make sure your board's lib folder or root filesystem has the adafruit_pca9685.mpy, adafruit_register, adafruit_motor, adafruit_bus_device and adafruit_servokit files and folders copied over.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
CircuitPython Usage
To demonstrate the usage, we'll use Python code to control PWM to dim an LED and to control servo motors from the board's Python REPL.
Dimming LEDs
This FeatherWing uses the PCA9685. Each channel of the FeatherWing 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 you'll need to import the necessary modules, initialize the I2C bus for your board, and create an instance of the class.
import board import busio import adafruit_pca9685 i2c = busio.I2C(board.SCL, board.SDA) wing = 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:
wing.frequency = 60
The FeatherWing supports 8 separate channels that share a frequency but can have independent duty cycles. That way you could dim 8 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
.
led_channel = wing.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
(65535), 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
:
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
:
led_channel.duty_cycle = 0
Try an in-between value like 1000
:
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:
# 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.
That's all there is to dimming LEDs using CircuitPython and the PWM/Servo FeatherWing!
Controlling Servos
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 PWM/Servo FeatherWing.
First you'll need to import and initialize the ServoKit
class. You must specify the number of channels available on your board. The FeatherWing has 8 channels, so when you create the class object, you will specify 8
.
from adafruit_servokit import ServoKit kit = ServoKit(channels=8)
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:
kit.servo[0].angle = 180
To return the servo to 0
degrees:
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:
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:
kit.servo[0].set_pulse_width_range(1000, 2000)
That's all there is to controlling standard servos with the PWM/Servo FeatherWing, CircuitPython 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:
kit.continuous_servo[1].throttle = 1
To start the continuous rotation servo connected to channel 1
to full reverse throttle:
kit.continuous_servo[1].throttle = -1
To set half throttle, use a decimal:
kit.continuous_servo[1].throttle = 0.5
And, to stop continuous rotation servo movement set throttle
to 0
:
kit.continuous_servo[1].throttle = 0
That's all there is to controlling continuous rotation servos with the PWM/Servo FeatherWing, CircuitPython 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
Page last edited January 21, 2025
Text editor powered by tinymce.