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# This simple test outputs a 50% duty cycle PWM single on the 0th channel. Connect an LED and
 5# resistor in series to the pin to visualize duty cycle changes and its impact on brightness.
 6
 7from board import SCL, SDA
 8import busio
 9
10# Import the PCA9685 module.
11from adafruit_pca9685 import PCA9685
12
13# Create the I2C bus interface.
14i2c_bus = busio.I2C(SCL, SDA)
15
16# Create a simple PCA9685 class instance.
17pca = PCA9685(i2c_bus)
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
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
11from board import SCL, SDA
12import busio
13
14# Import the PCA9685 module.
15from adafruit_pca9685 import PCA9685
16
17# Create the I2C bus interface.
18i2c_bus = busio.I2C(SCL, SDA)
19
20# Create a simple PCA9685 class instance.
21pca = PCA9685(i2c_bus)
22
23# Set the PWM frequency to 100hz.
24pca.frequency = 100
25
26input("Press enter when ready to measure default frequency.")
27
28# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
29# but the PCA9685 will only actually give 12 bits of resolution.
30print("Running with default calibration")
31pca.channels[0].duty_cycle = 0x7FFF
32time.sleep(1)
33pca.channels[0].duty_cycle = 0
34
35measured_frequency = float(input("Frequency measured: "))
36print()
37
38pca.reference_clock_speed = pca.reference_clock_speed * (
39    measured_frequency / pca.frequency
40)
41# Set frequency again so we can get closer. Reading it back will produce the real value.
42pca.frequency = 100
43
44input("Press enter when ready to measure coarse calibration frequency.")
45pca.channels[0].duty_cycle = 0x7FFF
46time.sleep(1)
47pca.channels[0].duty_cycle = 0
48measured_after_calibration = float(input("Frequency measured: "))
49print()
50
51reference_clock_speed = measured_after_calibration * 4096 * pca.prescale_reg
52
53print("Real reference clock speed: {0:.0f}".format(reference_clock_speed))
examples/pca9685_servo.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6from board import SCL, SDA
 7import busio
 8
 9# Import the PCA9685 module. Available in the bundle and here:
10#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
11from adafruit_motor import servo
12from adafruit_pca9685 import PCA9685
13
14i2c = busio.I2C(SCL, SDA)
15
16# Create a simple PCA9685 class instance.
17pca = PCA9685(i2c)
18# You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
19# timing pulses. This calibration will be specific to each board and its environment. See the
20# calibration.py example in the PCA9685 driver.
21# pca = PCA9685(i2c, reference_clock_speed=25630710)
22pca.frequency = 50
23
24# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
25# match the stall points of the servo.
26# This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
27# servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2350)
28# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
29#   https://www.adafruit.com/product/2307
30# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600)
31# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
32#   https://www.adafruit.com/product/155
33# servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400)
34# This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
35# servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2500)
36# This is an example for the Micro servo - TowerPro SG-92R: https://www.adafruit.com/product/169
37# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2400)
38
39# The pulse range is 750 - 2250 by default. This range typically gives 135 degrees of
40# range, but the default is to use 180 degrees. You can specify the expected range if you wish:
41# servo7 = servo.Servo(pca.channels[7], actuation_range=135)
42servo7 = servo.Servo(pca.channels[7])
43
44# We sleep in the loops to give the servo time to move into position.
45for i in range(180):
46    servo7.angle = i
47    time.sleep(0.03)
48for i in range(180):
49    servo7.angle = 180 - i
50    time.sleep(0.03)
51
52# You can also specify the movement fractionally.
53fraction = 0.0
54while fraction < 1.0:
55    servo7.fraction = fraction
56    fraction += 0.01
57    time.sleep(0.03)
58
59pca.deinit()