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
- ENS160 Air Quality Sensor
- AHT21 Temperature & Humidity Sensor
- Arduino Board (e.g., Uno, Mega, Nano)
- Breadboard
- Jumper Wires
- 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:
- Adafruit ENS160 Library
- Adafruit AHTX0 Library
Steps to Install:
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for "Adafruit ENS160" and click Install.
- 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
- Connect the Arduino to your computer via USB.
- Open the Arduino IDE and select the correct Board and Port under the Tools menu.
- Upload the code to the Arduino by clicking Upload.
- Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to
9600
. - 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
- Indoor air quality monitoring systems
- Smart thermostats
- Environmental data logging for IoT projects
- 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!