Expand description
Platform agnostic Rust driver for Sensirion SGP41 device with
gas, temperature and humidity sensors based on
the embedded-hal
traits.
§Sensirion SGP41
Sensirion SGP41 is a low-power accurate gas sensor for air quality application. The sensor has different sampling rates to optimize power-consumption per application bases as well as ability save and set the baseline for faster start-up accuracy. The sensor uses I²C interface and measures TVOC (Total Volatile Organic Compounds) and NOx (Nitrogen Oxides).
Datasheet: https://www.sensirion.com/file/datasheet_sgp41
§Usage
§Instantiating
Import this crate and an embedded_hal
implementation, then instantiate
the device:
use linux_embedded_hal as hal;
use hal::{Delay, I2cdev};
use sgp41::Sgp41;
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sgp = Sgp41::new(dev, 0x59, Delay);
§Doing Measurements
The SGP41 sensor provides both VOC and NOx measurements. The device performs measurements independently and calls to the driver fetch the latest information.
§Basic VOC and NOx Measurements
use linux_embedded_hal as hal;
use hal::{Delay, I2cdev};
use std::time::Duration;
use std::thread;
use sgp41::Sgp41;
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Sgp41::new(dev, 0x59, Delay);
// Discard the first 45 samples as the algorithm is warming up.
for _ in 1..=45 {
sensor.measure_voc_index().unwrap();
}
loop {
// Individual measurements
if let Ok(voc) = sensor.measure_voc_index() {
println!("VOC index: {}", voc);
}
if let Ok(nox) = sensor.measure_nox_index() {
println!("NOx index: {}", nox);
}
// Combined measurement (more efficient)
if let Ok((voc, nox)) = sensor.measure_indices() {
println!("Combined - VOC: {}, NOx: {}", voc, nox);
}
thread::sleep(Duration::new(1_u64, 0));
}
§Temperature and Humidity Compensation
For improved accuracy, provide temperature and humidity data:
let humidity = 50_u16; // 50% RH
let temperature = 25_i16; // 25°C
// Compensated individual measurements
let voc = sensor.measure_voc_index_with_rht(humidity, temperature).unwrap();
let nox = sensor.measure_nox_index_with_rht(humidity, temperature).unwrap();
// Compensated combined measurement
let (voc, nox) = sensor.measure_indices_with_rht(humidity, temperature).unwrap();
§Raw Signal Access
Access raw sensor signals for custom processing:
// Individual raw signals
let voc_raw = sensor.measure_raw().unwrap();
let nox_raw = sensor.measure_raw_nox().unwrap();
// Combined raw signals
let (voc_raw, nox_raw) = sensor.measure_raw_signals().unwrap();
§Gas Index Calculation
Both VOC and NOx index calculations use the gas-index-algorithm crate which implements the official Sensirion algorithms and is no-std compatible.
- VOC index range: 1-500 (100 = average air quality)
- NOx index range: 1-500 (1 = average air quality)
Structs§
- Sgp41
- Sgp41 driver instance
Enums§
- Error
- Sgp41 errors