Contributed by WangChong, organized by Ai-Thinker
[Peripheral Porting] Ai-WB2 + AHT20 Temperature and Humidity Sensor
Peripheral Introduction

The AHT20 is a high-precision temperature and humidity sensor launched by Aosong. Compared with the previous generation AHT10, the AHT20 offers higher accuracy and more stable performance.
Key features:
High accuracy:
- Temperature measurement accuracy: ±0.3°C
- Humidity measurement accuracy: ±2% RH
Wide operating range:
- Temperature measurement range: -40°C to 85°C
- Humidity measurement range: 0% RH to 100% RH
Digital output:
- I²C communication interface, easy to integrate with various microcontrollers and development boards.
Low power consumption:
- Suitable for low-power applications, such as battery-powered devices.
Fast response time:
- Fast response, suitable for real-time temperature and humidity monitoring.
Calibration and compensation:
- The sensor is calibrated at the factory and does not require recalibration during use.
Package:
Compact package, easy to integrate into various applications.
Application scenarios: The AHT20 is widely used in scenarios that require high-precision temperature and humidity monitoring, such as:
- Smart home (e.g., air quality monitors, smart air conditioners)
- Industrial automation
- Weather stations
- Medical devices
- Agricultural environment monitoring
The I²C interface of the AHT20 makes it easy to connect to various common microcontrollers, and it provides rich library and driver support, making it convenient for developers to integrate and use quickly.
Porting Process
According to the datasheet, the AHT20 communicates over I2C, and since we are using the WB2, we need to confirm the IIC interface pins of the WB2 from the schematic. Since I am using the WB2-12F development board, the corresponding schematic can be found under the WB2 section of the Ai-Thinker community. Meanwhile, according to the Bouffalo Lab official GPIO function multiplexing, you can find which pins support IIC.

We copy any project from SDK/application/iot_solution, delete all the code inside, modify the corresponding project name, and rename it demo_aht20.
According to the official AHT20 documentation, we write the driver functions to read the humidity and temperature data of the AHT20 sensor. If you copied an IIC-related project, you can use the previous IIC driver.

Attachments: 
AHT20 Product Specification (Chinese Version) B1.pdf(1.7 MB, downloads: 8)
The code is as follows:
Click to expand full code
#include <stdio.h>
#include <FreeRTOS.h>
#include <task.h>
#include <hosal_i2c.h>
#include <bl_gpio.h>
#include <blog.h>
#define AHT20_DEFAULT_ADDR 0x38 // AHT20 I2C地址
#define AHT20_CMD_INIT 0xE1 // AHT20初始化命令
#define AHT20_CMD_TRIGGER 0xAC // AHT20触发测量命令
#define AHT20_CMD_SOFT_RESET 0xBA // AHT20软复位命令
// AHT20 传感器数据的结构体
#pragma pack(1)
struct aht20_data
{
uint8_t status;
uint8_t humidity_high;
uint8_t humidity_mid;
uint8_t humidity_low;
uint8_t temp_high;
uint8_t temp_mid;
uint8_t temp_low;
};
#pragma pack()
// 初始化AHT20传感器
static void aht20_init(hosal_i2c_dev_t *i2c)
{
uint8_t init_cmd[3] = {AHT20_CMD_INIT, 0x08, 0x00}; // 初始化命令
hosal_i2c_master_send(i2c, AHT20_DEFAULT_ADDR, init_cmd, sizeof(init_cmd), 100); // 发送初始化命令
}
// 触发AHT20测量
static void aht20_trigger_measurement(hosal_i2c_dev_t *i2c)
{
uint8_t trigger_cmd[3] = {AHT20_CMD_TRIGGER, 0x33, 0x00}; // 触发测量命令
hosal_i2c_master_send(i2c, AHT20_DEFAULT_ADDR, trigger_cmd, sizeof(trigger_cmd), 100); // 发送测量命令
}
// 主函数
int main(void)
{
// I2C 设备配置
static hosal_i2c_dev_t i2c0 = {
.config = {
.address_width = HOSAL_I2C_ADDRESS_WIDTH_7BIT, // 7位I2C地址模式
.freq = 100000, // 100kHz I2C速率
.mode = HOSAL_I2C_MODE_MASTER, // 主模式
.scl = 12, // SCL连接到GPIO 12
.sda = 3, // SDA连接到GPIO 3
},
.port = 0, // I2C端口号
};
hosal_i2c_init(&i2c0); // 初始化I2C
aht20_init(&i2c0); // 初始化AHT20传感器
for (;;)
{ // 无限循环,定时读取温湿度数据
aht20_trigger_measurement(&i2c0); // 触发AHT20进行一次测量
vTaskDelay(portTICK_RATE_MS * 100); // 延时100ms,等待测量完成
struct aht20_data data; // 数据存储结构体
hosal_i2c_master_recv(&i2c0, AHT20_DEFAULT_ADDR, (uint8_t *)&data, sizeof(data), 100); // 接收传感器返回的数据
if ((data.status & 0x80) == 0)
{ // 检查数据是否有效
uint32_t humidity_raw = ((uint32_t)data.humidity_high << 12) | ((uint32_t)data.humidity_mid << 4) | (data.humidity_low >> 4); // 提取湿度数据
uint32_t temp_raw = (((uint32_t)data.temp_high & 0x0F) << 16) | ((uint32_t)data.temp_mid << 8) | data.temp_low; // 提取温度数据
float humidity = (humidity_raw * 100.0) / 1048576.0; // 转换为实际湿度
float temperature = (temp_raw * 200.0) / 1048576.0 - 50.0; // 转换为实际温度
blog_info("Temperature: %.2f C, Humidity: %.2f %%\r\n", temperature, humidity); // 输出温湿度数据
}
else
{
blog_error("AHT20 Data Invalid\r\n"); // 数据无效时输出错误信息
}
vTaskDelay(portTICK_RATE_MS * 1000); // 延时1秒,进入下一次测量
}
return 0;
}Flash and Verify


My AHT20 temperature sensor here may have a problem: the temperature drifts up and down too much. If any expert has this sensor, please help me verify whether this code can work properly with your sensor.
I won't refactor it into library functions here, so that it can be compiled and flashed directly for verification.
Project Files
Attachments: 
demo_aht20.zip(307.34 KB, downloads: 9)
How to Use
After unzipping the project, copy it to the SDK/application/iot-solution directory, then enter the project root directory and run make -j to compile.

