Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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})
18 changes: 18 additions & 0 deletions components/sensors/humiture/aht20/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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


config AHT20_I2C_CLK_SPEED
int "I2C clock speed"
default 100000
range 1 400000
help
Clock speed used for i2c communication by AHT20 device.
Limited to maximum of 400KHZ.

endmenu
115 changes: 93 additions & 22 deletions components/sensors/humiture/aht20/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,103 @@
[![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)

Configurable I2C clock speed (pre-compilation, not runtime, only via menuconfig)

Components compatible with AHT30 and AHT21 (AHT21 is deprecated).
Unit tested


┌───────────────────┐
│ Application │
└────────┬──────────┘
┌───────────────────┐
│ AHT20 Driver │
│ (this component) │
└────────┬──────────┘
┌───────────────────┐
│ i2c_bus component │
└────────┬──────────┘
┌───────────────────┐
│ I2C Bus │
└────────┬──────────┘
┌────────────────────────────┐
│ AHT20 Temperature/Humidity │
│ Sensor │
└────────────────────────────┘

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


## Usage
# How To Use

### Initialization
> Note: Note: You need to initialize the I2C bus first.
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;
## Senosr Hub

Following are the general guidelines.
```

/*create a sensor with specific sensor_id and configurations*/
iot_sensor_create("aht20", &sensor_config, &sensor_handle);

/*register handler with sensor's handle*/
iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL);

/*start the sensor, data ready events will be posted once data is acquired successfully*/
iot_sensor_start(sensor_handle);

```

## How to Configure CRC and I2C clock speed
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/fc8680fb-1567-477c-92f8-52dd126e6f9d)

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);
```
or
In sdkconfig under Component Config → AHT20,
![image](https://github.com/user-attachments/assets/1f9612df-8d73-4ad1-bec7-75cbe6ed327a)
Loading