Image of eBits Academy airquality illustration

Writing Code to Collect Sensor Data - Air Quality Monitoring Station Series, Episode 10

  • March 11, 2025
  • |
  • Teofil Corad



Lesson objectives

In this lesson, we will explore the AHT10 temperature & humidity sensor, an important component in your IoT air quality monitoring station. By the end of the lesson, you will understand what the AHT10 sensor is, how it works, and why it is suitable for measuring environmental data in this project.

What is the AHT10 sensor?

The AHT10 is a high-precision, fully calibrated digital sensor designed to measure both temperature and humidity. It combines accurate measurements with a compact form factor, making it ideal for IoT applications.

List of required components:

 

Key features of the AHT10:

  • Temperature range: -40°C to 85°C with an accuracy of ±0.3°C.
  • Humidity range: 0% to 100% relative humidity with an accuracy of ±2% rH.
  • I2C interface: Uses the I2C protocol for communication, simplifying wiring and programming when connecting to the ESP32.
  • Low power consumption: Suitable for battery-powered IoT devices and enables long-term operation in remote environments.

How does the AHT10 sensor work?

The AHT10 sensor works by using a specialized polymer capacitor to measure relative humidity and a thermistor to measure temperature. Here is a brief overview of how it works:

  • Measuring humidity: The sensor detects changes in capacitance as the moisture level in the air changes, allowing relative humidity to be calculated.
  • Measuring temperature: The sensor uses a thermistor, a type of resistor whose value changes with temperature, to detect the ambient temperature accurately.

The sensor then digitizes these values and sends them to the ESP32 via the I2C interface.

Why is the AHT10 sensor ideal for our project?

The AHT10 sensor is an excellent choice for our IoT air quality monitoring station because of its combination of precision, low power consumption, and easy integration. Here are some reasons why it is a perfect fit:

  1. Accurate measurements: The sensor provides reliable data, which is essential for environmental monitoring.
  2. Compact design: Its small size makes it easy to integrate the sensor into any IoT project without taking up much space.
  3. Simple communication: Using the I2C protocol makes connecting to the ESP32 straightforward, since only two wires are required for communication, reducing circuit complexity.

How does the AHT10 fit into our project?

The sensor data will be processed by the ESP32 and either displayed on a TFT screen or sent to a web server for remote access.

The sensor's role in the project:

  • Data collection: The AHT10 will continuously monitor the surroundings and record temperature and humidity values.

What happens next?

Key takeaways:

  • The AHT10 is an accurate and power-efficient sensor for measuring temperature and humidity, making it ideal for our IoT air quality monitoring station.
  • It communicates with the ESP32 via the I2C protocol, simplifying wiring and programming.
  • In the project, the sensor will collect real-time environmental data that is displayed locally and shared via WiFi.

About the measurements: The station in this series measures temperature and relative humidity using the AHT10. These are indoor climate readings, not direct measurements of CO2, VOCs, or particles. The series name “air quality monitoring” should be understood with this limitation.

Write and run the sensor code

Install Adafruit AHTX0 and the dependencies suggested by the Library Manager. The Adafruit_AHTX0 library was introduced in lesson 9. Connect the AHT10 as in lesson 7: VCC to 3.3V, GND to GND, SDA to GPIO 21, and SCL to GPIO 22. These pins apply to the original ESP32/TTGO T-Display board in this series; check the pinout for other board variants.

#include <Wire.h>
#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  if (!aht.begin(&Wire)) {
    Serial.println("AHT10 not found. Check power and I2C wiring.");
    while (true) { delay(100); }
  }
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" C");
  Serial.print("Relative humidity: ");
  Serial.print(humidity.relative_humidity);
  Serial.println(" %");
  delay(2000);
}

Create a sketch, paste the code, select the correct board and port, and upload. Open the Serial Monitor at 115200 baud. Temperature in °C and relative humidity in % appear every 2 seconds. If the sensor is not found, the program stops after an error message: check the power supply and I2C wiring.

The code uses begin() for initialization and getEvent(&humidity, &temp) for readings. The order is humidity first, then temperature. The API and field names were checked against Adafruit’s official example. Official Adafruit AHTX0 example.

In the next lesson, episode 11, we cover troubleshooting in Arduino IDE. Episode 12 introduces TFT screens.

Leave a comment

Please note, comments need to be approved before they are published.