Temperature and Humidity | AHT20
Links and Resources #
Connecting the Sensor #
These sensors come with a very handy connector that allows us to use it without any soldering or having to use the breadboard.
Using the Qwiic/STEMMA QT connector #
Just use a Qwiic/STEMMA QT cable to connect the sensor to your board. It does not matter which of the connectors you use, they are all connected together.
Connecting directly to the pins #
Sometimes you might not have the connector on your microcontroller so you need to wire it up manually. This is also quite simple:
- 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, connect to GND on your board
- 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.
Recommended Library #
Use the Adafruit AHTX0 Library
I2C Bus on the Uno R4 boards #
Please note! The default examples do not work directly with the Qwiic connectors on the Arduino Uno R4 WiFi boards. This due to the fact that the R4 boards have a different I2C port connected to the Qwiic connectors. We need to somehow configure the library for each sensor to use
Wire1
I2C bus instead of the default one (Wire
). Each library does this slightly differently, I try to provide the details for all the sensors that you have in your Physical Computing kit, but for many other devices, you need to figure this out on your own.This is quite often done in the
begin()
method in the libraries.Wire1.begin(); libraryName.begin(&Wire1);
Sometimes there is a specific method to switch the I2C bus.
Wire1.begin(); libraryName.begin(); libraryName.setBus(&Wire1);
Selecting Wire1 I2C Bus with the Adafruit AHTX0 Library #
For the Adafruit AHT20 sensor breakout boards, you do it like this:
aht20.begin(&Wire1);
The
&
means that we are passing a reference to the address of the variable. This is a pretty confusing and complicated topic, but you can read up on it:
Full example code #
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(115200);
Serial.println("Adafruit AHT10/AHT20 demo!");
if (! aht.begin(&Wire1)) {
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);
}