[][src]Crate pwm_pca9685

This is a platform agnostic Rust driver for the PCA9685 PWM/Servo/LED controller, based on the embedded-hal traits.

This driver allows you to:

The device

This device is an I2C-bus controlled 16-channel, 12-bit PWM controller. Its outputs can be used to control servo motors or LEDs, for example.

Each channel output has its own 12-bit resolution (4096 steps) fixed frequency individual PWM controller that operates at a programmable frequency from a typical of 24 Hz to 1526 Hz with a duty cycle that is adjustable from 0% to 100%. All outputs are set to the same PWM frequency.

Each channel output can be off or on (no PWM control), or set at its individual PWM controller value. The output driver is programmed to be either open-drain with a 25 mA current sink capability at 5 V or totem pole with a 25 mA sink, 10 mA source capability at 5 V. The PCA9685 operates with a supply voltage range of 2.3 V to 5.5 V and the inputs and outputs are 5.5 V tolerant. LEDs can be directly connected to the outputs (up to 25 mA, 5.5 V) or controlled with external drivers and a minimum amount of discrete components for larger current, higher voltage LEDs, etc. It is optimized to be used as an LED controller for Red/Green/Blue/Amber (RGBA) color backlighting applications.

Datasheet:

Usage examples (see also examples folder)

To use this driver, import this crate and an embedded_hal implementation, then instantiate the appropriate device.

Please find additional examples in this repository: [pca9685-examples] [pca9685-examples]: https://github.com/eldruin/pca9685-examples

Create a driver instance

extern crate linux_embedded_hal as hal;
extern crate pwm_pca9685 as pca9685;
use pca9685::{ Pca9685, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let pwm = Pca9685::new(dev, address);
// do something...

// get the I2C device back
let dev = pwm.destroy();

Create a driver instance for the PCA9685 with an alternative address

extern crate linux_embedded_hal as hal;
extern crate pwm_pca9685 as pca9685;
use pca9685::{ Pca9685, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let (a5, a4, a3, a2, a1, a0) = (false, true, false, true, true, false);
let address = SlaveAddr::Alternative(a5, a4, a3, a2, a1, a0);
let pwm = Pca9685::new(dev, address);

Set the PWM frequency and channel duty cycles

  • Set a PWM frequency of 60 Hz (corresponds to a value of 100 for the prescale).
  • Set a duty cycle of 50% for channel 0.
  • Set a duty cycle of 75% for channel 1 delayed 814 µs with respect to channel 0.
extern crate linux_embedded_hal as hal;
extern crate pwm_pca9685 as pca9685;
use pca9685::{ Channel, Pca9685, SlaveAddr };

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut pwm = Pca9685::new(dev, address);
pwm.set_prescale(100).unwrap();

// Turn on channel 0 at 0
pwm.set_channel_on(Channel::C0, 0).unwrap();

// Turn off channel 0 at 2047, which is 50% in the range `[0..4095]`.
pwm.set_channel_off(Channel::C0, 2047).unwrap();

// Turn on channel 1 at 200. This value comes from:
// 0.000814 (seconds) * 60 (Hz) * 4096 (resolution) = 200
pwm.set_channel_on(Channel::C1, 200).unwrap();

// Turn off channel 1 at 3271, which is 75% in the range `[0..4095]`
// plus 200 which is when the channel turns on.
pwm.set_channel_off(Channel::C1, 3271).unwrap();

Structs

Pca9685

PCA9685 PWM/Servo/LED controller.

Enums

Channel

Output channel selection

Error

All possible errors in this crate

OutputLogicState

Output logic state inversion

SlaveAddr

Possible slave addresses