Experiment-5
Experiment Title: Arduino Air Quality Monitoring System.
An Air Quality Index (AQI) sensor measures the concentration of various pollutants in the
air to determine overall air quality. These sensors typically detect gases like carbon
monoxide (CO), nitrogen dioxide (NO₂), sulfur-dioxide (SO₂), ozone (O₃), and particulate
matter (PM) such as PM2.5 and PM10. The readings are then converted into an AQI score,
which indicates how safe or hazardous the air is for human health.
Components:
Components Required for this Project are:
• Arduino board
• MQ135 gas sensor for detecting various gases.
• DHT11 temperature and humidity sensor
• OLED display for visual output
• Breadboard and jumper wires
Circuit Diagram:
Fig-1:Circuit diagram of Air Quality Monitoring System
Code:
#include <SPI.h>
#include <DHT.h>
#define sensor A0
#define DHTPIN 2 // Pin where the DHT sensor is connected
#define DHTTYPE DHT11 // Define the type of DHT sensor used (DHT11 or DHT22)
int gasLevel = 0;
String quality = "";
DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
void setup() {
Serial.begin(9600);
pinMode(sensor, INPUT);
dht.begin(); // Initialize the DHT sensor
}
void sendSensor() {
// Function to read temperature and humidity from the DHT sensor
float h = dht.readHumidity(); // Read humidity
float t = dht.readTemperature(); // Read temperature
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return; // Return if reading fails
}
// Output to Serial Monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
}
void air_sensor() {
gasLevel = analogRead(sensor);
if (gasLevel < 151) {
quality = "GOOD!";
} else if (gasLevel >= 151 && gasLevel < 200) {
quality = "Poor!";
} else if (gasLevel >= 200 && gasLevel < 300) {
quality = "Very bad!";
} else if (gasLevel >= 300 && gasLevel < 500) {
quality = "Toxic!";
} else {
quality = "Toxic";
}
// Output to Serial Monitor
Serial.print("Gas Level: ");
Serial.print(gasLevel);
Serial.print(" - Quality: ");
Serial.println(quality);
}
void loop() {
// Read and display sensor data
air_sensor();
sendSensor();
// Add a delay to avoid flooding the Serial Monitor
delay(2000); // Adjust delay as needed
}
Explanation of Code:
This Arduino code uses an analog gas sensor and a DHT11 temperature and humidity
sensor to monitor air quality, temperature, and humidity levels. Here's a breakdown of the
main parts of the code and the output it produces:
Library and Sensor Setup:
• #include <SPI.h> and #include <DHT.h>: Imports the necessary libraries for handling
SPI communication and the DHT sensor.
• #define DHTPIN 2 and #define DHTTYPE DHT11: Specifies that the DHT sensor is
connected to pin 2 and uses the DHT11 type.
• DHT dht(DHTPIN, DHTTYPE): Creates an instance of the DHT class to interface with
the sensor.
sendSensor() Function:
• Reads humidity and temperature values from the DHT sensor.
• If readings fail (isnan(h) or isnan(t)), it prints an error message.
• If successful, it prints temperature and humidity values to the Serial Monitor.
air_sensor() Function:
Reads the analog value of the gas sensor and assigns a corresponding air quality label:
• <151: "GOOD!"
• 151 - 199: "Poor!"
• 200 - 299: "Very bad!"
• 300 - 499: "Toxic!"
• >=500: "Toxic"
Outputs the gas level and quality to the Serial Monitor.
Output: