Crates.io | pwm-pca9685 |
lib.rs | pwm-pca9685 |
version | 1.0.0 |
source | src |
created_at | 2018-11-26 19:35:12.262659 |
updated_at | 2024-04-05 20:56:10.881742 |
description | Platform-agnostic Rust driver for the PCA9685 I2C 16-channel, 12-bit PWM/Servo/LED controller. |
homepage | https://github.com/eldruin/pwm-pca9685-rs |
repository | https://github.com/eldruin/pwm-pca9685-rs |
max_upload_size | |
id | 98812 |
size | 108,730 |
This is a platform agnostic Rust driver for the PCA9685 PWM/Servo/LED
controller, based on the embedded-hal
traits.
This driver also supports the embedded-hal-async
traits if the async
feature is enabled.
This driver allows you to:
enable()
.set_channel_on()
.set_channel_on_off()
.set_channel_full_on()
.set_all_on_off()
.set_all_channels()
.set_prescale()
.set_output_logic_state()
.set_output_change_behavior()
.set_output_driver()
.use_external_clock()
.enable_programmable_address()
.set_programmable_address()
.set_address()
.enable_restart_and_disable()
.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: PCA9685
Please find additional examples in this repository: driver-examples
To use this driver, import this crate and an embedded_hal
implementation,
then instantiate the appropriate device.
In this example we set a PWM frequency of 60 Hz and a duty cycle of 50% on channel 0.
use linux_embedded_hal::I2cdev;
use pwm_pca9685::{Address, Channel, Pca9685};
fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = Address::default();
let mut pwm = Pca9685::new(dev, address).unwrap();
// This corresponds to a frequency of 60 Hz.
pwm.set_prescale(100).unwrap();
// It is necessary to enable the device.
pwm.enable().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();
let _dev = pwm.destroy(); // Get the I2C device back
}
The same settings, but async with the Embassy framework on an RP2040:
# Cargo.toml
pwm-pca9685 = { version = "1.0.0", features = ["async"] }
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_rp::{bind_interrupts, i2c};
use embassy_rp::peripherals::I2C0;
use embassy_time::Timer;
use panic_halt as _;
use pwm_pca9685::{Address, Channel, Pca9685};
bind_interrupts!(struct Irqs {
I2C0_IRQ => i2c::InterruptHandler<I2C0>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let i2c = i2c::I2c::new_async(p.I2C0, p.PIN_1, p.PIN_0, Irqs, i2c::Config::default());
let address = Address::default();
let mut pwm = Pca9685::new(dev, address).await.unwrap();
// This corresponds to a frequency of 60 Hz.
pwm.set_prescale(100).await.unwrap();
// It is necessary to enable the device.
pwm.enable().await.unwrap();
// Turn on channel 0 at 0.
pwm.set_channel_on(Channel::C0, 0).await.unwrap();
// Turn off channel 0 at 2047, which is 50% in
// the range `[0..4095]`.
pwm.set_channel_off(Channel::C0, 2047).await.unwrap();
let _dev = pwm.destroy(); // Get the I2C device back
}
For questions, issues, feature requests, and other changes, please file an issue in the github project.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.