User Manual
Dimming LEDs
This HAT and Bonnet use the PCA9685. Each channel of the HAT and Bonnet 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.
The PCA9685 class provides control of the PWM frequency and each channel's duty cycle. Check out the PCA9685
class documentation (https://adafru.it/C5n) 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 HAT and Bonnet support 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 (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 :
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 :
import board
import busio
import adafruit_pca9685
i2c = busio.I2C(board.SCL, board.SDA)
hat = adafruit_pca9685.PCA9685(i2c)
hat.frequency = 60
led_channel = hat.channels[0]
led_channel.duty_cycle = 0xffff
led_channel.duty_cycle = 0
© Adafruit Industries https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi Page 15 of 28










