0% found this document useful (0 votes)
7 views4 pages

Air Quality Code Explained

This Arduino sketch implements an Air Quality Index (AQI) Alert System using an MQ-135 gas sensor, LEDs, a buzzer, and an LCD for display. It reads AQI values in real-time and activates corresponding alerts based on predefined thresholds. The system effectively monitors air quality and provides visual and auditory feedback for different AQI categories.

Uploaded by

Meera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Air Quality Code Explained

This Arduino sketch implements an Air Quality Index (AQI) Alert System using an MQ-135 gas sensor, LEDs, a buzzer, and an LCD for display. It reads AQI values in real-time and activates corresponding alerts based on predefined thresholds. The system effectively monitors air quality and provides visual and auditory feedback for different AQI categories.

Uploaded by

Meera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

This Arduino sketch implements an Air Quality Index (AQI) Alert System using an MQ-135

gas sensor, LEDs, a buzzer, and an LCD for display. Below is a detailed explanation of the
code:

1. Library and Pin Initialization

 The LiquidCrystal library is included to control the LCD.


 Pins for the LCD (rs, en, d4, d5, d6, d7) and other components (MQ-135 sensor, LEDs,
and buzzer) are defined.

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);

const int mq135_aqi_sensor = A0;


const int green_led = 8;
const int blue_led = 9;
const int red_led = 10;
const int buzzer = 11;

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

 Sets up input/output pins for the sensor, LEDs, and buzzer.


 Initializes serial communication and the LCD.
 Displays a welcome message on the LCD and serial monitor.

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);

Serial.println("AQI Alert System");


lcd.setCursor(0, 0);
lcd.print("AQI Alert System");
delay(1000);
}

4. loop() Function

This is the main logic for monitoring AQI and controlling outputs.

a. Read Sensor Data

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);

5. AQI Threshold Logic

The AQI is classified into five categories based on its value, and corresponding actions are taken.

a. Good (0–50)

 Green LED is ON.


 No buzzer or other LEDs are activated.

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)

 Blue LED is ON.


 Green LED briefly flashes as a tone is generated.
 No buzzer is activated.

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)

 Blue LED is ON.


 No tone or buzzer is activated.

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)

 Red LED is ON.


 Blue LED briefly flashes as a tone is generated.
 No buzzer is activated.

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)

 Red LED and buzzer are ON to indicate danger.

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.

You might also like