sht4x-rs 0.1.0

Sensirion SHT4x temperature & humidity sensor driver (embedded-hal 1.0, no_std, blocking + async)
Documentation

sht4x-rs

Crates.io Docs.rs License Build

A platform-agnostic embedded-hal 1.0 driver for the Sensirion SHT4x family of high-accuracy temperature and humidity sensors. no_std, blocking and async APIs, zero unsafe.

Note: the bare sht4x name on crates.io is already taken, so this crate is published as sht4x-rs. The library itself is imported as use sht4x::....

Supported sensors

  • SHT40
  • SHT41
  • SHT45

All variants share the same I²C protocol; the differences are accuracy class and packaging.

Features

Feature Default Description
blocking yes Sync API on embedded-hal 1.0 traits.
async no Mirror API on embedded-hal-async 1.0 traits.
defmt no defmt::Format impls for the public types & errors.

Blocking usage

use sht4x::{Sht4x, Precision, DEFAULT_ADDRESS};

# fn run<I, D>(i2c: I, delay: D) -> Result<(), sht4x::Error<I::Error>>
# where I: embedded_hal::i2c::I2c, D: embedded_hal::delay::DelayNs {
let mut sensor = Sht4x::new(i2c, delay, DEFAULT_ADDRESS);
let m = sensor.measure(Precision::High)?;
println!("{:.2} °C / {:.2} %RH", m.temperature_celsius(), m.humidity_percent());
# Ok(()) }

Async usage

Enable the async cargo feature:

sht4x-rs = { version = "0.1", default-features = false, features = ["async"] }
use sht4x::{Sht4x, Precision, DEFAULT_ADDRESS};

let mut sensor = Sht4x::new(i2c, delay, DEFAULT_ADDRESS);
let m = sensor.measure_async(Precision::High).await?;

Integer accessors (no FPU? no problem)

Every Measurement exposes fixed-point accessors that never touch f32:

m.temperature_milli_celsius(); // e.g. 23_456 -> 23.456 °C
m.humidity_milli_percent();    // e.g. 50_123 -> 50.123 %RH

Linux example

See examples/linux.rs for a Raspberry-Pi-style usage with linux-embedded-hal:

cargo run --example linux --features blocking

Datasheet

Sensirion SHT4x datasheet (current revision): https://sensirion.com/resource/datasheet/sht4x.

Command bytes and timings in this driver were cross-checked against datasheet rev. 7.1 (March 2025).

Contributing

Issues, PRs, and additional hardware test reports are welcome at https://github.com/KarpagamKarthikeyan/sht4x-rs. Please run cargo test, cargo clippy --all-features -- -D warnings, and cargo fmt before opening a PR.

License

Licensed under either of

at your option.

Contribution

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.