AHT21B + ESP32: Pinout, Wiring Diagram and Working Code
Share
AHT21B + ESP32: Pinout, Wiring Diagram and Working Code
Quick answer: wire VCC to 3V3, GND to GND, SDA to GPIO21 and SCL to GPIO22. The AHT21B answers on I2C address 0x38. The sketch below follows the datasheet's own sequence: wait 100 ms after power-up, send 0xAC 0x33 0x00, wait 80 ms, then read the result. No library is needed.
What are the AHT21B module pins?
The AHT21B chip has four pins: VDD, SDA, GND and SCL (Aosong datasheet, pin assignment). The module brings them to a 4-pin header named VCC, GND, SDA and SCL in the listing. The silkscreen is too small to read in the catalogue photo, so the table gives names, not positions. Follow the labels on your board.
| Pin | Function | Source |
|---|---|---|
| VCC | Supply (chip: 2.2–5.5 V) | Datasheet + listing |
| GND | Ground | Datasheet + listing |
| SDA | I2C data, bidirectional | Datasheet + listing |
| SCL | I2C clock | Datasheet + listing |
How do you wire the AHT21B to an ESP32?
| AHT21B pin | ESP32 DevKit pin | Why this pin |
|---|---|---|
| VCC | 3V3 | Keeps the module's pull-ups and I/O at 3.3 V |
| GND | GND | Common ground |
| SDA | GPIO21 | Default I2C data pin |
| SCL | GPIO22 | Default I2C clock pin |
Which ESP32 pins should you avoid?
Every GPIO choice on this page follows the same rules for the 30-pin ESP32-WROOM-32 DevKit (the board Compoden stocks as the ESP32 30-pin CP2102 development board). Espressif's ESP32 GPIO reference is the source:
- Never use GPIO6–11. They connect to the module's SPI flash.
- Avoid the strapping pins GPIO0, 2, 5, 12 and 15 for sensors. Their level at reset decides boot mode and flash voltage, so a sensor holding one of them can stop the board booting. GPIO0 is not even on the 30-pin header; it only goes to the BOOT button.
- GPIO34, 35, 36 and 39 are input-only and have no internal pull-up or pull-down. They are ideal for analog inputs and useless for outputs or clocks.
- Use ADC1 (GPIO32–39) for analog readings. ADC2 pins (GPIO0, 2, 4, 12–15, 25–27) cannot be read while Wi-Fi is running.
- Leave GPIO1 and GPIO3 alone. They carry the USB serial link used for uploads and the Serial Monitor.
- ESP32 GPIOs are 3.3 V only. They are not 5 V tolerant.
What voltage does the AHT21B need?
The Aosong datasheet gives a supply range of 2.2–5.5 V (3.3 V typical) and limits SDA and SCL to VDD + 0.3 V. Powered from the ESP32's 3V3 pin, the bus sits at 3.3 V and matches the ESP32 directly. The datasheet requires 100 nF of decoupling close to the sensor and an external pull-up on SDA and SCL; the listing says the module already includes the pull-ups.
The datasheet also advises keeping SDA and SCL apart, with VDD or GND between them on long cables, to avoid crosstalk.
Working Arduino code for the AHT21B on ESP32
The sketch follows the datasheet's reading process step by step: check the status byte (bits 0x18 should be set), trigger a measurement with 0xAC 0x33 0x00, wait 80 ms, confirm the busy bit is clear, then convert the two 20-bit values with the datasheet formulas: RH = SRH / 220 × 100 and T = ST / 220 × 200 − 50.
// AHT21B temperature + humidity module + ESP32 DevKit (ESP32-WROOM-32)
// Wiring: VCC -> 3V3, GND -> GND, SDA -> GPIO21, SCL -> GPIO22
// Command sequence and formulas from the Aosong AHT21B datasheet (I2C address 0x38).
#include <Wire.h>
const int SDA_PIN = 21;
const int SCL_PIN = 22;
const uint8_t AHT_ADDR = 0x38;
uint8_t readStatus() {
Wire.requestFrom(AHT_ADDR, (uint8_t)1);
return Wire.available() ? Wire.read() : 0xFF;
}
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);
delay(100); // datasheet: wait at least 100 ms after power-on
uint8_t st = readStatus();
if (st == 0xFF) {
Serial.println("AHT21B not found at 0x38. Check VCC, GND, SDA and SCL.");
} else if ((st & 0x18) != 0x18) {
Serial.println("AHT21B status bits 0x18 not set: run the maker's initialisation routine.");
}
}
void loop() {
Wire.beginTransmission(AHT_ADDR); // trigger measurement: 0xAC 0x33 0x00
Wire.write(0xAC);
Wire.write(0x33);
Wire.write(0x00);
Wire.endTransmission();
delay(80); // datasheet: wait 80 ms
uint8_t d[7] = {0};
Wire.requestFrom(AHT_ADDR, (uint8_t)7); // status, 5 data bytes, CRC
for (int i = 0; i < 7 && Wire.available(); i++) d[i] = Wire.read();
if (d[0] & 0x80) { // status bit 7 = still measuring
delay(20);
return;
}
uint32_t srh = ((uint32_t)d[1] << 12) | ((uint32_t)d[2] << 4) | (d[3] >> 4);
uint32_t stv = (((uint32_t)d[3] & 0x0F) << 16) | ((uint32_t)d[4] << 8) | d[5];
float rh = srh * 100.0f / 1048576.0f; // RH = SRH / 2^20 * 100
float tc = stv * 200.0f / 1048576.0f - 50.0f; // T = ST / 2^20 * 200 - 50
Serial.printf("Temperature: %.2f C Humidity: %.1f %%\n", tc, rh);
delay(2000);
}
Compile check: this exact sketch compiled for the esp32:esp32:esp32 board (ESP32 Dev Module, ESP32 Arduino core 3.3.10) on 5 October 2026. A clean compile proves the code and libraries agree. It does not prove your wiring, so test it on the bench with the Serial Monitor at 115200 baud.
Common mistakes with the AHT21B and ESP32
- Reading before 100 ms after power-up. The datasheet asks for at least 100 ms first.
- Reading the data without waiting 80 ms. The busy bit will still be set.
- Two AHT21B modules on one bus. The address 0x38 is fixed; use the ESP32's second I2C controller.
- SDA/SCL on GPIO34–39. Those pins are input-only and cannot drive an I2C bus.
- Mounting it next to the ESP32 or a regulator. Their heat shows up as a temperature error.
- Using it outdoors bare. The listing notes the module is not waterproof.
Where can you get the parts?
| Item | Role | Link |
|---|---|---|
| AHT21B temperature and humidity module | I2C temperature and humidity sensor | View AHT21B module |
| ESP32 30-pin CP2102 development board | Controller with Wi-Fi and Bluetooth | View ESP32 board |
Want this sensor in a bigger build? Describe the whole device in Soldr, for example “I want to build a greenhouse climate logger with an ESP32 and an AHT21B”. Soldr returns the exact parts, a wiring map and code that compiles, all from one parts list, so the pins in the code match the pins in the wiring. Compoden stocks the parts and delivers across India.
Sources
Pins, supply range, I2C address, reading sequence and conversion formulas: Aosong AHT21B datasheet V1.0. ESP32 pin rules: Espressif ESP32 GPIO reference. Module pull-ups and pin names: Compoden AHT21B listing.
Related ESP32 wiring guides
- Vibe coding hardware: build an ESP32 project from one sentence
- TCS34725 colour sensor + ESP32
- DHT22 temperature and humidity + ESP32
- XKC-Y25-V non-contact liquid level + ESP32
- INMP441 I2S microphone + ESP32
- 5 mm LDR light sensor + ESP32
- Capacitive soil moisture sensor v2.0 + ESP32
- HX711 and 5 kg load cell + ESP32
- Water level sensor + ESP32
- AS5600 magnetic angle sensor + ESP32
Frequently asked questions
Which ESP32 pins should I use for the AHT21B?
SDA to GPIO21 and SCL to GPIO22, VCC to 3V3 and GND to GND. These are the ESP32 Arduino core's default I2C pins.
What is the AHT21B I2C address?
0x38, according to the Aosong datasheet. It is fixed, so one AHT21B per I2C bus.
Do I need a library for the AHT21B?
No. The datasheet's sequence of a status check, the 0xAC 0x33 0x00 trigger, an 80 ms wait and a 7-byte read is short enough to write with the Wire library, as the sketch shows.