import pca9685
The pca9685 module controls a PCA9685 over I2C. It provides 16 independent 12-bit PWM outputs and is commonly used for servo banks, LED dimming, motor driver PWM inputs, and other low-frequency PWM expansion.
Quick example
import pca9685 driver = pca9685.PCA9685(sda=21, scl=22) driver.set_servo(0, 90) driver.set_servo(1, 30, min_us=1000, max_us=2000) leds = pca9685.PCA9685(sda=21, scl=22, address=0x41, pwm_frequency=1000) leds.set_duty(0, 0.25) leds.set_value(1, 2048)
importpca9685
PCA9685 16-channel PWM driver module.
The pca9685 module controls a PCA9685 over I2C. It provides 16 independent 12-bit PWM outputs and is commonly used for servo banks, LED dimming, motor driver PWM inputs, and other low-frequency PWM expansion.
pca9685.PCA9685(sda: int, scl: int, address: int = None, i2c_frequency: any = None, pwm_frequency: any = None, port: int = None, oscillator_hz: any = None, open_drain: bool = None, invert: bool = None) -> pca9685_controller
pca9685.PCA9685(sda: int, scl: int, address: int = None, i2c_frequency: any = None, pwm_frequency: any = None, port: int = None, oscillator_hz: any = None, open_drain: bool = None, invert: bool = None) -> pca9685_controllerCreate and initialize an independent PCA9685 PWM controller.
Use one PCA9685 object for each physical controller. Multiple controllers can share one I2C bus when each board has a unique address.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO connected to I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO connected to I2C SCL. |
address |
int |
positional or keyword | No | Optional 7-bit I2C address. Defaults to 0x40. |
i2c_frequency |
any |
positional or keyword | No | Optional I2C bus frequency in hertz. Defaults to 400000. |
pwm_frequency |
any |
positional or keyword | No | Optional PWM output frequency in hertz. Defaults to 50, which is typical for hobby servos. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port. Defaults to 0. |
oscillator_hz |
any |
positional or keyword | No | Optional PCA9685 oscillator frequency. Defaults to 25000000. |
open_drain |
bool |
positional or keyword | No | Optional output driver mode. Defaults to False. |
invert |
bool |
positional or keyword | No | Optional output polarity inversion. Defaults to False. |
pca9685_controller pca9685_controller object.
pca9685.PCA9685().set_pwm_frequency(frequency, oscillator_hz=None) -> bool
pca9685.PCA9685().set_pwm_frequency(frequency, oscillator_hz=None) -> boolSet the shared PWM frequency for all channels on this controller.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
frequency |
int |
positional or keyword | Yes | Required value. |
oscillator_hz |
any |
positional or keyword | No | Optional value. Defaults to None. |
pca9685.PCA9685().set_pwm(channel, on, off) -> bool
pca9685.PCA9685().set_pwm(channel, on, off) -> boolWrite raw 12-bit on/off ticks for one channel.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
on |
any |
positional or keyword | Yes | Required value. |
off |
any |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().set_all_pwm(on, off) -> bool
pca9685.PCA9685().set_all_pwm(on, off) -> boolWrite raw on/off ticks to all channels.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
on |
any |
positional or keyword | Yes | Required value. |
off |
any |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().set_duty(channel, duty) -> bool
pca9685.PCA9685().set_duty(channel, duty) -> boolSet one channel by normalized duty cycle from 0.0 through 1.0.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
duty |
any |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().set_value(channel, value) -> bool
pca9685.PCA9685().set_value(channel, value) -> boolSet one channel by raw 12-bit value from 0 through 4095.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
value |
any |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().set_pulse_us(channel, pulse_us) -> bool
pca9685.PCA9685().set_pulse_us(channel, pulse_us) -> boolSet one channel by pulse width in microseconds.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
pulse_us |
int |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().set_servo(channel, angle, min_us=None, max_us=None, min_angle=None, max_angle=None) -> bool
pca9685.PCA9685().set_servo(channel, angle, min_us=None, max_us=None, min_angle=None, max_angle=None) -> boolSet a servo-style channel by angle.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
angle |
float |
positional or keyword | Yes | Required value. |
min_us |
int |
positional or keyword | No | Optional value. Defaults to None. |
max_us |
int |
positional or keyword | No | Optional value. Defaults to None. |
min_angle |
any |
positional or keyword | No | Optional value. Defaults to None. |
max_angle |
any |
positional or keyword | No | Optional value. Defaults to None. |
pca9685.PCA9685().full_on(channel) -> bool
pca9685.PCA9685().full_on(channel) -> boolForce one channel fully on.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().full_off(channel) -> bool
pca9685.PCA9685().full_off(channel) -> boolForce one channel fully off.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().all_off() -> bool
pca9685.PCA9685().all_off() -> boolForce every channel fully off.
pca9685.PCA9685().sleep(enabled=None) -> bool
pca9685.PCA9685().sleep(enabled=None) -> boolEnter or leave sleep mode.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
enabled |
bool |
positional or keyword | No | Optional value. Defaults to None. |
pca9685.PCA9685().restart() -> bool
pca9685.PCA9685().restart() -> boolRestart PWM output after sleep or frequency changes.
pca9685.PCA9685().read_pwm(channel) -> dict
pca9685.PCA9685().read_pwm(channel) -> dictRead one channel's PWM registers.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | Required value. |
pca9685.PCA9685().info() -> dict
pca9685.PCA9685().info() -> dictReturn controller configuration and register state.
pca9685.PCA9685().deinit() -> bool
pca9685.PCA9685().deinit() -> boolRelease this PCA9685 I2C device and bus.
pca9685.PCA9685().close() -> bool
pca9685.PCA9685().close() -> boolRelease this PCA9685 handle.
pca9685.PCA9685().Example
pca9685.PCA9685().Exampledriver = pca9685.PCA9685(sda=21, scl=22) driver.set_servo(0, 90)
pca9685.begin(sda: int, scl: int, address: int = None, i2c_frequency: any = None, pwm_frequency: any = None, port: int = None, oscillator_hz: any = None, open_drain: bool = None, invert: bool = None) -> bool
pca9685.begin(sda: int, scl: int, address: int = None, i2c_frequency: any = None, pwm_frequency: any = None, port: int = None, oscillator_hz: any = None, open_drain: bool = None, invert: bool = None) -> boolLegacy: initialize the module-level default PCA9685 controller.
Prefer ``driver = pca9685.PCA9685(...)`` for new projects. The legacy wrapper keeps older singleton-style scripts working.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
sda |
int |
positional or keyword | Yes | GPIO connected to I2C SDA. |
scl |
int |
positional or keyword | Yes | GPIO connected to I2C SCL. |
address |
int |
positional or keyword | No | Optional 7-bit I2C address. Defaults to 0x40. |
i2c_frequency |
any |
positional or keyword | No | Optional I2C bus frequency in hertz. Defaults to 400000. |
pwm_frequency |
any |
positional or keyword | No | Optional PWM output frequency in hertz. Defaults to 50, which is typical for hobby servos. |
port |
int |
positional or keyword | No | Optional ESP-IDF I2C port. Defaults to 0. |
oscillator_hz |
any |
positional or keyword | No | Optional PCA9685 oscillator frequency. Defaults to 25000000. |
open_drain |
bool |
positional or keyword | No | Optional output driver mode. Defaults to False, which uses totem-pole output mode. |
invert |
bool |
positional or keyword | No | Optional output polarity inversion. Defaults to False. |
bool True when the controller is configured.
pca9685.set_pwm_frequency(frequency: int, oscillator_hz: float = None) -> bool
pca9685.set_pwm_frequency(frequency: int, oscillator_hz: float = None) -> boolSet the shared PWM frequency for all channels.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
frequency |
int |
positional or keyword | Yes | Output PWM frequency in hertz. Valid range is about 24 through 1526. |
oscillator_hz |
float |
positional or keyword | No | Optional oscillator frequency used for prescale math. Defaults to the current oscillator setting. |
bool True when the PRE_SCALE register was updated.
pca9685.set_pwm(channel: int, on: any, off: any) -> bool
pca9685.set_pwm(channel: int, on: any, off: any) -> boolWrite raw 12-bit on/off ticks for one channel.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
on |
any |
positional or keyword | Yes | Tick where the output turns on. Values are clamped to 0 through 4096. |
off |
any |
positional or keyword | Yes | Tick where the output turns off. Values are clamped to 0 through 4096. |
bool True when the channel registers were written.
pca9685.set_all_pwm(on: any, off: any) -> bool
pca9685.set_all_pwm(on: any, off: any) -> boolWrite raw on/off ticks to all channels.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
on |
any |
positional or keyword | Yes | Tick where all outputs turn on. |
off |
any |
positional or keyword | Yes | Tick where all outputs turn off. |
bool True when the all-channel registers were written.
pca9685.set_duty(channel: int, duty: any) -> bool
pca9685.set_duty(channel: int, duty: any) -> boolSet one channel by normalized duty cycle.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
duty |
any |
positional or keyword | Yes | Duty cycle from 0.0 through 1.0. |
bool True when the channel output was updated.
pca9685.set_value(channel: int, value: any) -> bool
pca9685.set_value(channel: int, value: any) -> boolSet one channel by raw 12-bit PWM value.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
value |
any |
positional or keyword | Yes | PWM value from 0 through 4095. |
bool True when the channel output was updated.
pca9685.set_pulse_us(channel: int, pulse_us: int) -> bool
pca9685.set_pulse_us(channel: int, pulse_us: int) -> boolSet one channel by pulse width in microseconds.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
pulse_us |
int |
positional or keyword | Yes | Pulse width in microseconds. |
bool True when the pulse was converted to ticks and written.
pca9685.set_servo(channel: int, angle: float, min_us: int = None, max_us: int = None, min_angle: any = None, max_angle: any = None) -> bool
pca9685.set_servo(channel: int, angle: float, min_us: int = None, max_us: int = None, min_angle: any = None, max_angle: any = None) -> boolSet a servo-style channel by angle.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
angle |
float |
positional or keyword | Yes | Requested angle in degrees. |
min_us |
int |
positional or keyword | No | Optional pulse width for min_angle. Defaults to 500. |
max_us |
int |
positional or keyword | No | Optional pulse width for max_angle. Defaults to 2500. |
min_angle |
any |
positional or keyword | No | Optional minimum angle. Defaults to 0. |
max_angle |
any |
positional or keyword | No | Optional maximum angle. Defaults to 180. |
bool True when the servo pulse was written.
pca9685.full_on(channel: int) -> bool
pca9685.full_on(channel: int) -> boolForce one channel fully on.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
bool True when the full-on bit was written.
pca9685.full_off(channel: int) -> bool
pca9685.full_off(channel: int) -> boolForce one channel fully off.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
bool True when the full-off bit was written.
pca9685.all_off() -> bool
pca9685.all_off() -> boolForce every channel fully off.
bool True when all channels were disabled.
pca9685.sleep(enabled: bool = None) -> bool
pca9685.sleep(enabled: bool = None) -> boolEnter or leave PCA9685 sleep mode.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
enabled |
bool |
positional or keyword | No | Optional boolean. Defaults to True. |
bool True when MODE1 was updated.
pca9685.restart() -> bool
pca9685.restart() -> boolRestart PWM output after sleep or frequency changes.
bool True when the restart bit was written.
pca9685.read_pwm(channel: int) -> dict
pca9685.read_pwm(channel: int) -> dictRead one channel's PWM registers.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | Yes | PCA9685 channel from 0 through 15. |
dict Dict with ok, channel, on, off, duty, full_on, and full_off fields.
pca9685.info() -> dict
pca9685.info() -> dictReturn PCA9685 configuration and register state.
dict Dict with initialized, address, sda, scl, i2c_frequency, pwm_frequency, oscillator_hz, channels, mode1, mode2, and prescale.
pca9685.deinit() -> bool
pca9685.deinit() -> boolRelease the PCA9685 I2C device and bus.
bool True when resources were released.