You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
A cargo library for the SHT31 temperature / humidity sensors
Usage
The default sensor usage includes a blocking sensor read that blocksthe current thread until the sensor does a reading.
use sht31::prelude::*;fnmain() ->Result<()>{// Requires an i2c connection + delay (both from embedded_hal 1.x.x)let sht =SHT31::new(i2c, delay);loop{let reading = sht.read()?;}}
Advanced Single Shot Usage
Breaks down the simple usage into two commands, one wherethe sensor is notified to start reading and another wherethe reading is requested
use sht31::prelude::*;fnmain() ->Result<()>{// i2c setup// Use single shot, with high accuracy, an alternate I2C address// and return temp data in Fahrenheitlet sht =SHT31::single_shot(i2c,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 Usage
Periodic mode continually reads data at a rate of thegiven measurements per second (MPS) and at a qualityof the given Accuracy. This means that you dont have toworry about runningSHT31::measure() every time you wantto read data
use sht31::prelude::*;fnmain() ->Result<()>{// i2c setup// In periodic mode, the sensor keeps updating the reading// without needing to measureletmut sht =SHT31::periodic( i2c,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 with Accelerated Response Time
Periodic mode has a unique mode that allows the sensor to read data at 4Hz
use sht31::prelude::*;fnmain() ->Result<()>{// Makes the sensor acquire the data at 4 Hzletmut sht =SHT31::periodic(i2c,Periodic::new().with_art()); sht.measure()?;loop{let reading = sht.read()?;}}
Mode switching
This crate also supports more complex case scenarios where you might want to switchbetween period to single shot for example
use sht31::prelude::*;fnmain() ->Result<()>{// i2c setupletmut sht =SHT31::periodic(i2c,Periodic::new());// Trigger the measure before running your loop to initialize the periodic mode sht.measure()?;// Do a periodic readlet 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()?;}
About
A cargo library for the SHT31 temperature / humidity sensors