Arduino Air Quality Monitoring System
Arduino Air Quality Monitoring System
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:
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
}
void air_sensor() {
gasLevel = analogRead(sensor);
void loop() {
// Read and display sensor data
air_sensor();
sendSensor();
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:
• #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:
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: