Adafruit STEMMA QT AHT20 Temperature & Humidity Sensor User Manual

Adafruit AHT20 Temperature & Humidity Sensor
Created by Kattni Rembor
Last updated on 2021-02-21 09:04:23 AM EST
2 3 5 5 5 6
6
6 7 7 9
10
10 10 11 11 12
12 13 14
14 14 14
Guide Contents Overview Pinouts Power Pins I2C Logic Pins Arduino
Wiring
Installation Load Example Example Code Arduino Docs Python & CircuitPython
CircuitPython Microcontroller Wiring Python Computer Wiring CircuitPython Installation of AHT20 Library Python Installation of AHT20 Library CircuitPython & Python Usage
Full Example Code Python Docs Downloads
Files Schematic Fab Print
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 2 of 15
Overview
The AHT20 is a nice but inexpensive temperature and humidity sensor from the same folks that brought
us the DHT22 (https://adafru.it/sb4). 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.
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 3 of 15
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 (https://adafru.it/Fpw) compatible STEMMA QT (https://adafru.it/Ft4) JST SH connectors that allow
you to get going without needing to solder. Just use a STEMMA QT adapter cable (https://adafru.it/FA-),
plug it into your favorite microcontroller or Blinka supported SBC and you're ready to rock!
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 4 of 15
Pinouts
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 (https://adafru.it/Ft4) - These connectors allow you to connectors to dev boards
with STEMMA QT connectors or to other things with various associated
accessories (https://adafru.it/Ft6)
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 5 of 15
Arduino
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:
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 6 of 15
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:
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 7 of 15
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 8 of 15
Arduino Docs
Arduino Docs (https://adafru.it/LxB)
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 9 of 15
Python & CircuitPython
It's easy to use the AHT20 sensor with CircuitPython and the Adafruit CircuitPython
AHT20 (https://adafru.it/QDP) 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 (https://adafru.it/BSN).
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 (https://adafru.it/BSN).
Here's the Raspberry Pi wired with I2C:
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 10 of 15
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 (https://adafru.it/QDP) library on your CircuitPython
board.
First make sure you are running the latest version of Adafruit CircuitPython (https://adafru.it/Amd) 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 (https://adafru.it/uap). Our
CircuitPython starter guide has a great page on how to install the library bundle (https://adafru.it/ABU).
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 (https://adafru.it/Awz)so you are at the CircuitPython >>> prompt.
Python Installation of AHT20 Library
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 11 of 15
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 (https://adafru.it/BSN)!
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
import time import board import adafruit_ahtx0
# Create the sensor object using I2C sensor = adafruit_ahtx0.AHTx0(board.I2C())
while True: print("\nTemperature: %0.1f C" % sensor.temperature) print("Humidity: %0.1f %%" % sensor.relative_humidity) time.sleep(2)
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 12 of 15
Python Docs
Python Docs (https://adafru.it/LA7)
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 13 of 15
Downloads
Files
AHT20 Datasheet (https://adafru.it/LAm)
Fritzing object in the Adafruit Fritzing Library (https://adafru.it/LAn)
EagleCAD PCB files on GitHub (https://adafru.it/LAo)
Schematic
Fab Print
© Adafruit Industries https://learn.adafruit.com/adafruit-aht20 Page 14 of 15
© Adafruit Industries Last Updated: 2021-02-21 09:04:23 AM EST Page 15 of 15
Loading...