2 releases
Uses new Rust 2024
| 0.1.1 | Sep 18, 2025 |
|---|---|
| 0.1.0 | Sep 18, 2025 |
#404 in Embedded development
291 downloads per month
54KB
489 lines
SHT4x temperature and humidity sensor embedded-hal driver
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.
Features
By default, this crate contains a blocking driver, blocking::SHT4x.
Optional features include:
- Async support via
embedded-hal-async. Use theasyncfeature flag and theasynch::SHT4xdriver struct. The blocking and async drivers are otherwise identical. defmtsupport through thedefmtfeature flag.- Fixed-point conversions through the
fixedfeature flag and thefixedcrate.
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.
Example usage
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()
);
Driver operation
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).
Sensor I2C address
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.
Sensor variant support
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.
Similar crates
You may prefer to use the following drivers for the SHT4x:
License
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.
Dependencies
~0–0.9MB
~16K SLoC