//Define Gpio pins with meaningful names
#define GAS_SENSOR_PIN 34
#define BUZZER_GPIO 26
//concentration (read via MQ135)crosses the set gas threshold value (whichis 200 in this case)
#define SAFE_LED_PIN 19
#define ALERT_LED_PIN 21
int gasthreshold = 200;
void setup() {
// Initalize serial communication at 115200 baud rate
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//Set pin modes for output devices
pinMode(BUZZER_GPIO, OUTPUT);
pinMode(SAFE_LED_PIN, OUTPUT);
pinMode( ALERT_LED_PIN, OUTPUT);
}
void loop() {
// Read analog gas sensor value from MQ-135
int gasLevel = analogRead(GAS_SENSOR_PIN);
//Print gas level to serial monitor for debugging
Serial.print("Gas Level:");
Serial.println(gasLevel);
//Compare gas level with threshold
if (gasLevel > gasThreshold){
//High gas concentration detected - activate danger alters
digitalWrite( ALERT_LED_PIN,HIGH);
digitalWrite(SAFE_LED_PIN,LOW);
digitalWrite(BUZZER_GPIO,HIGH);
Serial.println("Warning! Harmful gas detected!");
}else{
//Gas level is safe - keep clam and glow green
digitalWrite(ALERT_LED_PIN,LOW);
digitalWrite(SAFE_LED_PIN,HIGH);
digitalWrite(BUZZER_GPIO,LOW);
tone(26, 265, 250);
Serial.println("Air quality is safe.");
}
delay(1000); // Wait for 1 sec before next reading
}