pca9685 - Driver for 16-channel, 12-bit PWM PCA9685

group pca9685

ESP-IDF driver for 16-channel, 12-bit PWM PCA9685.

Ported from esp-open-rtos

Copyright (C) 2016, 2018 Ruslan V. Uss unclerus@gmail.com

BSD Licensed as described in the file LICENSE

Defines

PCA9685_ADDR_BASE

Base I2C device address.

PCA9685_MAX_PWM_VALUE

Enums

enum pca9685_channel_t

PWM channel.

Values:

enumerator PCA9685_CHANNEL_0

PCA9685_CHANNEL_0.

enumerator PCA9685_CHANNEL_1

PCA9685_CHANNEL_1.

enumerator PCA9685_CHANNEL_2

PCA9685_CHANNEL_2.

enumerator PCA9685_CHANNEL_3

PCA9685_CHANNEL_3.

enumerator PCA9685_CHANNEL_4

PCA9685_CHANNEL_4.

enumerator PCA9685_CHANNEL_5

PCA9685_CHANNEL_5.

enumerator PCA9685_CHANNEL_6

PCA9685_CHANNEL_6.

enumerator PCA9685_CHANNEL_7

PCA9685_CHANNEL_7.

enumerator PCA9685_CHANNEL_8

PCA9685_CHANNEL_8.

enumerator PCA9685_CHANNEL_9

PCA9685_CHANNEL_9.

enumerator PCA9685_CHANNEL_10

PCA9685_CHANNEL_10.

enumerator PCA9685_CHANNEL_11

PCA9685_CHANNEL_11.

enumerator PCA9685_CHANNEL_12

PCA9685_CHANNEL_12.

enumerator PCA9685_CHANNEL_13

PCA9685_CHANNEL_13.

enumerator PCA9685_CHANNEL_14

PCA9685_CHANNEL_14.

enumerator PCA9685_CHANNEL_15

PCA9685_CHANNEL_15.

enumerator PCA9685_CHANNEL_ALL

All channels.

Functions

esp_err_t pca9685_init_desc(i2c_dev_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptor.

Parameters
  • dev – Pointer to I2C device descriptor

  • addr – PCA9685 address

  • port – I2C port number

  • sda_gpio – GPIO pin number for SDA

  • scl_gpio – GPIO pin number for SCL

Returns

ESP_OK on success

esp_err_t pca9685_free_desc(i2c_dev_t *dev)

Free device descriptor.

Parameters

dev – Pointer to I2C device descriptor

Returns

ESP_OK on success

esp_err_t pca9685_init(i2c_dev_t *dev)

Init device.

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t pca9685_set_subaddr(i2c_dev_t *dev, uint8_t num, uint8_t subaddr, bool enable)

Setup device subaddress.

See section 7.3.6 if the datasheet

Parameters
  • dev – Device descriptor

  • num – Subaddress number, 0..2

  • subaddr – Subaddress, 7 bit

  • enable – True to enable subaddress, false to disable

Returns

ESP_OK on success

esp_err_t pca9685_restart(i2c_dev_t *dev)

Restart device.

See section 7.3.1.1 of the datasheet

Parameters

dev – Device descriptor

Returns

ESP_OK on success

esp_err_t pca9685_is_sleeping(i2c_dev_t *dev, bool *sleeping)

Check if device is in sleep mode.

Parameters
  • dev – Device descriptor

  • [out] sleeping – True if device is sleeping

Returns

ESP_OK on success

esp_err_t pca9685_sleep(i2c_dev_t *dev, bool sleep)

Switch device to low-power mode or wake it up.

Parameters
  • dev – Device descriptor

  • sleep – True for sleep mode, false for wake up

Returns

ESP_OK on success

esp_err_t pca9685_is_output_inverted(i2c_dev_t *dev, bool *inv)

Get logic inversion of the outputs.

Parameters
  • dev – Device descriptor

  • [out] inv – True if outputs are inverted, false otherwise

Returns

ESP_OK on success

esp_err_t pca9685_set_output_inverted(i2c_dev_t *dev, bool inverted)

Logically invert outputs.

See section 7.7 of the datasheet

Parameters
  • dev – Device descriptor

  • inverted – True for inverted outputs

Returns

ESP_OK on success

esp_err_t pca9685_get_output_open_drain(i2c_dev_t *dev, bool *od)

Get outputs mode.

Parameters
  • dev – Device descriptor

  • [out] od – True if outputs are in open drain mode

Returns

ESP_OK on success

esp_err_t pca9685_set_output_open_drain(i2c_dev_t *dev, bool od)

Set outputs mode.

Parameters
  • dev – Device descriptor

  • od – True to set open drain mode, false to normal mode

Returns

ESP_OK on success

esp_err_t pca9685_get_prescaler(i2c_dev_t *dev, uint8_t *prescaler)

Get PWM frequency prescaler.

Parameters
  • dev – Device descriptor

  • [out] prescaler – Frequency prescaler

Returns

ESP_OK on success

esp_err_t pca9685_set_prescaler(i2c_dev_t *dev, uint8_t prescaler)

Set PWM frequency prescaler.

Parameters
  • dev – Device descriptor

  • prescaler – Prescaler value

Returns

ESP_OK on success

esp_err_t pca9685_get_pwm_frequency(i2c_dev_t *dev, uint16_t *freq)

Get PWM frequency.

Parameters
  • dev – Device descriptor

  • [out] freq – PWM frequency, Hz

Returns

ESP_OK on success

esp_err_t pca9685_set_pwm_frequency(i2c_dev_t *dev, uint16_t freq)

Set PWM frequency.

Parameters
  • dev – Device descriptor

  • freq – PWM frequency, Hz

Returns

ESP_OK on success

esp_err_t pca9685_set_pwm_value(i2c_dev_t *dev, uint8_t channel, uint16_t val)

Set PWM value on output channel.

Parameters
  • dev – Device descriptor

  • channel – Channel number, 0..15 or >15 for all channels

  • val – PWM value, 0..4096

Returns

ESP_OK on success

esp_err_t pca9685_set_pwm_values(i2c_dev_t *dev, uint8_t first_ch, uint8_t channels, const uint16_t *values)

Set PWM values on multiple output channels.

Parameters
  • dev – Device descriptor

  • first_ch – First channel, 0..15

  • channels – Number of channels to update

  • values – Array of the channel values, each 0..4096

Returns

ESP_OK on success