[][src]Crate veml6030

This is a platform agnostic Rust driver for the VEML6030 and VEML7700 high-accuracy ambient light sensors using the embedded-hal traits.

This driver allows you to:

Introductory blog post

The devices

Vishay's VEML6030 / VEML7700 are high accuracy ambient light digital 16-bit resolution sensors in a miniature transparent package. They include a high sensitive photodiode, a low noise amplifier, a 16-bit A/D converter and support an easy to use I2C bus communication interface and additional interrupt feature. The ambient light result is as digital value available.

Datasheets: VEML6030 - VEML7700

Application Note:

Usage examples (see also examples folder)

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

VEML6030 and VEML7700 expose the same interface over I2C. To communicate with a VEML7700 simply use this driver as if communicating with a VEML6030.

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

Read the lux

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.enable().unwrap();
loop {
    let lux = sensor.read_lux().unwrap();
    println!("lux: {:2}", lux);
}

Provide an alternative address

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::Alternative(true);
let mut sensor = Veml6030::new(dev, address);

Set the gain and integration time

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{Gain, IntegrationTime, SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.set_gain(Gain::OneQuarter).unwrap();
sensor.set_integration_time(IntegrationTime::Ms200).unwrap();
sensor.enable().unwrap();

Enable power-saving mode

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{PowerSavingMode, SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.enable_power_saving(PowerSavingMode::One).unwrap();
sensor.enable().unwrap();

Set thresholds, fault count and enable interrupts

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{
    FaultCount, Gain, IntegrationTime, SlaveAddr, Veml6030
};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.set_gain(Gain::OneQuarter).unwrap();
// this will compensate the value automatically before setting it
sensor.set_high_threshold_lux(10000.0).unwrap();
sensor.set_low_threshold_lux(100.0).unwrap();
sensor.set_fault_count(FaultCount::Four).unwrap();
sensor.enable_interrupts().unwrap();
sensor.enable().unwrap();

Precalculate and set compensated threshold values

Using current device configuration

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{Gain, IntegrationTime, SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.set_gain(Gain::OneEighth).unwrap();
sensor.set_integration_time(IntegrationTime::Ms200).unwrap();
let high_th_raw = sensor.calculate_raw_threshold_value(10000.0);
// ...
sensor.set_high_threshold_raw(high_th_raw).unwrap();
// this requires no compensation because the value is < 1000
sensor.set_low_threshold_lux(100.0).unwrap();

Precalculate and set compensated threshold values

Using free function

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{
    calculate_raw_threshold_value,
    Gain, IntegrationTime, SlaveAddr, Veml6030
};

let gain = Gain::OneEighth;
let it = IntegrationTime::Ms200;
let high_th_raw = calculate_raw_threshold_value(it, gain, 10000.0);
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.set_gain(gain).unwrap();
sensor.set_integration_time(it).unwrap();
// ...
sensor.set_high_threshold_raw(high_th_raw).unwrap();
// this requires no compensation because the value is < 1000
sensor.set_low_threshold_lux(100.0).unwrap();
sensor.enable_interrupts().unwrap();
sensor.enable().unwrap();

Read interrupt status

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
// ...
loop {
    let status = sensor.read_interrupt_status().unwrap();
    if status.was_too_high {
        // ...
    }
    if status.was_too_low {
        // ...
    }
}

Read the raw ALS measurement and convert to lux separately

Using current device configuration

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{SlaveAddr, Veml6030};

let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.enable().unwrap();
loop {
    let raw = sensor.read_raw().unwrap();
    // ...
    let lux = sensor.convert_raw_als_to_lux(raw);
    println!("lux: {:2}", lux);
}

Read the raw ALS measurement and convert to lux separately

Using free function

extern crate linux_embedded_hal as hal;
extern crate veml6030;
use veml6030::{
    convert_raw_als_to_lux,
    Gain, IntegrationTime, SlaveAddr, Veml6030
};

let gain = Gain::OneEighth;
let it = IntegrationTime::Ms200;
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Veml6030::new(dev, SlaveAddr::default());
sensor.set_gain(gain).unwrap();
sensor.set_integration_time(it).unwrap();
sensor.enable().unwrap();
loop {
    let raw = sensor.read_raw().unwrap();
    // ...
    let lux = convert_raw_als_to_lux(it, gain, raw);
    println!("lux: {:2}", lux);
}

Structs

InterruptStatus

Interrupt status

Veml6030

VEML6030 device driver

Enums

Error

All possible errors in this crate

FaultCount

Fault count

Gain

Gain

IntegrationTime

Integration time

PowerSavingMode

Power-saving mode

SlaveAddr

Possible slave addresses

Functions

calculate_raw_threshold_value

Calculate raw value for threshold applying compensation if necessary.

convert_raw_als_to_lux

Calculate lux value for a raw ALS measurement.