The AHT20 is a nice but inexpensive temperature and humidity sensor from the same folks that brought us the DHT22. You can take sensor readings as often as you like, and it uses standard I2C so its super easy to use with any Arduino or Linux/Raspberry Pi board.

This sensor has a typical accuracy of +- 2% relative humidity, and +-0.3 °C. There is only one I2C address so its not a good option when you need multiple humidity sensors.

As with all Adafruit breakouts, we've done the work to make this handy temperature-and-humidity super easy to use. We've put it on a breakout board with the required support circuitry and connectors to make it easy to work with and is now a trend we've added SparkFun Qwiic compatible STEMMA QT JST SH connectors that allow you to get going without needing to solder. Just use a STEMMA QT adapter cable, plug it into your favorite microcontroller or Blinka supported SBC and you're ready to rock!

Power Pins

The sensor on the breakout requires between a 2.7V and 5.5V, and can be easily used with most microcontrollers from an Arduino to a Feather or something else.

  • VIN - this is the power pin.  To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V
  • GND - common ground for power and logic

I2C Logic Pins

  • SCL - I2C clock pin, connect to your microcontrollers I2C clock line. The logic level is the same as VIN and it has a 10K pullup already on it.
  • SDA - I2C data pin, connect to your microcontrollers I2C data line. The logic level is the same as VIN. and it has a 10K pullup already on it.
  • STEMMA QT - These connectors allow you to connectors to dev boards with STEMMA QT connectors or to other things with various associated accessories

Wiring

Connecting the AHT20 to your Feather or Arduino is easy:

  • If you are running a Feather (3.3V), connect Feather 3V to board VIN
  • If you are running a 5V Arduino (Uno, etc.), connect Arduino 5V to board VIN
  • Connect Feather or Arduino GND to board GND
  • Connect Feather or Arduino SCL to board SCL
  • Connect Feather or Arduino SDA to board SDA

The final results should resemble the illustration above, showing an Adafruit Metro development board.

Installation

You can install the Adafruit AHTx0 Library for Arduino using the Library Manager in the Arduino IDE:

Click the Manage Libraries ... menu item, search for Adafruit AHTx0, and select the Adafruit AHTx0 library:

Then follow the same process for the Adafruit BusIO library.

Load Example

Open up File -> Examples -> Adafruit AHTx0 -> adafruit_aht_test and upload to your Arduino wired up to the sensor.

Upload the sketch to your board and open up the Serial Monitor (Tools->Serial Monitor). You should see the the values for temperature and humidity.

Example Code

The following example code is part of the standard library, but illustrates how you can retrieve sensor data from the AHT20 for the temperature and humidity values:

#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  Serial.println("Adafruit AHT10/AHT20 demo!");

  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
  Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");

  delay(500);
}

You should get something resembling the following output when you open the Serial Monitor at 115200 baud:

It's easy to use the AHT20 sensor with CircuitPython and the Adafruit CircuitPython AHT20 module.  This module allows you to easily write Python code that reads the temperature and humidity from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a AHT20 to your board exactly as follows. Here is an example of the AHT20 wired to a Feather using I2C:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA to sensor SDA

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCL
  • Pi SDA to sensor SDA

CircuitPython Installation of AHT20 Library

You'll need to install the Adafruit CircuitPython AHT20 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

Copy the following files from the bundle to the lib folder on your CIRCUITPY drive:

  • adafruit_ahtx0.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_ahtx0.mpy, and adafruit_bus_device file and folder copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of AHT20 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-ahtx0

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the temperature and humidity from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

import board
import adafruit_ahtx0

sensor = adafruit_ahtx0.AHTx0(board.I2C())

Now you're ready to read values from the sensor using these properties:

  • temperature - The temperature in Celsius.
  • humidity - The humidity in percent.

For example to print temperature and humidity values:

print("\nTemperature: %0.1f C" % sensor.temperature)
print("Humidity: %0.1f %%" % sensor.relative_humidity)

That's all there is to using the AHT20 sensor with CircuitPython!

Full Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Basic `AHTx0` example test
"""

import time
import board
import adafruit_ahtx0

# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C()  # uses board.SCL and board.SDA
sensor = adafruit_ahtx0.AHTx0(i2c)

while True:
    print("\nTemperature: %0.1f C" % sensor.temperature)
    print("Humidity: %0.1f %%" % sensor.relative_humidity)
    time.sleep(2)

This guide was first published on Jun 05, 2020. It was last updated on Jun 05, 2020.