esp-idf-lib/aht

1.1.1

Latest
uploaded 5 days ago
Driver for AHT10/AHT15/AHT20 temperature and humidity sensor

Readme

# esp-idf-lib/aht

[![Build examples](https://github.com/esp-idf-lib/aht/actions/workflows/build.yml/badge.svg)](https://github.com/esp-idf-lib/aht/actions/workflows/build.yml)
[![Build docs](https://github.com/esp-idf-lib/aht/actions/workflows/build-docs.yml/badge.svg)](https://github.com/esp-idf-lib/aht/actions/workflows/build-docs.yml)
[![Validation](https://github.com/esp-idf-lib/aht/actions/workflows/validate-component.yml/badge.svg)](https://github.com/esp-idf-lib/aht/actions/workflows/validate-component.yml)

ESP-IDF driver for the AHT10, AHT15 and AHT20 humidity and temperature sensors.

The driver talks to the sensor over I2C through [i2cdev](https://github.com/esp-idf-lib/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.

* [Documentation](https://esp-idf-lib.github.io/aht/)
* [Repository](https://github.com/esp-idf-lib/aht)
* [Issues](https://github.com/esp-idf-lib/aht/issues)
* [Discussions and questions](https://github.com/esp-idf-lib/core/discussions)
* [Component page at the ESP Component Registry](https://components.espressif.com/components/esp-idf-lib/aht)

## 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 instead of a blind delay, with a timeout
- Several sensors on one bus: every device has its own descriptor
- Thread safety through the i2cdev device mutex

## Supported targets

Any target with I2C support: ESP32, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2, ESP32-P4, ESP32-S2, ESP32-S3.

## Hardware

| Module pin | Connect to | Note |
| --- | --- | --- |
| VDD | 3.3 V | see the datasheet of your part for the supply range |
| 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 and is one of two values:

| ADDR pin | Address |
| --- | --- |
| GND | `0x38` (`AHT_I2C_ADDRESS_GND`) |
| VDD | `0x39` (`AHT_I2C_ADDRESS_VCC`) |

The driver rejects any other address in `aht_init_desc()`. The I2C 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.

## Installation

```sh
idf.py add-dependency "esp-idf-lib/aht"
```

## Usage

```c
#include <i2cdev.h>
#include <aht.h>

void app_main(void)
{
    ESP_ERROR_CHECK(i2cdev_init());

    aht_t dev = { 0 };
    dev.type = AHT_TYPE_AHT20;

    ESP_ERROR_CHECK(aht_init_desc(&dev, AHT_I2C_ADDRESS_GND, I2C_NUM_0,
            GPIO_NUM_21, GPIO_NUM_22));
    ESP_ERROR_CHECK(aht_init(&dev));

    while (true)
    {
        float temperature, humidity;
        esp_err_t err = aht_get_data(&dev, &temperature, &humidity);
        if (err == ESP_OK)
            printf("%.1f C, %.2f %%RH\n", temperature, humidity);
        else
            printf("read failed: %s\n", esp_err_to_name(err));

        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
```

`aht_get_data()` triggers the measurement, polls the status register until the sensor is idle and reads the result. The output values are written only when the call returns `ESP_OK`: on a timeout or a bad CRC they keep their previous content, so an error is never reported as a temperature.

The order matters: `i2cdev_init()` once per application, then `aht_init_desc()` per sensor, then `aht_init()` once per sensor. `aht_init()` waits for the power-on delay, sends the calibration command and checks the calibration bit; `aht_reset()` does the same after a soft reset.

## Timing

The delays below are taken from the driver and follow the datasheet:

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

## Examples

`examples/default` reads the sensor once per second and prints the values. The address and the chip type are selected in `menuconfig` under *Example configuration*, the SDA and SCL pins are there as well.

## Limitations

- The driver performs single measurements; the continuous mode of the sensor (`AHT_MODE_CYCLE`) is not implemented, and the field exists only for source compatibility.
- Only the two addresses above are accepted; a module with a different address wiring is not supported.
- The sensor has no unique ID, so nothing identifies it on the bus except the address.

## Support

For questions and discussions about the component, please use
[Discussions](https://github.com/esp-idf-lib/core/discussions)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

## Contributing

Please read [CONTRIBUTING.md](https://github.com/esp-idf-lib/core/blob/main/CONTRIBUTING.md)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).

## License

BSD-3-Clause, see `LICENSE`.

Links

Supports all targets

Maintainer

  • Ruslan V. Uss (@UncleRus) <unclerus@gmail.com>
To add this component to your project, run:

idf.py add-dependency "esp-idf-lib/aht^1.1.1"

download archive

Stats

  • Archive size
    Archive size ~ 18.35 KB
  • Downloaded in total
    Downloaded in total 931 times
  • Weekly Downloads Weekly Downloads (All Versions)
  • Downloaded this version
    This version: 10 times

Badge

esp-idf-lib/aht version: 1.1.1
|