An async library for the SHT30, SHT31 and SHT35 temperature and humidity sensors.
SHT3x-DIS is the next generation of Sensirion’s temperature and humidity sensors. It builds on a new CMOSens® sensor chip that is at the heart of Sensirion’s new humidity and temperature platform. The SHT3x-DIS has increased intelligence, reliability and improved accuracy specifications compared to its predecessor. Its functionality includes enhanced signal processing, two distinctive and user selectable I2C addresses and communication speeds of up to 1 MHz. The DFN package has a footprint of 2.5 x 2.5 mm2 while keeping a height of 0.9 mm. This allows for integration of the SHT3x-DIS into a great variety of applications. Additionally, the wide supply voltage range of 2.15 V to 5.5 V guarantees compatibility with diverse assembly situations. All in all, the SHT3x-DIS incorporates 15 years of knowledge of Sensirion, the leader in the humidity sensor industry.
For more information, see the official data sheet.
The default sensor usage includes a blocking sensor read that blocks the current thread until the sensor does a reading.
use sht31::prelude::*;
fn main() -> Result<()> {
// Requires an i2c connection + delay (both from embedded_hal::blocking)
let sht = SHT31::new(i2c, delay);
loop {
let reading = sht.read()?;
}
}
Breaks down the simple usage into two commands, one where the sensor is notified to start reading and another where the reading is requested
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
// Use single shot, with high accuracy, an alternate I2C address
// and return temp data in Fahrenheit
let sht = SHT31::new(i2c)
.with_mode(SingleShot::new())
.with_accuracy(Accuracy::High)
.with_unit(TemperatureUnit::Fahrenheit)
.with_address(DeviceAddr::AD1);
loop {
// Start measuring before you need the reading,
// this is more efficient than waiting for readings
sht.measure()?;
// Some other code here...
let reading = sht.read()?;
}
}
Periodic mode continually reads data at a rate of the given measurements per
second (MPS) and at a quality of the given Accuracy. This means that you dont
have to worry about running SHT31::measure()
every time you want to read data
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
// In periodic mode, the sensor keeps updating the reading
// without needing to measure
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_mps(MPS::Normal))
.with_accuracy(Accuracy::High);
// Trigger the measure before running your loop to initialize the periodic mode
sht.measure()?;
loop {
let reading = sht.read()?;
}
}
Periodic mode has a unique mode that allows the sensor to read data at 4Hz
use sht31::prelude::*;
fn main() -> Result<()> {
// Makes the sensor acquire the data at 4 Hz
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new().with_art());
sht.measure()?;
loop {
let reading = sht.read()?;
}
}
This crate also supports more complex case scenarios where you might want to switch between period to single shot for example
use sht31::prelude::*;
fn main() -> Result<()> {
// i2c setup
let mut sht = SHT31::new(sht_i2c)
.with_mode(Periodic::new());
// Trigger the measure before running your loop to initialize the periodic mode
sht.measure()?;
// Do a periodic read
let reading = sht.read()?;
// Cancel the currently running command (periodic)
sht.break_command()?;
sht = sht.with_mode(SingleShot::new());
sht.measure()?;
let new_reading = sht.read()?;
}