/*
Wokwi | projects
Can anyone help in doing this project by code
and hardware, air quality using esp32
Karthik September 11, 2025 at 1:15 PM
Task - 1: Air Quality Safety Monitor
Problem Statement: Indoor pollution often goes unnoticed.
Build a system that checks air quality and alerts when unsafe using ESP32
Requirements:
Use a MQ- Gas Sensor to detect harmful gases.
Use a DHTSensor for temperature & humidity.
If the gas level is above threshold and humidity is high (>70%),
turn on a simulated buzzer (LED).
Show readings and alert status on Serial Monitor.
*/
#include "DHTesp.h"
const int DHT_PIN = 12;
const int MQ2_PIN = 13;
const int LED_PIN = 14;
const int BUZZ_PIN = 27;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(MQ2_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
}
void loop() {
// read DHT
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
// read MQ2
if (digitalRead(MQ2_PIN)) {
Serial.println("No gas sensed.");
} else {
Serial.println("Gas sensed!");
}
Serial.println("---");
if (!digitalRead(MQ2_PIN) && data.humidity > 70) {
tone(BUZZ_PIN, 440);
digitalWrite(LED_PIN, HIGH);
} else {
noTone(BUZZ_PIN);
digitalWrite(LED_PIN, LOW);
}
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}