MQ-2 Combustible Gas & Smoke Safety Detector
Monitor LPG, methane, propane, and smoke concentration using a heated SnO2 semiconductor gas sensor and safety alarm.
Build an environmental gas and smoke alarm monitor with the MQ-2 metal-oxide sensor module. Understand internal heater preheat stabilization, capture clean-air baseline ADC values, and trigger staged Warning/Danger LED and piezo alerts when combustible vapors rise.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerThe MQ-2 contains a 5V Nichrome heating coil drawing ~150mA continuously to activate its tin-dioxide (SnO2) sensing layer. Ensure a solid USB port capable of 500mA when running alongside LEDs and a buzzer. SAFETY: Internal heater warms the metal mesh noticeably—do not touch while powered. Educational prototype only; never replace certified UL/EN home smoke alarms!
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| MQ-2 Gas & Smoke Sensor Module | Detects LPG, propane, methane, H2, smoke (300–10000ppm) | 1 | $2.50 |
| Green & Red LEDs + 220Ω Resistors | Safe vs Hazard status indicators | 2 | $0.30 |
| 5V Piezo Buzzer | Audible warning alarm on D8 | 1 | $0.80 |
Electrical & System Architecture
Connect MQ-2 VCC to 5V, GND to GND, AO to A0, and DO to D2. Wire a Red Hazard LED (+220Ω) to D7, Green Safe LED (+220Ω) to D6, and Piezo Buzzer to D8.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling MQ-2 Combustible Gas & Smoke Module acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Review Gas Sensor Thermal & Testing Safety
Place the MQ-2 in a well-ventilated area where the warm stainless-steel mesh cannot touch plastic or skin.
- Inspect the 4 pins on the MQ-2 module: VCC, GND, DO, AO.
- Position the sensor upright on the breadboard with clear airflow.
- For safe testing, use a cotton swab dipped in rubbing alcohol (isopropyl vapor)—never use open flame!
Wire MQ-2, Status LEDs & Piezo Buzzer
Connect the sensor to A0/D2, Green LED to D6, Red LED to D7, and Buzzer to D8.
- Wire MQ-2 VCC → 5V, GND → GND, AO → A0, DO → D2.
- Wire Green LED (+220Ω) to D6 and Red LED (+220Ω) to D7.
- Wire Piezo Buzzer (+) to D8 and (-) to GND.
Verify Heater Warm-Up & Baseline Reading
Power up and observe raw AO drop gradually as the heater reaches operating temperature.
- Upload the diagnostic snippet and open Serial Monitor.
- Watch raw A0 start high (~400–600) and settle steadily toward a clean-air baseline (~100–250).
- Confirm the sensor housing becomes gently warm to the touch nearby.
Upload Dual-Stage Safety Monitor Firmware
Flash the auto-calibrating gas detector with 20-second warm-up and buzzer siren.
- Upload the production sketch below.
- Wait 20 seconds for the Serial warm-up countdown to complete and lock clean-air baseline.
- Verify Green LED (D6) illuminates steadily once monitoring begins.
Safe Vapor Response Test
Hold an alcohol-dampened cotton swab 2cm from the mesh (no contact) and verify rapid alarm trigger.
- Hold isopropyl swab near the mesh and watch ADC climb by 150–400 counts.
- Confirm Red LED (D7) illuminates and the piezo buzzer sounds its warning pulse.
- Remove the swab and watch readings recover cleanly back to Green SAFE state.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: MQ-2 Combustible Gas & Smoke Module Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for MQ-2 Combustible Gas & Smoke Module before running the full project firmware.
// Diagnostic: Raw MQ-2 Analog Trend Monitor
void setup() { Serial.begin(115200); }
void loop() { Serial.print("MQ2 AO: "); Serial.println(analogRead(A0)); delay(500); }Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 20: MQ-2 Combustible Gas & Smoke Safety Monitor
* Board: Arduino Uno R3 | Pins: A0 (MQ2 AO), D6 (Green), D7 (Red), D8 (Buzzer)
*/
const uint8_t MQ2_AO = A0;
const uint8_t LED_SAFE = 6;
const uint8_t LED_ALERT = 7;
const uint8_t BUZZER_PIN = 8;
const int ALERT_DELTA = 120; // Trigger alarm when ADC rises +120 above baseline
int cleanBaseline = 200;
int readAvg() {
long sum = 0;
for (uint8_t i = 0; i < 10; i++) { sum += analogRead(MQ2_AO); delay(10); }
return sum / 10;
}
void setup() {
Serial.begin(115200);
pinMode(LED_SAFE, OUTPUT);
pinMode(LED_ALERT, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.println(F("MQ-2 Heating up (20s)... Keep in clean air."));
for (int s = 20; s > 0; s--) {
digitalWrite(LED_SAFE, s % 2);
Serial.print(F("Warmup: ")); Serial.print(s); Serial.println(F("s"));
delay(1000);
}
cleanBaseline = readAvg();
Serial.print(F("Baseline Locked: ")); Serial.println(cleanBaseline);
}
void loop() {
int val = readAvg();
int delta = val - cleanBaseline;
bool danger = (delta >= ALERT_DELTA);
digitalWrite(LED_SAFE, !danger);
digitalWrite(LED_ALERT, danger);
if (danger) {
tone(BUZZER_PIN, 2100, 180);
Serial.print(F("[ALARM] Gas/Smoke Elevated! AO="));
} else {
noTone(BUZZER_PIN);
Serial.print(F("[SAFE] Air Normal | AO="));
}
Serial.print(val); Serial.print(F(" (Delta: ")); Serial.print(delta); Serial.println(F(")"));
delay(350);
}Firmware Architecture Explanation:
- Executes a 20-second preheat warm-up countdown before locking cleanBaseline in fresh air.
- Uses relative delta above clean air so room humidity or sensor aging does not cause nuisance trips.
- Activates both visual (Red LED) and audible (2.1kHz piezo pulse) warnings during gas spikes.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Delta < +120 | A0 Clean Air | Normal Environment | Green LED ON, Buzzer OFF |
| Delta ≥ +120 | A0 Gas Spike | Vapor / Smoke Detected | Red LED ON + 2.1kHz Alarm |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Alarm triggers immediately after power-on | Sensor still cold during initial burn-in | Watch AO values drop over 60 seconds | Let sensor warm up fully in clean air, then press Arduino Reset. |
| MQ-2 mesh stays cold and AO reads 0 | 5V supply wire loose or unplugged | Measure 5.0V across module VCC and GND | Re-seat power jumpers tightly. |
| Slow recovery after vapor test | Normal SnO2 desorption time | Wait 30–60s in fresh air | Provide gentle airflow; readings naturally settle back to baseline. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Add a Yellow Caution LED for Delta +60..+119 before triggering the Red siren at +120.
Display live baseline, delta bar graph, and SAFE/ALERT status on Project 4 OLED.
Drive a 5V DC fan motor (Project 29) automatically whenever smoke is detected.