Crate embedded_sht3x

Source
Expand description

This is a platform agnostic Rust driver the SHT3x (SHT30, SHT31 and SHT35) digital humidity and temperature sensors using the embedded-hal traits.

§The device

The sensors of the SHT3x family are humidity and temperature sensors, that are fully calibrated and working with wide range of supply voltages, from 2.15 V to 5.5 V, and using an I²C interface.

They procure fast start-up and measurement times, and great accuracy. The typical accuracies are the following:

SHT30SHT31SHT35
±2 %RH / ±0.2 °C±2 %RH / ±0.2 °C±1.5 %RH / ±0.1 °C

The SHT3x is equipped with an internal heater which can increase the temperature in the range of a few degrees centigrade, and that is useful for pausibility checks.

§Documentation:

§Features

  • Get the status of the sensor.
  • Clear the status of the sensor.
  • Enable the internal heater.
  • Disable the internal heater.
  • Perform a single-shot measurement of temperature and relative humidity.
  • Do a sofware set.
  • Convert temperatures between °C and °F.
  • Calculate the absolute humidity from a measurement.
  • Perform periodic measurement of temperature and relative humidity.
  • Include a no floating-point variant for systems without fpu.

§Usage

To use this driver, import what you need from this crate and an embedded-hal implentation, then instatiate the device.

use embedded_sht3x::{Repeatability::High, Sht3x, DEFAULT_I2C_ADDRESS};
use linux_embedded_hal as hal;

fn main() -> Result<(), embedded_sht3x::Error<hal::I2CError>> {
    // Create the I2C device from the chosen embedded-hal implementation,
    // in this case linux-embedded-hal
    let i2c = match hal::I2cdev::new("/dev/i2c-1") {
        Err(err) => {
            eprintln!("Could not create I2C device: {}", err);
            std::process::exit(1);
        }
        Ok(i2c) => i2c,
    };

    // Create the sensor and configure its repeatability
    let mut sensor = Sht3x::new(i2c, DEFAULT_I2C_ADDRESS, hal::Delay {});
    sensor.repeatability = High;

    // Perform a temperature and humidity measurement
    let measurement = sensor.single_measurement()?;
    println!(
        "Temperature: {:.2} °C, Relative humidity: {:.2} %",
        measurement.temperature, measurement.humidity
    );
    Ok(())
}

§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.

Structs§

Measurement
The result of a measurement.
Sht3x
SHT3x device driver
Status
The status of the sensor.

Enums§

Error
All possible errors generated when using the Sht3x struct
Repeatability
The repeatability influences the measument duration and the energy consumption of the sensor It also gives a more or less accurate measurement
TemperatureUnit
The temperature unit to use in the measurements.

Constants§

DEFAULT_I2C_ADDRESS
The default I2C address (ADDR pin connected to low)
I2C_ADDRESS_LOGIC_HIGH
The I2C address when the ADDR pin is connected to logic high
I2C_ADDRESS_LOGIC_LOW
The I2C address when the ADDR pin is connected to logic low

Functions§

calculate_absolute_humidity
Converts a relative humidity value in % to an absolute humidity value in g/m³, temperature being in °C.
convert_celcius_to_farenheit
Converts a temperature in °C to °F.
convert_farenheit_to_celcius
Converts a temperature in °F to °C.