Gas Leakage & Air Quality Prototype Alert System
Implement an environmental safety detector using an MQ gas sensor prototype module with audible buzzer alarm and visual warning alert.
Domestic LPG kitchen safety detectors, industrial gas storage monitoring, and mine air quality sentinels.
The MQ sensor uses a Tin Dioxide (SnO2) sensitive layer heated by an internal element. When combustible gases pass over the heated surface, oxygen adsorption decreases and electrical resistance drops, yielding a proportional analog voltage.
Pin Connection & Wiring Table
| Component Pin / Terminal | Microcontroller Pin (ESP32 / Arduino) | Wiring Note |
|---|---|---|
| MQ-2 VCC | VIN / 5V | Heater requires 5V supply |
| MQ-2 GND | GND | Common ground |
| MQ-2 AOUT | GPIO 35 / A0 | Analog sensor voltage |
| Buzzer (+) | GPIO 19 | Audio alarm |
| LED Anode via 220Ω | GPIO 4 | Visual alarm |
Step-by-Step Plug & Play Assembly
Connect MQ-2 VCC to 5V and GND to GND.
Connect MQ-2 analog output (AOUT) to GPIO 35.
Allow the sensor to pre-heat for 2-3 minutes until readings stabilize.
Wire the buzzer to GPIO 19 and warning LED to GPIO 4.
Complete Tested Source Code
// NovaCircuit Project 10: Gas Leakage Safety Alert
const int SENSOR_PIN = 35;
const int BUZZER_PIN = 19;
const int LED_PIN = 4;
const int GAS_ALARM_THRESHOLD = 1800; // Calibrate during pre-heating
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println("Gas Leakage Alert System Active (Pre-heating...)");
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
Serial.print("Air Quality Gas ADC: ");
Serial.println(sensorValue);
if (sensorValue > GAS_ALARM_THRESHOLD) {
// Dangerous gas concentration detected
Serial.println("EMERGENCY ALERT: High Gas / Smoke Concentration Detected!");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(150);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
} else {
// Normal ambient conditions
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
delay(400);
}Continuous polling of GPIO 35 measures gas concentration. If the ADC reading exceeds GAS_ALARM_THRESHOLD, an emergency pulsing buzzer alarm and blinking warning LED are engaged immediately.
Expected Physical Output
- •High readings initially: MQ sensors run hot by design and require 2-5 minutes of initial warm-up to burn off protective coatings.
- •Sensor body warm to touch: Normal operation; the internal heater coil maintains ~200°C for catalytic sensing.
- •Do NOT test with open gas cylinders or flammable flames. Use a safe swab with rubbing alcohol vapors to test response.
Related Book Chapter & Hardware Kit
Covered in depth within Chapter 11: Hands-on Electronics Projects. Build this easily with the IoT Innovator Kit.