Simple test¶
Ensure your device works with this simple test.
from machine import Pin, I2C
from micropython_pca9685 import PCA9685
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
pca = PCA9685(i2c)
pca.frequency = 60
# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
# but the PCA9685 will only actually give 12 bits of resolution.
pca.channels[0].duty_cycle = 0x7FFF
Servo test¶
Servo test
import time
from machine import Pin, I2C
from micropython_pca9685 import PCA9685
from micropython_pca9685 import Servo
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
pca = PCA9685(i2c)
servo7 = Servo(pca.channels[7])
pca.frequency = 50
# We sleep in the loops to give the servo time to move into position.
for i in range(180):
servo7.angle = i
time.sleep(0.03)
for i in range(180):
servo7.angle = 180 - i
time.sleep(0.03)
# You can also specify the movement fractionally.
fraction = 0.0
while fraction < 1.0:
servo7.fraction = fraction
fraction += 0.01
time.sleep(0.06)
pca.deinit()