Skip to content

Updated aht20 driver to use thread safe api from idf>=5.4. (AEGHB-1062) #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/sensors/humiture/aht20/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ChangeLog

## v1.1.1 (2025-05-28)
* Replace the i2c interface with i2c_bus.

## v1.0.0 (2024-08-09)

* Added description of AHT30
Expand Down
13 changes: 7 additions & 6 deletions components/sensors/humiture/aht20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
idf_component_register(
SRCS "aht20.c"
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS "priv_include"
REQUIRES "driver"
)
idf_component_register(SRCS "aht20.c"
INCLUDE_DIRS "include")


if(CONFIG_SENSOR_INCLUDED_HUMITURE)
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u humiture_aht20_init")
endif()

include(package_manager)
cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR})
9 changes: 9 additions & 0 deletions components/sensors/humiture/aht20/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
menu "AHT20 : CONFIGURATION"

config AHT20_CHECK_CRC
bool "perform crc check on AHT20 readings"
help
CRC check to be performed on results or not?.
default n

endmenu
101 changes: 79 additions & 22 deletions components/sensors/humiture/aht20/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,89 @@
[![Component Registry](https://components.espressif.com/components/espressif/aht20/badge.svg)](https://components.espressif.com/components/espressif/aht20)

# Component: AHT20
I2C driver and definition of AHT20 humidity and temperature sensor.
I2C driver with Sensor Hub support for Aosong AHT20 humidity and temperature sensor using esp-idf.
Tested with AHT20 using ESP32 and ESP32-S3 devkits.

# Features

Temperature and humidity measurement

Thread-safe via esp-i2c-driver

CRC checksum verification (optional, only via menuconfig)

Components compatible with AHT30 and AHT21 (AHT21 is deprecated).
Configurable I2C clock speed (pre-compilation, not runtime, only via menuconfig)

See [AHT20 datasheet](http://www.aosong.com/en/products-32.html), [AHT30 datasheet](http://www.aosong.com/en/products-131.html).
Unit tested


β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Application β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AHT20 Driver β”‚
β”‚ (this component) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ i2c_bus component β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ I2C Bus β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AHT20 Temperature/Humidity β”‚
β”‚ Sensor β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


## Usage

### Initialization
> Note: Note: You need to initialize the I2C bus first.
# How To Use

All public APIs are documented in aht20.h.

## Driver

Following are the general guidelines.
```c
aht20_i2c_config_t i2c_conf = {
.i2c_port = I2C_MASTER_NUM,
.i2c_addr = AHT20_ADDRRES_0,
};
aht20_new_sensor(&i2c_conf, &handle);
//create a AHT20 device object and receive a device handle for it
// my_i2c_bus_handle here is a preintialized i2c_bus_handle_t i2c_bus object
aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht20.h

//use the previously created AHT20 device handle for initializing the AHT20
aht20_init(aht20_handle);

float_t temperature;

aht20_read_temperature( aht20_handle, &temperature);

printf("Temperature = %.2fΒ°C\n", temperature);

vTaskDelay(pdMS_TO_TICKS(2000));

float_t temperature;

aht20_read_temperature( aht20_handle, &temperature);

printf("Temperature = %.2fΒ°C\n", temperature);
```

### Read data
> The user can periodically call the aht20_read_temp_hum API to retrieve real-time data.
```c
uint32_t temp_raw, hum_raw;
float temp, hum;

aht20_read_temp_hum(aht20, &temp_raw, &temp, &hum_raw, &hum);
ESP_LOGI(TAG, "Humidity : %2.2f %%", hum);
ESP_LOGI(TAG, "Temperature : %2.2f degC", temp);
```
## How to Configure CRC
Additionally, select in menuconfig under Component Config β†’ AHT20; to use CRC(default is not used)
or change the clock speed of device (default is 100KHz).

Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz.
Higher clock speeds may cause occasional data inconsistencies depending on your board layout and wiring.

![image](https://github.com/user-attachments/assets/58a07cc9-5d87-4afe-9675-637b3e776faa)


or
In sdkconfig under Component Config β†’ AHT20,

Loading