How to Use the ENS160 Air Quality Sensor and AHT21 Temperature & Humidity Sensor with Arduino

How to Use the ENS160 Air Quality Sensor and AHT21 Temperature & Humidity Sensor with Arduino

The ENS160 and AHT21 are powerful sensors often paired together for environmental monitoring projects. The ENS160 is a digital air quality sensor, while the AHT21 measures temperature and humidity. Together, they can monitor indoor air quality and climate conditions. In this tutorial, we’ll show you how to use the ENS160 and AHT21 with Arduino.

What You Will Need

  1. ENS160 Air Quality Sensor
  2. AHT21 Temperature & Humidity Sensor
  3. Arduino Board (e.g., Uno, Mega, Nano)
  4. Breadboard
  5. Jumper Wires
  6. A computer with the Arduino IDE installed

Step 1: Understand the ENS160 and AHT21 Sensors

ENS160 Pinout

Pin Function
VCC Power Supply (3.3V/5V)
GND Ground
SDA I2C Data Line
SCL I2C Clock Line

AHT21 Pinout

Pin Function
VCC Power Supply (3.3V/5V)
GND Ground
SDA I2C Data Line
SCL I2C Clock Line

Both sensors communicate via the I2C interface and can share the same I2C bus.

Step 2: Wiring the Sensors to Arduino

Here’s how to wire both sensors to an Arduino Uno:

ENS160 Pin AHT21 Pin Arduino Pin
VCC VCC 5V
GND GND GND
SDA SDA A4
SCL SCL A5

Step 3: Install the Required Libraries

To simplify the interaction with these sensors, you’ll need the following libraries:

  1. Adafruit ENS160 Library
  2. Adafruit AHTX0 Library

Steps to Install:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for "Adafruit ENS160" and click Install.
  4. Search for "Adafruit AHTX0" and click Install.

Step 4: Upload the Code

Here’s an example sketch to read data from both sensors:

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

Adafruit_ENS160 ens160;
Adafruit_AHTX0 aht;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // Initialize ENS160
  if (!ens160.begin()) {
    Serial.println("ENS160 not found. Check connections.");
    while (1);
  }
  Serial.println("ENS160 initialized.");

  // Initialize AHT21
  if (!aht.begin()) {
    Serial.println("AHT21 not found. Check connections.");
    while (1);
  }
  Serial.println("AHT21 initialized.");
}

void loop() {
  // Read temperature and humidity from AHT21
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" °C");

  Serial.print("Humidity: ");
  Serial.print(humidity.relative_humidity);
  Serial.println(" %");

  // Read air quality data from ENS160
  ens160.setTempAndHumidity(temp.temperature, humidity.relative_humidity); // Provide environmental data

  Serial.print("AQI (Air Quality Index): ");
  Serial.println(ens160.getAQI());

  delay(2000); // Wait 2 seconds before next reading
}

Step 5: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code to the Arduino by clicking Upload.
  4. Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600.
  5. You should see temperature, humidity, and air quality readings displayed every 2 seconds.

Troubleshooting

  • Sensors not detected: Double-check the wiring and ensure SDA/SCL lines are correctly connected.
  • No readings: Ensure the I2C addresses of both sensors don’t conflict. Use an I2C scanner if needed.
  • Erratic values: Keep the sensors away from extreme conditions or sudden environmental changes during initialization.

Applications of ENS160 + AHT21

  1. Indoor air quality monitoring systems
  2. Smart thermostats
  3. Environmental data logging for IoT projects
  4. Air purification systems

Conclusion

You’ve successfully interfaced the ENS160 air quality sensor and AHT21 temperature and humidity sensor with Arduino. These sensors work together to provide comprehensive environmental monitoring, making them perfect for smart home or IoT applications. Experiment with the data to build your own environmental projects!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.