Crates.io | sht4x-rjw |
lib.rs | sht4x-rjw |
version | 0.1.1 |
created_at | 2025-09-18 05:47:54.661729+00 |
updated_at | 2025-09-18 05:54:41.036998+00 |
description | no-std driver for the Sensirion SHT4x series of temperature and humidity sensors |
homepage | |
repository | https://github.com/robjwells/sht4x-rjw |
max_upload_size | |
id | 1844274 |
size | 77,295 |
sht4x-rjw
is an embedded-hal
, no_std
driver for the Sensirion SHT4x
series of I2C temperature and humidity sensors with blocking and
async support. The driver implements all features described in section 4.5
of the datasheet.
By default, this crate contains a blocking driver, blocking::SHT4x
.
Optional features include:
embedded-hal-async
. Use the async
feature flag
and the asynch::SHT4x
driver struct. The blocking and async drivers are
otherwise identical.defmt
support through the defmt
feature flag.fixed
feature flag and the
fixed
crate.You can remove the blocking driver by passing --no-default-features
to
cargo add
, or adding default-features = false
to the dependency spec in
your Cargo.toml
.
let mut sensor = SHT4x::new(i2c, Default::default());
let serial_number = sensor.serial_number()?;
let measurement = sensor.measure(&mut delay)?;
defmt::info!(
"SHT4x sensor with serial {}, currently: {}°C, {}%RH",
serial_number,
measurement.celsius(),
measurement.humidity()
);
Construct the driver by passing in an I2C interface and configuration struct
(used to set defaults for SHT4x::measure()
). You can retrieve the I2C
interface with SHT4x::destroy()
.
You can choose the type of measurement conducted (varying in repeatability
and heater use) with ReadingMode
. The default value of Config
is set
for high-repeatability measurements.
When reading measurements from the sensor (or performing a soft reset), pass
in an implementation of embedded_hal::delay::DelayNs
. (This is done to avoid
the driver having to take ownership of the delay struct, as it can be less easy
to share these than I2C interfaces.) The length of the delay is controlled by
DelayMode
.
Temperature and humidity measurements are provided through Measurement
,
which has methods for converting the raw two-byte sensor measurement into
recognisable units. The raw measurements can also be accessed, and the
conversion functions are available in the conversions
module.
Data is read from the sensor as two groups of three bytes: two data bytes and
one CRC byte for error detection. These CRC bytes are always checked before the
data bytes are made available for conversion. Should an error be detected in
the data read from the sensor, the Error
enum will contain the bytes in
question (both data bytes and the CRC byte read from the sensor).
The sensor struct uses a default I2C address of 0x44
, as this appears to be
the most common. However, sensors with part numbers including -B
and -C
have I2C addresses of 0x45
and 0x46
, respectively. See section 9 of the
datasheet.
Should you need to use an address other than 0x44
, instantiate the struct
as normal and write to its address
field, as so:
let mut sensor = SHT4x::new(i2c, Default::default());
sensor.address = 0x46;
10-bit I2C addresses are not supported.
There are (as of this writing) four parts in the SHT4x line: the SHT40, SHT41, SHT43 and SHT45. There is also the SHT4xA line of automotive parts. All of these sensors share the same I2C interface, so this library should work with all of them, though at the moment it has only been tested with the SHT40.
Note that there appear to be some differences in the characteristics of the automotive parts (slower timings, for instance) which are not accounted for at present. If this affects you please open an issue.
You may prefer to use the following drivers for the SHT4x:
The sht4x_rjw
crate is copyright 2025 Rob Wells, and is licensed under the
Apache License, Version 2.0, or the MIT License, at your option.