Expand description

AHT20 driver.

Example:

let mut aht20_uninit = AHT20::new(mock_i2c, SENSOR_ADDRESS);
let mut aht20 = aht20_uninit.init(&mut mock_delay).unwrap();
let measurement = aht20.measure(&mut mock_delay).unwrap();

println!("temperature (aht20): {:.2}C", measurement.temperature);
println!("humidity (aht20): {:.2}%", measurement.humidity);

AHT20 Datasheet

Note that the datasheet linked directly from the manufacturer’s website Aogong AHT20 is an older datasheet (version 1.0, rather than version 1.1 as linked above) and is significantly more difficult to understand. I recommend reading version 1.1. All section references in this file are to the 1.1 version.

The below is a flowchart of how the sensor gets initialized and measurements taken. Note that the flowchart does not include the parameters that you need to give to some commands, and it also doesn’t include the SoftReset command flow.

          Start (Power on)
                 │
                 ▼
             Wait 40 ms
                 │
                 ▼
  Command::CheckStatus  (0x71)    ◄───    Wait 10 ms
                 │                           ▲
                 ▼                           │
         Status::Calibrated ──► No ──► Command::Initialize (0xBE)
                 │
                 ▼
                Yes
                 │
                 ▼
Command::TriggerMeasurement  (0xAC)  ◄─┐
                 │                     │
                 ▼                     │
            Wait 80 ms                 │
                 │                     │
                 ▼                     │
  Command::CheckStatus (0x71) ◄──┐     │
                 │               │     │
                 ▼               │     │
            Status::Busy  ───►  Yes    │
                 │                     │
                 ▼                     │
                No                     │
                 │                     │
                 ▼                     │
            Read 7 bytes               │
                 │                     │
                 ▼                     │
            Calculate CRC              │
                 │                     │
                 ▼                     │
              CRC good ─► No  ─────────┘
                 │                     ▲
                 ▼                     │
                Yes                    │
                 │                     │
                 ▼                     │
          CRC-checked Ready ─► No ─────┘
                 │
                 ▼
                Yes
                 │
                 ▼
       Calc Humidity and Temp

Structs

An AHT20 sensor on the I2C bus I.

AHT20Initialized is returned by AHT20::init() and the sensor is ready to read from.

SensorReading is a single reading from the AHT20 sensor.

SensorStatus is the response from the sensor indicating if it is ready to read from, and if it is calibrated.

Enums

Commands that can be sent to the AHT20 sensor.

Driver errors.

Status byte meanings.

Constants

AHT20 sensor’s I2C address.