Air Quality Code Explained
Air Quality Code Explained
gas sensor, LEDs, a buzzer, and an LCD for display. Below is a detailed explanation of the
code:
cpp
Copy code
#include "LiquidCrystal.h"
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
2. Global Variables
aqi_ppm: Stores the air quality value read from the sensor.
A threshold system is used to categorize air quality.
3. setup() Function
cpp
Copy code
void setup() {
pinMode (mq135_aqi_sensor, INPUT);
pinMode (green_led, OUTPUT);
pinMode (blue_led, OUTPUT);
pinMode (red_led, OUTPUT);
pinMode (buzzer, OUTPUT);
digitalWrite(green_led, LOW);
digitalWrite(blue_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(buzzer, LOW);
Serial.begin (9600);
lcd.clear();
lcd.begin (16, 2);
4. loop() Function
This is the main logic for monitoring AQI and controlling outputs.
The air quality value is read using the MQ-135 sensor through the analog pin A0.
cpp
Copy code
aqi_ppm = analogRead(mq135_aqi_sensor);
b. Print Data
The AQI value is printed to both the LCD and the serial monitor.
cpp
Copy code
Serial.print("Air Quality: ");
Serial.println(aqi_ppm);
lcd.setCursor(0, 0);
lcd.print("Air Quality: ");
lcd.print(aqi_ppm);
The AQI is classified into five categories based on its value, and corresponding actions are taken.
a. Good (0–50)
cpp
Copy code
if ((aqi_ppm >= 0) && (aqi_ppm <= 50)) {
lcd.setCursor(0, 1);
lcd.print("AQI Good");
Serial.println("AQI Good");
digitalWrite(green_led, HIGH);
digitalWrite(blue_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(buzzer, LOW);
}
b. Moderate (51–100)
cpp
Copy code
else if ((aqi_ppm >= 51) && (aqi_ppm <= 100)) {
lcd.setCursor(0, 1);
lcd.print("AQI Moderate");
Serial.println("AQI Moderate");
tone(green_led, 1000, 200);
digitalWrite(blue_led, HIGH);
digitalWrite(red_led, LOW);
digitalWrite(buzzer, LOW);
}
c. Unhealthy (101–200)
cpp
Copy code
else if ((aqi_ppm >= 101) && (aqi_ppm <= 200)) {
lcd.setCursor(0, 1);
lcd.print("AQI Unhealthy");
Serial.println("AQI Unhealthy");
digitalWrite(green_led, LOW);
digitalWrite(blue_led, HIGH);
digitalWrite(red_led, LOW);
digitalWrite(buzzer, LOW);
}
d. Very Unhealthy (201–300)
cpp
Copy code
else if ((aqi_ppm >= 201) && (aqi_ppm <= 300)) {
lcd.setCursor(0, 1);
lcd.print("AQI V. Unhealthy");
Serial.println("AQI V. Unhealthy");
digitalWrite(green_led, LOW);
tone(blue_led, 1000, 200);
digitalWrite(red_led, HIGH);
digitalWrite(buzzer, LOW);
}
e. Hazardous (301 and above)
cpp
Copy code
else if (aqi_ppm >= 301) {
lcd.setCursor(0, 1);
lcd.print("AQI Hazardous");
Serial.println("AQI Hazardous");
digitalWrite(green_led, LOW);
digitalWrite(blue_led, LOW);
digitalWrite(red_led, HIGH);
digitalWrite(buzzer, HIGH);
}
6. Delay
A delay of 700 ms ensures the system refreshes at regular intervals without overwhelming the
outputs.
cpp
Copy code
delay(700);
Summary
The program reads real-time AQI values using the MQ-135 sensor.
Based on the thresholds, it activates LEDs and the buzzer to provide visual and auditory alerts.
The LCD and serial monitor display the AQI values and their respective categories.
This system is effective for real-time air quality monitoring in small-scale environments.