Simple test

Ensure your device works with this simple test.

examples/pca9685_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Outputs a 50% duty cycle PWM single on the 0th channel.
 5# Connect an LED and resistor in series to the pin
 6# to visualize duty cycle changes and its impact on brightness.
 7
 8import board
 9from adafruit_pca9685 import PCA9685
10
11# Create the I2C bus interface.
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = busio.I2C(board.GP1, board.GP0)    # Pi Pico RP2040
14
15# Create a simple PCA9685 class instance.
16pca = PCA9685(i2c)
17
18# Set the PWM frequency to 60hz.
19pca.frequency = 60
20
21# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
22# but the PCA9685 will only actually give 12 bits of resolution.
23pca.channels[0].duty_cycle = 0x7FFF

Calibration Example

This advanced example can be used to compute a more precise reference_clock_speed. Use an oscilloscope or logic analyzer to measure the signal frequency and type the results into the prompts.

examples/pca9685_calibration.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# This advanced example can be used to compute a more precise reference_clock_speed. Use an
 5# oscilloscope or logic analyzer to measure the signal frequency and type the results into the
 6# prompts. At the end it'll give you a more precise value around 25 mhz for your reference clock
 7# speed.
 8
 9import time
10import board
11from adafruit_pca9685 import PCA9685
12
13# Create the I2C bus interface.
14i2c = board.I2C()  # uses board.SCL and board.SDA
15# i2c = busio.I2C(board.GP1, board.GP0)    # Pi Pico RP2040
16
17# Create a simple PCA9685 class instance.
18pca = PCA9685(i2c)
19
20# Set the PWM frequency to 100hz.
21pca.frequency = 100
22
23input("Press enter when ready to measure default frequency.")
24
25# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
26# but the PCA9685 will only actually give 12 bits of resolution.
27print("Running with default calibration")
28pca.channels[0].duty_cycle = 0x7FFF
29time.sleep(1)
30pca.channels[0].duty_cycle = 0
31
32measured_frequency = float(input("Frequency measured: "))
33print()
34
35pca.reference_clock_speed = pca.reference_clock_speed * (
36    measured_frequency / pca.frequency
37)
38# Set frequency again so we can get closer. Reading it back will produce the real value.
39pca.frequency = 100
40
41input("Press enter when ready to measure coarse calibration frequency.")
42pca.channels[0].duty_cycle = 0x7FFF
43time.sleep(1)
44pca.channels[0].duty_cycle = 0
45measured_after_calibration = float(input("Frequency measured: "))
46print()
47
48reference_clock_speed = measured_after_calibration * 4096 * pca.prescale_reg
49
50print("Real reference clock speed: {0:.0f}".format(reference_clock_speed))

Servo Example

Example to show the library using a servo

examples/pca9685_servo.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6from adafruit_motor import servo
 7from adafruit_pca9685 import PCA9685
 8
 9i2c = board.I2C()  # uses board.SCL and board.SDA
10# i2c = busio.I2C(board.GP1, board.GP0)    # Pi Pico RP2040
11
12# Create a simple PCA9685 class instance.
13pca = PCA9685(i2c)
14# You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
15# timing pulses. This calibration will be specific to each board and its environment. See the
16# calibration.py example in the PCA9685 driver.
17# pca = PCA9685(i2c, reference_clock_speed=25630710)
18pca.frequency = 50
19
20# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
21# match the stall points of the servo.
22# This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
23# servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2350)
24# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
25#   https://www.adafruit.com/product/2307
26# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600)
27# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
28#   https://www.adafruit.com/product/155
29# servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400)
30# This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
31# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2500)
32# This is an example for the Micro servo - TowerPro SG-92R: https://www.adafruit.com/product/169
33# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2400)
34
35# The pulse range is 750 - 2250 by default. This range typically gives 135 degrees of
36# range, but the default is to use 180 degrees. You can specify the expected range if you wish:
37# servo7 = servo.Servo(pca.channels[7], actuation_range=135)
38servo7 = servo.Servo(pca.channels[7])
39
40# We sleep in the loops to give the servo time to move into position.
41for i in range(180):
42    servo7.angle = i
43    time.sleep(0.03)
44for i in range(180):
45    servo7.angle = 180 - i
46    time.sleep(0.03)
47
48# You can also specify the movement fractionally.
49fraction = 0.0
50while fraction < 1.0:
51    servo7.fraction = fraction
52    fraction += 0.01
53    time.sleep(0.03)
54
55pca.deinit()