AHT20, AHT21 Humidity & Temperature Sensors
Contents
AHT20 and AHT21 Description
The ASAIR AHT20 and AHT21 are second generation humidity and temperature sensors from the Chinese company, AOSONG Electronic Co. They are inexpensive and appear readily available (at time of writing, 2024) especially through Chinese online stores.
Fig 1 - AHT20 and AHT21 sensor chips (from datasheet)
The AHT20 is a replacement for the first generation AHT10 offering. The AHT10 had reported issues with the stability and sharing of the I2C bus that seem to be sorted with these newer sensors.
The supply voltage for both chips is 2.0V to 5.5V. They have a higher supply voltage tolerance than the original AHT10 sensor (1.8V - 3.6V).
The claimed typical current draw for the AHT20 sensor during measurement is a miserly 23 µA. Similar sensors from competitors have a measurement cycle typical current draw of 400 µA and sometimes much higher. So this very low figure quoted for the AHT20 probably represents an average current usage during a sampling period e.g. 1 measurement taken every two seconds.
Meanwhile, the AHT21 sensor datasheet states a measurement cycle typical current draw of 980 µA. This is probably the true value for both AHT20 and AHT21 chips.
Sensor Type and AccuracyRelative humidity is measured with a capacitive humidity sensor. This sensor consists of a hygroscopic dielectric material placed between a pair of electrodes that forms a small capacitor. The measured capacitance of the device changes as the dielectric material absorbs or loses moisture in proportion to the relative humidity of the surrounding air.
Temperature is most likely measured with a bandgap temperature sensor, though not explicitly stated in the datasheet. This class of sensor relies upon the temperature dependent voltage characteristic of silicon diode e.g. base-emitter junction of a bipolar junction transistor. They are more accurate and with better resolution than other measurement systems such as RTDs and thermocouples but don't have the extreme temperature range capability.
The product datasheet gives the following typical accuracies for both sensors:
- Relative Humidity: ±2%.
- Temperature : ±0.3°C.
The maximum quoted accuracies are:
- Relative Humidity (@25°C) : ±3% between RH of 20% to 80%
- Temperature: ±0.5°C between temperatures of 15°C to 55°C
These typical figures are similar to that published for the AHT10. But while they don't have a claimed higher accuracy, the AHT20 and AHT21 sensors are smaller, supposedly more reliable and tolerant of harsher environments than their first generation predecessors.
Sensor PackagingIt is probable that the only essential difference between the AHT20 and AHT21 sensors is in their packaging. They are both available as a two-sided, leadless flat SMD (Surface Mount Device) package.
The AHT20 chip measures 3mm x 3mm and sits on a tiny platform giving a total height of 1mm. The sensor hole in the top of the packaging is of a rectangular shape.
The AHT21 has the same basic square 3mm x 3mm shape. However it doesn't sit on a platform so is 0.2mm lower in height than the AHT20. The sensor hole is also different: being of a square shape.
It is fairly easy to differentiate between these two sensors with a good magnifying glass by the different shaped sensor holes even though these chips are tiny.
Each chip is laser etched with the sensor type and its product lot number. Neither chip has a programmatically available serial or ID number burnt into static memory.
Fig 4 - [L to R] Micrographs of (1) AHT20 chip and (2) AHT21 chip.
Microcontroller Connection Options
Both sensors come in a tiny SMD package that is not directly breadboard friendly. However that problem is easily rectified with both readily available on inexpensive breakout boards. These are very simple sensors and easy for the hobbyist to learn about when purchased as a breakout board.
Fig 5 - Both sides of a popular AHT20 breakout board
In addition to the sensor chip the board also contains:
- 662K 3.3V regulator.
- Decoupling capacitors for power stability.
- Logic level shifter.
- Pullup resistors (10kΩ) for the I2C pins.
Strictly speaking the voltage regulator and logic level shifter aren't required as the chip will work with a 5V power supply. The board design is identical to a popular AHT10 breakout board but with the sensor swapped out for the AHT20.
As can be seen in Fig 5 the pinout for the breakout board is simple and well labelled:
- VIN : power supply; 3.3V and 5V are fine.
- GND : Ground.
- SCL : I2C serial clock, pin19 on the micro:bit
- SDA : I2C bidirectional serial data pin20 on the micro:bit.
AHT21 based breakout boards generally have the same layout.
Accessing the AHT20/21 Sensors
The AHT20/21 sensors are quite basic and have only three user issuable commands (Initialisation, Measurement and Soft-reset) and a single read-only status byte.
Commands are written and the status byte is read via I2C. Both sensors come from the factory with a fixed address of 0x38.
Initialisation
The datasheet advises that the calibration state of the sensor should be checked before sending the command for the first reading after power-on. Reading one byte via I2C returns the Status byte. If Bit[3] of the Status byte is 1 then the chip has successfully calibrated itself at power-on.
If there is an issue with the calibration state then the sensor should be sent an Initialisation command. The Initialisation command consists of writing the command byte 0xBE followed by two parameters 0x08 and 0x00 The initialisation takes no more than 10ms to complete.
Reading Humidity and Temperature
The Measurement command consists of writing the command byte 0xAC followed by two parameters 0x33 and 0x00.
The datasheet advises to wait 80ms for the sensor to take the reading and process it. The status byte can be polled until the Busy indication bit (Bit[7]) returns 0 meaning the device is idle.
Seven bytes are read after issuing the Measurement command. The bytes in read order are:
- Byte 1 : Status byte
- Bytes 2 to 6: Raw humidity and raw temperature data
- First 20 bits : Raw humidity data
- Second 20 bits : Raw temperature data
- Byte 7 - Checksum calculated over the five data bytes
The raw values are converted to RH% and °C via formulae given in the datasheet.
The user can calculate a checksum across the 5-bytes of data read, then compare it with the checksum received from the sensor. The humidity and temperature reading can be accepted with high confidence If the checksums are equal.
Details of the checksum algorithm are provided in the datasheet and there is sample C++ code on the manufacturer's website.
Soft Reset
A soft reset can be performed on the sensor chip by writing the Soft-reset command 0xBA with no additional parameters. The reset restores default settings and is completed in no more than 20ms. There is no return value to be read after the reset has completed.
MicroPython Driver for micro:bit
An AHT20 (also works for AHT21) MicroPython driver specifically for the micro:bit has been developed as part of this series on MicroPython for the microbit. The driver webpage also provides a detailed description of the driver's methods with sample code.
The driver allows a user to easily connect and control these sensors from a micro:bit.
The AHT20 driver is implemented as a class. It's easy for a knowledgeable user to extend the class.
The AHT20 driver implements the full function set of this chip family. Included are methods that allow the user to:
- Return the Status byte from the sensor.
- Check the sensor's calibration status.
- Initialise the sensor.
- Read the relative humidity and temperature.
- Return data confidence by comparing the checksums.
- Perform a soft reset of the sensor.