#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include "DHT.h"
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define NO2_PIN A0
const float Vcc = 5.0;
const float RL = 4700.0;
float R0 = 12000.0;
float A = 1.0;
float B = -1.25;
const float scaleFactor = 0.001;
#define MQ136_PIN A7
float Vcc1 = 5.0;
float RL1 = 10000.0;
float R01;
float A_1 = 10.0;
float B_1 = -1.3;
#define TOUCH_PIN 11
#define LED_PIN 2
void setup() {
Serial.begin(9600);
pinMode(TOUCH_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Smart Acid Rain");
lcd.setCursor(0, 2);
lcd.print("Detection System");
delay(1000);
Serial.println("DFRobot Fermion / MEMS NO2 Sensor Reading");
Serial.println("Output in ppb (Calibrated for clean air ~1 ppb)");
delay(3000); // short warm-up
Serial.println("Calibrating MQ136 sensor...");
Serial.println("Ensure the sensor is in clean, fresh air for accurate R0 calculation.");
//delay(2000);
delay(180000);
R01 = calibrateR01();
delay(2000);
Serial.print("Calibration complete. R0 = ");
Serial.print(R01, 0);
Serial.println(" Ohms.");
Serial.println("Starting measurement loop in 5 seconds...");
digitalWrite(LED_PIN, LOW);
dht.begin();
delay(10000);
lcd.clear();
}
void loop() {
digitalWrite(LED_PIN, LOW);
/*
int touchState = digitalRead(TOUCH_PIN); // Read touch sensor
if (touchState == HIGH) { // Touched
/digitalWrite(LED_PIN, HIGH);
} else { // Not touched
}
lcd.setCursor(0,0);
lcd.print("ditance:");
lcd.setCursor(8,0);
*/
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
int sensorValue1 = analogRead(MQ136_PIN);
float sensorVoltage1 = (sensorValue1 / 1023.0) * Vcc1;
float Rs1 = (Vcc1 * RL1 / sensorVoltage1) - RL1;
float ratio1 = Rs1 / R01;
float ppm1 = A_1 * pow(ratio1, B_1);
float ppb1 = ppm1 * 1000.0;
int sensorValue = analogRead(NO2_PIN);
float sensorVoltage = (sensorValue / 1023.0) * Vcc;
if (sensorVoltage < 0.05) {
Serial.println("Low sensor voltage check wiring or sensor warm-up.");
delay(2000);
return;
}
float Rs = (Vcc * RL / sensorVoltage) - RL;
float ratio = Rs / R0;
float ppm = A * pow(ratio, B);
ppm = ppm * scaleFactor;
float ppb = ppm * 1000.0;
Serial.print("ADC: "); Serial.print(sensorValue1);
Serial.print(" | Voltage: "); Serial.print(sensorVoltage1, 3);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(" | SO2: "); Serial.print(ppm1, 3);
Serial.print(" ppm");
Serial.print(" | SO2: "); Serial.print(ppb1, 1);
Serial.print(" ppb");
Serial.print(" | NO2: "); Serial.print(ppm, 3); Serial.print(" ppm");
Serial.print(" ("); Serial.print(ppb, 1); Serial.println(" ppb)");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP:");
lcd.setCursor(5, 0);
lcd.print(t);
lcd.setCursor(11, 0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("HUMI:");
lcd.setCursor(5, 1);
lcd.print(h);
lcd.setCursor(11, 1);
lcd.print("%");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SO2:");
lcd.setCursor(4, 0);
lcd.print(ppb1, 1);
lcd.setCursor(8, 0);
lcd.print("% PPB");
lcd.setCursor(0, 1);
lcd.print("NOX:");
lcd.setCursor(4, 1);
lcd.print(ppb, 1);
lcd.setCursor(8, 1);
lcd.print("% PPB");
delay(5000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Air Quality");
if (ppb1 < 25 && ppb < 20) {
lcd.setCursor(3, 1);
lcd.print("Clean Air");
}
else if (ppb1 > 25 && ppb1 < 55 &&ppb1 > 40 && ppb < 60) {
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("Slight pollution");
}
else if (ppb1 > 80 && ppb1 < 100 && ppb > 80 && ppb < 140) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Acid rain may");
lcd.setCursor(2, 1);
lcd.print("start forming");
digitalWrite(LED_PIN, HIGH);
}
else if (ppb1 > 180 && ppb > 180) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Acid rain may");
lcd.setCursor(2, 1);
lcd.print("start Soon");
digitalWrite(LED_PIN, HIGH);
}
delay(2000);
}
float calibrateR01() {
int readings1 = 100;
float sumRs1 = 0;
for (int i1 = 0; i1 < readings1; i1++) {
int sensorValue1 = analogRead(MQ136_PIN);
float sensorVoltage1 = (sensorValue1 / 24.0) * Vcc1;
if (sensorVoltage1 < 0.1) sensorVoltage1 = 0.1; // avoid division by zero
float Rs1 = (Vcc1 * RL1 / sensorVoltage1) - RL1;
sumRs1 += Rs1;
delay(50);
}
return sumRs1 / readings1; // Average R0
}
Comments