Source: https://mokxi.com/libraries/adafruit-ahtx0
Updated: 2026-11-11

Libraries / Sensors

Arduino library · Sensors

# Adafruit AHTX0: the AHT20 temperature and humidity sensor

The AHT20 is a small I2C temperature and humidity chip found on many low-cost modules, often sharing a board with a BMP280. Adafruit’s AHTX0 library reads it at its one fixed address, 0x38, and returns two sensors_event_t structs per measurement: one for humidity, one for temperature, in the Unified Sensor style.

Open the example in the editor See the calls

Built in on Mokxi: #include <Adafruit_AHTX0.h> and it builds, with nothing to install. Free, and no account needed.

## What Adafruit AHTX0 does

Getting a reading out takes a short ritual that the library runs for you. It sends the trigger command, polls the busy bit every 10 ms while the chip works (about 80 ms), then unpacks two 20-bit counts into percent and degrees. begin() also checks the calibrated bit in the status byte and sends the chip’s initialization command when that bit is clear, a step that hand-written drivers often forget.

The DHT20 is an AHT20 inside a DHT-style case and has its own library page for Rob Tillaart’s driver; this page is for sketches written against Adafruit’s calls. In Mokxi the aht20 part takes its values from sliders. The example is the touch climate display: an AHT20 and a 16x2 LCD on the same two wires, and a touch pad that flips between Celsius and Fahrenheit using getTemperatureCentis(), a Mokxi addition that returns hundredths of a degree.

Library Manager name | Adafruit AHTX0

Author | Adafruit

Include | #include <Adafruit_AHTX0.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Humidity sensor, AHT20

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_AHTX0.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit AHTX0" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit AHTX0 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.6, so a sketch written here builds there too.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Adafruit_AHTX0 aht

Creates the driver. Unlike most Adafruit drivers, the bus and the address are passed to begin(), not the constructor.

### aht.begin()

Waits out the power-up time, resets the chip and makes sure it is calibrated. Returns false when nothing answers at 0x38. aht.begin(&Wire1) uses a second bus.

### aht.getEvent(&humidity, &temp)

Takes one measurement and fills both events: humidity.relative_humidity in percent and temp.temperature in degrees Celsius. Either pointer may be NULL.

### aht.getStatus()

The raw status byte: bit 7 is busy and bit 3 is calibrated. 0xFF means the chip did not reply.

### aht.getTemperatureSensor()

An Adafruit_Sensor for temperature alone, for code that wants one getEvent(&event) per value. getHumiditySensor() is the other half; each takes its own measurement.

### aht.getTemperatureCentis()

Mokxi’s own: the last measurement in hundredths of a degree, for sums without floats. getHumidityCentis() does the same for humidity. The upstream library has neither.

## Example: Touch climate display

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · Touch climate display: the project page

## Common errors and how to fix them

### Could not find AHT

Nothing replied at 0x38 within half a second. Check the two bus wires and the supply; on a combined AHT20 and BMP280 board both chips hang off the same pins, so a scanner should list two addresses.

### error: 'class Adafruit_AHTX0' has no member named 'readTemperature'

That call comes from the DHT and SHT31 libraries. AHTX0 reads through getEvent() only, so declare two sensors_event_t variables and read their fields.

### The humidity shown never moves while the temperature does

The sketch is printing a field from the wrong event, often temp twice. Read relative_humidity from the humidity event and temperature from the temp event after a single getEvent().

## Questions and answers

### Can two AHT20 sensors share one I2C bus?

Not directly: the address is fixed at 0x38. Put the second one on another bus with begin(&Wire1) where the board has one, or behind an I2C multiplexer.

### Why does the touch climate project use hundredths instead of floats?

The sensor library, two I2C drivers and the LCD font fill most of the flash an Uno example has, so the sketch does its Fahrenheit sum in whole numbers from getTemperatureCentis().

## Projects that use Adafruit AHTX0

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Solar sensor node A 6 V panel charges an 18650 through a TP4056, an MCP1700 makes 3.3 V, and a Pro Mini reports temperature, humidity, the battery and the panel every few seconds.

## Keep going

### Learn

- I2C Explained: Addresses, Acknowledges and Pull-Ups
- Arduino DHT22 and DHT11: Temperature and Humidity

### Parts

- Humidity sensor, AHT20

### More sensors libraries

- DHT sensor library
- DHT20
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280

Every built-in library · Open the editor

## Try Adafruit AHTX0 without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
