#i2c-driver #driver #embedded-hal-driver #i2c #sensor #light-sensor #color

no-std tcs3472

Platform-agnostic Rust driver for the TCS3472 RGB color light to digital converter with IR filter

4 releases (1 stable)

new 1.0.0 Jan 2, 2025
0.2.0 Apr 5, 2021
0.1.1 Oct 21, 2018
0.1.0 Oct 20, 2018

#1685 in Embedded development

Download history 3/week @ 2024-09-17 25/week @ 2024-09-24 15/week @ 2024-10-01 2/week @ 2024-12-03 11/week @ 2024-12-10 138/week @ 2024-12-31

151 downloads per month

MIT/Apache

30KB
303 lines

Rust TCS3472 RGB Color Light to Digital Converter with IR Filter Driver

crates.io Docs Minimum Supported Rust Version Build Status Coverage Status

This is a platform-agnostic Rust driver for the TCS3472 RGB color light to digital converter with IR filter, based on the embedded-hal traits.

This driver allows you to:

  • Enable/disable the device.
  • Enable/disable the RGB converter.
  • Set RGB converter gain.
  • Enable/disable the RGB converter interrupt generation.
  • Set the RGB converter interrupt clear channel low/high thresholds.
  • Set the RGB converter interrupt persistence.
  • Set the number of integration cycles.
  • Enable/disable the wait feature.
  • Set the number of wait time cycles.
  • Enable/disable the wait long setting.
  • Read status of RGB converter.
  • Read the clear (unfiltered) channel measurement.
  • Read the red channel measurement.
  • Read the green channel measurement.
  • Read the blue channel measurement.
  • Read the measurement of all channels at once.
  • Read the device ID.

The device

The TCS3472 device provides a digital return of red, green, blue (RGB), and clear light sensing values. An IR blocking filter, integrated on-chip and localized to the color sensing photodiodes, minimizes the IR spectral component of the incoming light and allows color measurements to be made accurately. The high sensitivity, wide dynamic range, and IR blocking filter make the TCS3472 an ideal color sensor solution for use under varying lighting conditions and through attenuating materials.

The TCS3472 color sensor has a wide range of applications including RGB LED backlight control, solid-state lighting, health/fitness products, industrial process controls and medical diagnostic equipment. In addition, the IR blocking filter enables the TCS3472 to perform ambient light sensing (ALS). Ambient light sensing is widely used in display-based products such as cell phones, notebooks, and TVs to sense the lighting environment and enable automatic display brightness for optimal viewing and power savings. The TCS3472, itself, can enter a lower-power wait state between light sensing measurements to further reduce the average power consumption.

Datasheet:

This driver is compatible with the devices TCS34725 and TCS34727.

Usage

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

Please find additional examples using hardware in this repository: driver-examples

use linux_embedded_hal::I2cdev;
use tcs3472::Tcs3472;

fn main() {
    let dev = I2cdev::new("/dev/i2c-1").unwrap();
    let mut sensor = Tcs3472::new(dev);
    sensor.enable().unwrap();
    sensor.enable_rgbc().unwrap();
    while !sensor.is_rgbc_status_valid().unwrap() {
        // wait for measurement to be available
    }
    let m = sensor.read_all_channels().unwrap();
    println!(
        "Measurements: clear = {}, red = {}, green = {}, blue = {}",
        m.clear, m.red, m.green, m.blue
    );
}

This driver also supports the embedded-hal-async traits if the async feature is enabled in the Cargo.toml file:

tcs3472 = { version = "0.3.0", features = ["async"] }

Example how it looks like when using the Embassy framework:

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_stm32::init(Default::default());
    // embassy i2c setup details omitted
    let mut i2c = I2c::new(..);
    let mut sensor = Tcs3472::new(i2c);
    sensor.enable().await.unwrap();
    sensor.enable_rgbc().await.unwrap();
    while !sensor.is_rgbc_status_valid().await.unwrap() {
        // wait for measurement to be available
    };
    let m = sensor.read_all_channels().await.unwrap();
    defmt::info!(
        "Measurements: clear = {}, red = {}, green = {}, blue = {}",
        m.clear, m.red, m.green, m.blue
    );
}

Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.81.0 and up. It might compile with older versions but that may change in any new patch release.

Support

For questions, issues, feature requests, and other changes, please file an issue in the github project.

License

Licensed under either of:

at your option.

Contributing

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.

Dependencies

~2MB
~41K SLoC