The AHT20 is a cheap, accurate I2C temperature and humidity sensor (±2 % RH, ±0.3 °C) that has replaced the DHT11 in many newer kits.
The ESP32 DevKit V1 wires it to 3V3 and the default I2C pins D21 (SDA) and D22 (SCL) — four wires, no resistors. The sketch uses Adafruit’s AHTX0 library and prints a reading every two seconds.
Wiring
| ESP32 DevKit V1 | AHT20 temperature + humidity sensor | Wire | Notes |
|---|---|---|---|
| 3V3 | VCC | red | The module’s pull-ups go to VCC; 3V3 keeps SDA/SCL at ESP32 levels. |
| GND | GND | black | |
| D22 | SCL | yellow | |
| D21 | SDA | blue |
2.0–5.5 V part, 3.3 V board. Most AHT20 breakouts have pull-ups to their own VCC. On ESP32, Pico and other 3.3 V boards, power the module from 3.3 V so SDA/SCL never rise above 3.3 V.
Board quirks
- The AHT20 has a fixed address, 0x38, so only one fits on a bus. For two, use the ESP32’s second I2C controller:
Wire1.begin(SDA, SCL)on another pin pair such as D32/D33. - The popular blue “AHT20 + BMP280” board puts both chips on the same four pins: the AHT20 at 0x38, the BMP280 at 0x77. The Adafruit AHTX0 and BMP280 libraries run side by side on the same
Wirebus. getEvent()starts a measurement and waits about 80 ms for it. Don’t call it faster than every 2 s or the chip self-heats.- Older AHT10 modules (0x38 too) also work with this library, but they misbehave when they share the bus with other devices.
Code
// AHT20 temperature + humidity on an ESP32 DevKit V1 (I2C address 0x38).
// VCC → 3V3, GND → GND, SCL → D22, SDA → D21.
#include <Wire.h>
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
if (!aht.begin(&Wire)) {
Serial.println("No AHT20 at 0x38 — check wiring and 3V3");
while (true) delay(1000);
}
Serial.println("AHT20 ready");
}
void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp); // triggers a measurement (~80 ms) and reads it
Serial.printf("Temp %.2f °C Humidity %.1f %%\n", temp.temperature, humidity.relative_humidity);
delay(2000);
}// AHT20 temperature + humidity on an ESP32 DevKit V1 (I2C address 0x38).
// VCC → 3V3, GND → GND, SCL → D22, SDA → D21.
#include <Wire.h>
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
if (!aht.begin(&Wire)) {
Serial.println("No AHT20 at 0x38 — check wiring and 3V3");
while (true) delay(1000);
}
Serial.println("AHT20 ready");
}
void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp); // triggers a measurement (~80 ms) and reads it
Serial.printf("Temp %.2f °C Humidity %.1f %%\n", temp.temperature, humidity.relative_humidity);
delay(2000);
}Library: Adafruit AHTX0 (Adafruit) — compiled with Adafruit AHTX0 2.0.6.
Expected serial output
AHT20 ready
Temp 28.62 °C Humidity 63.8 %
Temp 28.63 °C Humidity 63.9 %AHT20 ready Temp 28.62 °C Humidity 63.8 % Temp 28.63 °C Humidity 63.9 %
Common errors
“No AHT20 at 0x38”
Check SDA/SCL and 3V3. On the combo board make sure you’re on the I2C pins, not the BMP280’s unused SDO/CSB holes. I2C scanner.
Readings stop updating after hours
An AHT20 can lock up on a noisy bus. Power-cycle it from a GPIO (through a transistor) or add 100 nF across VCC–GND at the sensor.
Compile error: Adafruit_I2CDevice.h not found
Adafruit BusIO is missing — install it next to AHTX0. Fix.