aht: AHT10, AHT15 and AHT20 humidity and temperature sensors
ESP-IDF driver for the AHT10, AHT15 and AHT20 humidity and temperature sensors. The driver talks to the sensor over I2C through i2cdev, triggers a single measurement on request and returns calibrated values in degrees Celsius and relative humidity in percents. AHT10 and AHT15 share one command set, AHT20 has its own calibration command and appends a CRC-8 to the measurement frame, which the driver checks.
Features
Temperature in degrees Celsius and relative humidity in percents.
AHT10, AHT15 and AHT20 support; the type is a field of the device descriptor.
Calibration check: the sensor reports whether the factory calibration is valid.
Soft reset that re-initialises the sensor.
CRC-8 check of the AHT20 measurement frame: a corrupted frame is rejected instead of converted into a plausible number.
Polling for the end of the measurement with a timeout instead of a blind delay.
Several sensors on one bus, one descriptor per device.
Thread safety through the i2cdev device mutex.
Hardware
Module pin |
Connect to |
|---|---|
VDD |
3.3 V (see the datasheet of your part) |
GND |
GND |
SDA |
any GPIO, I2C data, needs a pull-up |
SCL |
any GPIO, I2C clock, needs a pull-up |
ADDR |
GND or VDD, selects the I2C address |
The address is fixed by the ADDR pin:
ADDR tied to GND:
0x38(AHT_I2C_ADDRESS_GND)ADDR tied to VDD:
0x39(AHT_I2C_ADDRESS_VCC)
The driver rejects any other address in aht_init_desc(). The bus runs at
400 kHz. The modules have no pull-ups of their own; use 4.7 kΩ from SDA and SCL
to 3.3 V unless the breakout board already has them.
Usage
Initialize i2cdev once per application, then initialize a descriptor per
sensor and call aht_init() once. aht_get_data() triggers the
measurement, waits for the sensor to finish it and reads the result; the output
values are written only when the call returns ESP_OK. An example is in
examples/default.
Timing
Operation |
AHT10/AHT15 |
AHT20 |
|---|---|---|
Power-on before the first command |
20 ms |
40 ms |
Calibration after the command |
350 ms |
10 ms |
Measurement |
80 ms |
80 ms |
Reset |
20 ms |
20 ms |
A measurement blocks the calling task for about 80 ms. If that is too long, run the reads in a separate task; the device mutex keeps the bus safe.
Limitations
The driver performs single measurements; the continuous mode (
AHT_MODE_CYCLE) is not implemented and the field exists only for source compatibility.Only the two addresses above are accepted.
The sensor has no unique ID, so nothing identifies it on the bus except the address.
Defines
- AHT_I2C_ADDRESS_GND
Device address when ADDR pin connected to GND.
- AHT_I2C_ADDRESS_VCC
Device address when ADDR pin connected to VCC.
Enums
- enum aht_type_t
Device types.
Values:
- enumerator AHT_TYPE_AHT1x
AHT10, AHT15.
- enumerator AHT_TYPE_AHT20
AHT20.
- enum aht_mode_t
Device modes.
- Deprecated:
The sensor modes are not supported by the driver: it always triggers a measurement in the command mode. The field is kept for the source compatibility only.
Values:
- enumerator AHT_MODE_NORMAL
Normal mode.
- enumerator AHT_MODE_CYCLE
Continuous measurements mode, not supported by the driver.
Functions
- esp_err_t aht_init_desc(aht_t *dev, uint8_t addr, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
Initialize device descriptor.
- Parameters:
dev – Device descriptor
addr – Device I2C address
port – I2C port
sda_gpio – SDA GPIO
scl_gpio – SCL GPIO
- Returns:
ESP_OKon success
- esp_err_t aht_free_desc(aht_t *dev)
Free device descriptor.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
- esp_err_t aht_init(aht_t *dev)
Init device.
Waits for the sensor to power up, sends the calibration command and checks the calibration bit. The command differs between the device types:
0xE1for AHT10/AHT15 and0xBEfor AHT20.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
ESP_ERR_INVALID_STATEif the sensor has not been calibrated
ESP_ERR_TIMEOUTif the sensor has not become idle
- esp_err_t aht_reset(aht_t *dev)
Soft reset device.
Resets the sensor and initializes it again.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
ESP_ERR_TIMEOUTif the sensor has not become idle
- esp_err_t aht_get_status(aht_t *dev, bool *busy, bool *calibrated)
Get device status.
- Parameters:
dev – Device descriptor
busy – [out] Busy flag
true: device currently measuring
false: device is idle
calibrated – [out] Calibration success flag
true: sensor calibrated
false: sensor not calibrated
- Returns:
ESP_OKon success
- esp_err_t aht_get_data(aht_t *dev, float *temperature, float *humidity)
Get temperature and relative humidity.
Triggers a measurement, waits for the sensor to finish it and reads the result. The output values are set only when
ESP_OKis returned. For the AHT20 the frame contains a CRC-8 checksum, and a corrupted frame is rejected.
- Parameters:
dev – Device descriptor
temperature – [out] Temperature, degrees Celsius
humidity – [out] Relative humidity, percents
- Returns:
ESP_OKon success
ESP_ERR_TIMEOUTif the sensor has not finished the measurement
ESP_ERR_INVALID_STATEif the sensor is not calibrated
ESP_ERR_INVALID_RESPONSEif the AHT20 frame has a wrong CRC
- struct aht_t
#include <aht.h>Device descriptor.