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
 9
10from adafruit_pca9685 import PCA9685
11
12# Create the I2C bus interface.
13i2c = board.I2C()  # uses board.SCL and board.SDA
14# i2c = busio.I2C(board.GP1, board.GP0)    # Pi Pico RP2040
15
16# Create a simple PCA9685 class instance.
17pca = PCA9685(i2c)
18
19# Set the PWM frequency to 60hz.
20pca.frequency = 60
21
22# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
23# but the PCA9685 will only actually give 12 bits of resolution.
24pca.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
10
11import board
12
13from adafruit_pca9685 import PCA9685
14
15# Create the I2C bus interface.
16i2c = board.I2C()  # uses board.SCL and board.SDA
17# i2c = busio.I2C(board.GP1, board.GP0)    # Pi Pico RP2040
18
19# Create a simple PCA9685 class instance.
20pca = PCA9685(i2c)
21
22# Set the PWM frequency to 100hz.
23pca.frequency = 100
24
25input("Press enter when ready to measure default frequency.")
26
27# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
28# but the PCA9685 will only actually give 12 bits of resolution.
29print("Running with default calibration")
30pca.channels[0].duty_cycle = 0x7FFF
31time.sleep(1)
32pca.channels[0].duty_cycle = 0
33
34measured_frequency = float(input("Frequency measured: "))
35print()
36
37pca.reference_clock_speed *= measured_frequency / pca.frequency
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(f"Real reference clock speed: {reference_clock_speed:.0f}")

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