Humidity sensor, AHT20
Temperature and humidity over I2C at 0x38, one 80 ms measurement at a time.
- 4 pins
- 2 settings
- In 2 projects
- In 1 lesson
The Humidity sensor, AHT20 in brief
What it is, and every setting the properties panel gives it.
The AHT20 is the temperature and humidity sensor that replaces the DHT11 in newer kits. It measures the same two things more accurately, and it does it over I2C at address 0x38, so it shares two wires with a display instead of needing a pin and a timing-critical protocol of its own. It has no registers: a write is a command and a read is the status byte followed by the data. Mokxi models the sequence as ASAIR’s datasheet gives it. Power-on takes up to 20 ms, during which the address is not acknowledged. A measurement is triggered with 0xAC 0x33 0x00, takes 80 ms and sets the busy bit the whole time, and the six bytes that follow hold two 20-bit counts and a CRC-8. Humidity is the count over 2 to the 20th times 100 percent, and temperature is the count over the same times 200, less 50. Two sliders move the weather. The accuracy figures, the response time and self-heating are not modeled.
- Family
- Sensors
- Pins
- 4
- temperature
- 22
- humidity
- 45
4 pins
Named as the editor names them, so a wire lands where you expect it.
How to use an AHT20 sensor with an Arduino and an LCD
The AHT20 has four pins: VCC, GND, SDA and SCL. The module carries the pull-ups. In the built-in project it goes to the Uno’s 5 V and to A4 and A5, and a 16x2 I2C LCD sits on the same two wires, because each device answers only its own address, 0x38 for the sensor and 0x27 for the display. A TTP223 touch pad on pin 2 flips the reading between Celsius and Fahrenheit, on the rising edge so a held finger flips it once.
Adafruit_AHTX0 asks for a reading and waits the 80 ms the chip takes to make one. The sketch takes the numbers in hundredths, getTemperatureCentis() and getHumidityCentis(), because Fahrenheit is then an integer sum, F times 100 equals C times 100 times 9 over 5 plus 3200, and the Uno has no floating point hardware. Sums on the library’s float readings link only on the Uno R4, so use the centis calls elsewhere.
Do not read the chip straight after the trigger. A read before 80 ms has the busy bit set and returns the old data, and the datasheet asks for a reading no more often than every 2 seconds because of self-heating, which the simulation does not model. A slider moved between measurements shows up in the next one, never the one already taken. The chip powers up calibrated, reading 0x18 when idle.
The chip runs from 2.0 V to 5.5 V, so a 3.3 V ESP32 or Pico powers it directly. Floats print on every board, but arithmetic on them links only on the Uno R4.
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(115200);
if (!aht.begin()) {
Serial.println("Could not find AHT? Check wiring");
while (1) delay(10);
}
}
void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
Serial.print(temp.temperature);
Serial.print(" C ");
Serial.print(humidity.relative_humidity);
Serial.println(" %");
delay(2000);
}Common Humidity sensor, AHT20 errors and fixes
Questions and answers
Is the AHT20 a replacement for the DHT11?
For most kits, yes. It measures temperature and humidity more accurately and sits on I2C, so it needs two wires shared with other devices instead of its own pin and protocol.
What is the AHT20’s I2C address?
0x38, fixed. A display at 0x27 or 0x3C can share the bus with it.
How long does a measurement take?
80 ms from the trigger 0xAC 0x33 0x00, with the busy bit set the whole time. Reading sooner returns stale data.
Which library reads the AHT20?
Adafruit_AHTX0. getEvent() fills a temperature and a humidity event, and getTemperatureCentis() and getHumidityCentis() return the same reading in hundredths.
What is true about the Humidity sensor, AHT20, here
In its help article’s own words: what the simulation gets right, and where it stops.
Modeled
The command set, the 20 ms power-on and reset times, the 80 ms measurement with its
busy bit, the bit packing and the CRC, all from the ASAIR datasheet (V1.1). The chip
powers up calibrated (status bit 3 set), as the parts sold do, and reads 0x18 when
idle.
Not modeled
The accuracy figures (±2 %RH and ±0.3 °C typical), the 8 s humidity response time, and self-heating, which is why the sheet asks for a reading no more often than every 2 seconds.
From AHT20 temperature and humidity sensor, in full.
See the Humidity sensor, AHT20 in a project
2 built-in projects use it. Each opens in the editor, already wired.
Where it turns up in a lesson
One lesson places it in its own circuit.
The boards it is shown on
This board has a built-in project with the Humidity sensor, AHT20 in it.
Arduino libraries for the Humidity sensor, AHT20
Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.
The rest of the bench
Every one of these is drawn and simulated the same way.
Wire up the Humidity sensor, AHT20
Open the editor and push it into the breadboard. It is free, and it runs on your own machine.