OPT300x Ambient Light Sensor Driver in Rust

OPT3001 measuring the ambient light
   

Today I present you a platform-agnostic Rust driver for the OPT300x family of ambient light sensors.
The OPT3001, for example, can be found on the TI CC2650STK SensorTag board.

The devices

This driver is compatible with the devices OPT3001, OPT3002, OPT3004, OPT3006 and OPT3007.

The OPT3001 is a sensor that measures the intensity of visible light. The spectral response of the sensor tightly matches the photopic response of the human eye and includes significant infrared rejection.

The OPT3001 is a single-chip lux meter, measuring the intensity of light as visible by the human eye. The precision spectral response and strong IR rejection of the device enables the OPT3001 to accurately meter the intensity of light as seen by the human eye regardless of light source. The strong IR rejection also aids in maintaining high accuracy when industrial design calls for mounting the sensor under dark glass for aesthetics. The OPT3001 is designed for systems that create light-based experiences for humans, and an ideal preferred replacement for photodiodes, photoresistors, or other ambient light sensors with less human eye matching and IR rejection.

Measurements can be made from 0.01 lux up to 83k lux without manually selecting full-scale ranges by using the built-in, full-scale setting feature. This capability allows light measurement over a 23-bit effective dynamic range.

The digital operation is flexible for system integration. Measurements can be either continuous or single-shot. The control and interrupt system features autonomous operation, allowing the processor to sleep while the sensor searches for appropriate wake-up events to report via the interrupt pin. The digital output is reported over an I2C- and SMBus-compatible, two-wire serial interface.

The low power consumption and low power-supply voltage capability of the OPT3001 enhance the battery life of battery-powered systems.

Datasheets: OPT3001, OPT3002, OPT3004, OPT3006, OPT3007
Application Guide: OPT3001 ALS Application Guide

Using the driver

To use the device from Rust, you have to add the opt300x crate to your project as well as a concrete implementation of the embedded-hal traits. For example if you are using the Raspberry Pi running Linux (see driver-examples for bare-metal hardware):

1
2
3
4
5
# Cargo.toml
...
[dependencies]
opt300x = "0.1"
linux-embedded-hal = "0.3"

Here is an example program which will measure the ambient light in lux and print it (source):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
extern crate linux_embedded_hal as hal;
extern crate opt300x;
use opt300x::{Opt300x, SlaveAddr};

fn main() {
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let sensor = Opt300x::new_opt3001(dev, address);
let mut sensor = sensor.into_continuous().ok().unwrap();
loop {
let lux = sensor.read_lux().unwrap();
println!("lux: {:2}", lux);
}
}

I also created an example program that runs on the STM32F1 “BluePill” board which continuously reads the me measurement and prints it on an OLED display. You can find the application source code here.

In the driver-examples repository you can find further examples which you can adapt to do other things with this device.

Where to go from here?

There is much more information and example programs in the crate documentation.
Please give this driver a try and report any issues you may encounter in the issue tracker.
Feedback, suggestions and improvements are gladly welcome.

What’s next?

I have been writing many other platform-agnostic Rust drivers although I am slow to announce them here. If you want to know what I am currently working on you can follow me on github.

Thanks for reading and stay tuned!

Links: Source code - Crate - Documentation

Share