#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
// -------- DHT --------
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// -------- SENSOR PINS --------
#define PM25_PIN 34
#define WIND_PIN 35
#define CO_PIN 32
#define O3_PIN 33
// -------- LED --------
#define GREEN_LED 25
#define YELLOW_LED 26
#define RED_LED 27
// -------- AQI --------
float calcAQI(float Cp, float Clow, float Chigh, float Ilow, float Ihigh) {
return ((Ihigh - Ilow) / (Chigh - Clow)) * (Cp - Clow) + Ilow;
}
float AQI_PM25(float pm) {
if (pm <= 30) return calcAQI(pm, 0, 30, 0, 50);
if (pm <= 60) return calcAQI(pm, 30, 60, 50, 100);
if (pm <= 90) return calcAQI(pm, 60, 90, 100, 200);
if (pm <= 120) return calcAQI(pm, 90, 120, 200, 300);
if (pm <= 250) return calcAQI(pm, 120, 250, 300, 400);
return calcAQI(pm, 250, 350, 400, 500);
}
float AQI_CO(float co) {
if (co <= 1) return calcAQI(co, 0, 1, 0, 50);
if (co <= 2) return calcAQI(co, 1, 2, 50, 100);
return calcAQI(co, 2, 10, 100, 200);
}
float AQI_O3(float o3) {
if (o3 <= 50) return calcAQI(o3, 0, 50, 0, 50);
if (o3 <= 100) return calcAQI(o3, 50, 100, 50, 100);
return calcAQI(o3, 100, 200, 100, 200);
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
dht.begin();
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
}
void loop() {
// -------- RAW VALUES --------
float pm25 = map(analogRead(PM25_PIN), 0, 4095, 0, 300);
float wind = map(analogRead(WIND_PIN), 0, 4095, 0, 5);
float co = map(analogRead(CO_PIN), 0, 4095, 0, 3);
float o3 = map(analogRead(O3_PIN), 0, 4095, 0, 200);
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (isnan(temp) || isnan(hum)) {
Serial.println("DHT Error");
return;
}
// -------- DELHI LOGIC --------
// Temperature effects
o3 *= (1 + 0.01 * (temp - 25));
pm25 *= (1 - 0.005 * (temp - 25));
// Humidity effects
if (hum < 30) pm25 *= 1.2;
if (hum > 70) pm25 *= 1.15;
// Wind effects
pm25 *= (1 - 0.08 * wind);
if (wind > 3.5) pm25 *= 1.2;
// CO stagnation
if (wind < 1.5) co *= 1.2;
// Rain washout
if (hum > 90) pm25 *= 0.7;
// PM10 approx
float PM10 = pm25 * 1.5;
// -------- AQI --------
float aqi_pm25 = AQI_PM25(pm25);
float aqi_co = AQI_CO(co);
float aqi_o3 = AQI_O3(o3);
float AQI = max(aqi_pm25, max(aqi_co, aqi_o3));
// -------- ALERT --------
int alert = 0;
String text = "NORMAL";
if (AQI > 200) {
alert = 2;
text = "HOTSPOT";
}
else if (AQI > 100) {
alert = 1;
text = "WARNING";
}
digitalWrite(GREEN_LED, alert == 0);
digitalWrite(YELLOW_LED, alert == 1);
digitalWrite(RED_LED, alert == 2);
// -------- LCD --------
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AQI:");
lcd.print((int)AQI);
lcd.setCursor(0, 1);
lcd.print(text);
// -------- SERIAL --------
Serial.print("PM2.5: "); Serial.print(pm25);
Serial.print(" | CO: "); Serial.print(co);
Serial.print(" | O3: "); Serial.print(o3);
Serial.print(" | Temp: "); Serial.print(temp);
Serial.print(" | Hum: "); Serial.print(hum);
Serial.print(" | Wind: "); Serial.print(wind);
Serial.print(" | AQI: "); Serial.println(AQI);
delay(500);
}PM2.5
WIND SPEED
CO
O
3