AHT20 Temperature and Humidity Sensor
About 2 min
AHT20 Temperature and Humidity Sensor
The AHT20 is a low-cost I2C temperature and humidity sensor, suitable as a more stable replacement for DHT11/DHT22 for indoor environment monitoring. The current driver is a lightweight implementation that does not depend on additional third-party libraries, making it available for slim ESP32 firmware.
Wiring
| AHT20 | ESP32 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GPIO21 |
| SCL | GPIO22 |
Default address is 0x38.
Configuration
Method 1: Web Interface Configuration (Recommended)
Before saving AHT20 configuration, verify the I2C bus, address, and sampling interval.
Step 1: Navigate to Peripheral Management Page
- Open a browser and navigate to the ESP32 IP address
- After logging in, click Peripheral Configuration in the left menu
Step 2: Add AHT20 Peripheral
Click the Add Peripheral button
Fill in the configuration:
Field Value Description Peripheral ID aht20_i2cUnique identifier Name AHT20 Temp & HumidityDisplay name Peripheral Type Generic Sensor (type: 38) I2C sensor SDA Pin 21I2C data pin SCL Pin 22I2C clock pin I2C Address 0x38Default address Sampling Interval 50005 seconds Click Save
Step 3: Verify Configuration
- Find the newly added peripheral in the peripheral list
- Click the Enable toggle
- Wait 5 seconds and check temperature and humidity data
💡 Tip: AHT20 is a low-cost I2C alternative to DHT11/DHT22 with better accuracy
Method 2: JSON Configuration File Import
{
"id": "aht20_i2c",
"name": "AHT20 Temp & Humidity",
"type": 2,
"enabled": false,
"pinCount": 2,
"pins": [21, 22, 255, 255, 255, 255, 255, 255],
"params": {
"frequency": 100000,
"address": 0,
"isMaster": true
}
}Peripheral Execution Integration
Web Interface Configuration Steps
Create Humidity Collection and Control Rule
- Switch to the Peripheral Execution Management tab
- Click the Add Rule button
- Configure a timer trigger:
- Trigger Type: Timer Trigger
- Execution Interval: 30 seconds
- Add action:
- Action 1: Read humidity
- Action Type: Sensor Read
- Target Peripheral: aht20_i2c
- Data Field: humidity
- Action 1: Read humidity
- Enable Report Data After Execution
- Click Save
Create Low Humidity Humidifier Rule
- Create a new rule
- Configure an event trigger:
- Trigger Type: Event Trigger
- Event ID: ds:aht20_i2c_humidity
- Comparison: Less Than
- Comparison Value: 40 (%)
- Add action:
- Action Type: High Level
- Target Peripheral: relay_humidifier (humidifier relay)
- Click Save
💡 Tip: Event ID format is
ds:{peripheralID}_{fieldName}
JSON Configuration Example
{
"id": "exec_aht20_humidity_read",
"name": "AHT20 Humidity Collection",
"enabled": false,
"triggers": [
{
"triggerType": 1,
"timerMode": 0,
"intervalSec": 30
}
],
"actions": [
{
"targetPeriphId": "aht20_i2c",
"actionType": 19,
"actionValue": "{\"periphId\":\"aht20_i2c\",\"sensorCategory\":\"AHT20\",\"dataField\":\"humidity\",\"sensorLabel\":\"Humidity\",\"unit\":\"%\",\"decimalPlaces\":1,\"driverParams\":{\"addr\":\"0x38\",\"sda\":21,\"scl\":22}}",
"useReceivedValue": false,
"syncDelayMs": 0,
"execMode": 0
}
],
"reportAfterExec": true
}Low Humidity Humidifier Relay
{
"id": "exec_aht20_dry_relay",
"name": "AHT20 Low Humidity Humidifier",
"enabled": false,
"triggers": [
{
"triggerType": 4,
"eventId": "ds:aht20_i2c_humidity",
"operatorType": 3,
"compareValue": "40"
}
],
"actions": [
{
"targetPeriphId": "relay_humidifier",
"actionType": 0,
"actionValue": "",
"useReceivedValue": false,
"syncDelayMs": 0,
"execMode": 0
}
],
"reportAfterExec": true
}Available Data Fields
| dataField | Meaning | Unit |
|---|---|---|
temperature | Temperature | ℃ |
humidity | Relative Humidity | % |
