#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define CO_SENSOR_PIN 34 // Simulated CO Sensor
#define NO2_SENSOR_PIN 35 // Simulated NO2 Sensor
#define AQ_SENSOR_PIN 32 // Simulated Air Quality Sensor
#define RED_LED 27 // High Pollution Alert LED
#define GREEN_LED 26 // Normal Air Quality LED
#define BUZZER 25 // Alarm for High Pollution
void setup() {
Serial.begin(115200);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BUZZER, LOW);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Air Pollution Monitoring");
display.display();
delay(2000);
}
void loop() {
int coValue = analogRead(CO_SENSOR_PIN); // Read CO sensor value
int no2Value = analogRead(NO2_SENSOR_PIN); // Read NO2 sensor value
int aqValue = analogRead(AQ_SENSOR_PIN); // Read air quality sensor
// Simulated conversion of sensor values (0-4095) to realistic units
float coLevel = map(coValue, 0, 4095, 0, 100); // Example: 0-100 ppm
float no2Level = map(no2Value, 0, 4095, 0, 200); // Example: 0-200 ppb
float airQuality = map(aqValue, 0, 4095, 0, 500); // Example AQI 0-500
Serial.print("CO Level: "); Serial.print(coLevel); Serial.println(" ppm");
Serial.print("NO2 Level: "); Serial.print(no2Level); Serial.println(" ppb");
Serial.print("Air Quality Index: "); Serial.println(airQuality);
// Update OLED Display
display.clearDisplay();
display.setCursor(0, 10);
display.print("CO: "); display.print(coLevel); display.println(" ppm");
display.print("NO2: "); display.print(no2Level); display.println(" ppb");
display.print("AQI: "); display.println(airQuality);
// Pollution alert conditions
if (coLevel > 50 || no2Level > 100 || airQuality > 150) { // High pollution threshold
digitalWrite(RED_LED, HIGH); // Turn on warning LED
digitalWrite(GREEN_LED, LOW); // Turn off normal LED
digitalWrite(BUZZER, HIGH); // Sound alarm
display.println(" High Pollution ");
} else {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BUZZER, LOW);
display.println("Air Quality: Normal");
}
display.display();
delay(2000); // Update every 2 seconds
}