Crates.io | sgp4x |
lib.rs | sgp4x |
version | 1.0.0 |
created_at | 2025-09-16 11:39:54.818624+00 |
updated_at | 2025-09-16 11:39:54.818624+00 |
description | Rust driver for the Sensirion SGP41 gas sensor with VOC and NOx measurement capabilities. |
homepage | |
repository | https://github.com/lomagno2003/sgp4x-rs |
max_upload_size | |
id | 1841474 |
size | 80,866 |
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 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:
Datasheet: https://www.sensirion.com/file/datasheet_sgp41
use linux_embedded_hal as hal;
use hal::{Delay, I2cdev};
use sgp41::Sgp41;
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Sgp41::new(dev, 0x59, Delay);
// Warm up the sensor (first 45 samples should be discarded)
for _ in 0..45 {
sensor.measure_voc_index().ok();
}
// 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 (voc, nox) = sensor.measure_indices()?;
let humidity = 50_u16; // 50% RH
let temperature = 25_i16; // 25°C
// Compensated measurements
let voc = sensor.measure_voc_index_with_rht(humidity, temperature)?;
let nox = sensor.measure_nox_index_with_rht(humidity, temperature)?;
// Combined compensated measurement
let (voc, nox) = sensor.measure_indices_with_rht(humidity, temperature)?;
// Individual raw signals
let voc_raw = sensor.measure_raw()?;
let nox_raw = sensor.measure_raw_nox()?;
// Combined raw signals
let (voc_raw, nox_raw) = sensor.measure_raw_signals()?;
// Conditioning (recommended for new sensors)
sensor.execute_conditioning()?;
// Conditioning with VOC reading
sensor.execute_conditioning_with_voc_read()?;
The SGP41 driver maintains API compatibility with SGP40 for VOC measurements:
// SGP40 code
use sgp40::Sgp40;
let mut sensor = Sgp40::new(i2c, 0x59, delay);
let voc = sensor.measure_voc_index()?;
// SGP41 equivalent
use sgp41::Sgp41;
let mut sensor = Sgp41::new(i2c, 0x59, delay);
let voc = sensor.measure_voc_index()?; // Same API
let nox = sensor.measure_nox_index()?; // New capability
main.rs
- Linux example showing all measurement typesnrf52-example
- Embedded no-std example for NRF52The crate is feature complete with full VOC and NOx measurement capabilities.
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.
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.