// Mine Safety Gas Detection System
// ESP32 + MQ2 Gas Sensor + Buzzer + LED
// For detecting hazardous gases in mines
#define MQ2_ANALOG_PIN 34 // Analog pin for MQ2 gas sensor (AO)
#define BUZZER_PIN 2 // Digital pin for buzzer
#define LED_PIN 23 // Digital pin for warning LED
#define GAS_THRESHOLD 2000 // Gas threshold value (mid-range for safety)
void setup() {
Serial.begin(115200);
// Initialize pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(MQ2_ANALOG_PIN, INPUT);
// Initial state - safe
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
Serial.println("Mine Safety Gas Detection System Initialized");
Serial.println("Monitoring gas levels...");
delay(2000); // Sensor warm-up time
}
void loop() {
// Read gas sensor value
int gasValue = analogRead(MQ2_ANALOG_PIN);
// Display readings on serial monitor
Serial.print("Gas Level: ");
Serial.print(gasValue);
Serial.print(" (Range: 843-4041)");
// Check if gas level exceeds threshold
if (gasValue > GAS_THRESHOLD) {
// Danger - activate alarm
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000); // 1kHz alarm tone
Serial.println(" - DANGER: Gas detected!");
} else {
// Safe - deactivate alarm
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
Serial.println(" - Safe");
}
delay(500); // Check every 500ms
}