sgp4x
Platform agnostic Rust device driver for Sensirion SGP4x created based on https://crates.io/crates/sgp40. At the moment, it supports both SGP41 and SGP40 (although NOx will fail for SGP40)
Platform agnostic Rust driver for Sensirion SGP41 gas sensor using the embedded-hal traits.
Sensirion SGP4X
Sensirion SGP4X is a low-power accurate gas sensor for air quality applications. The sensor uses I²C interface and measures both VOC (Total Volatile Organic Compounds) and NOx (Nitrogen Oxides) providing comprehensive air quality monitoring capabilities.
Key features:
- Dual gas measurement: VOC and NOx indices
- Temperature/humidity compensation: Improved accuracy with environmental data
- Low power consumption: Optimized for battery-powered applications
- I²C interface: Simple integration with 0x59 address
- No-std compatible: Works in embedded environments
- Gas index algorithms: Uses official Sensirion algorithms via gas-index-algorithm crate
Datasheet: https://www.sensirion.com/file/datasheet_sgp41
Usage
Basic VOC and NOx Measurements
use linux_embedded_hal as hal;
use ;
use Sgp41;
let dev = new.unwrap;
let mut sensor = new;
// Warm up the sensor (first 45 samples should be discarded)
for _ in 0..45
// Individual measurements
let voc_index = sensor.measure_voc_index?; // 1-500 (100 = average)
let nox_index = sensor.measure_nox_index?; // 1-500 (1 = average)
// Combined measurement (more efficient)
let = sensor.measure_indices?;
Temperature and Humidity Compensation
let humidity = 50_u16; // 50% RH
let temperature = 25_i16; // 25°C
// Compensated measurements
let voc = sensor.measure_voc_index_with_rht?;
let nox = sensor.measure_nox_index_with_rht?;
// Combined compensated measurement
let = sensor.measure_indices_with_rht?;
Raw Signal Access
// Individual raw signals
let voc_raw = sensor.measure_raw?;
let nox_raw = sensor.measure_raw_nox?;
// Combined raw signals
let = sensor.measure_raw_signals?;
SGP41-Specific Features
// Conditioning (recommended for new sensors)
sensor.execute_conditioning?;
// Conditioning with VOC reading
sensor.execute_conditioning_with_voc_read?;
Migration from SGP40
The SGP41 driver maintains API compatibility with SGP40 for VOC measurements:
// SGP40 code
use Sgp40;
let mut sensor = new;
let voc = sensor.measure_voc_index?;
// SGP41 equivalent
use Sgp41;
let mut sensor = new;
let voc = sensor.measure_voc_index?; // Same API
let nox = sensor.measure_nox_index?; // New capability
Examples
main.rs- Linux example showing all measurement typesnrf52-example- Embedded no-std example for NRF52
Development status
The crate is feature complete with full VOC and NOx measurement capabilities.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) 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.