MQ-2 Gas & Smoke Sensor

Complete Tutorial: Smoke Detection & Gas Leak Alarm

📖 12 min read 🔧 Beginner Friendly 💻 Arduino / ESP32

1. Introduction

The MQ-2 is a versatile gas sensor that can detect combustible gases and smoke. It's commonly used in DIY smoke detectors, gas leak alarms, and air quality monitors.

📦 Gases Detected

  • LPG (Liquefied Petroleum Gas)
  • Methane (Natural Gas)
  • Propane
  • Butane
  • Hydrogen
  • Smoke

⚠️ Safety Warning

This sensor is for educational purposes only. Do NOT use it as the sole safety device for life-critical applications. Always use certified commercial gas detectors for safety-critical installations.

2. Technical Specifications

Parameter Specification
Operating Voltage 5V DC
Heater Current ~150mA
Detection Range 300-10000 ppm
Preheat Time 20+ seconds (24-48hrs for accurate calibration)
Output Type Analog (AO) + Digital (DO)
Digital Trigger Adjustable via potentiometer

How It Works

The MQ-2 uses a tin dioxide (SnO2) semiconductor that changes resistance when exposed to gases:

  1. Internal heater warms the sensing element
  2. Clean air: High resistance → Low output voltage
  3. Gas present: Low resistance → High output voltage
  4. Compare voltage to threshold for detection

⚠️ Preheat Required

The sensor needs to warm up for at least 20 seconds before readings are stable. For accurate calibration, run continuously for 24-48 hours in clean air.

3. Wiring Guide

MQ-2 Pin Description Arduino ESP32
VCC Power (5V) 5V VIN/5V
GND Ground GND GND
AO Analog Output A0 GPIO 34
DO Digital Output Pin 2 GPIO 13
MQ-2 wiring with Arduino

Figure 1: MQ-2 sensor wiring with Arduino

💡 Module Features

Potentiometer: Adjusts the digital output (DO) threshold

LEDs: Power indicator + alarm indicator

4. Project: Smoke & Gas Alarm

Basic Gas Detection

const int analogPin = A0;
const int digitalPin = 2;
const int buzzerPin = 8;

void setup() {
    Serial.begin(115200);
    pinMode(digitalPin, INPUT);
    pinMode(buzzerPin, OUTPUT);
    
    Serial.println("MQ-2 Gas Sensor");
    Serial.println("Warming up (20 seconds)...");
    delay(20000);  // Preheat time
    Serial.println("Ready!");
}

void loop() {
    int gasLevel = analogRead(analogPin);
    int alarm = digitalRead(digitalPin);

    Serial.print("Gas Level: ");
    Serial.print(gasLevel);

    if (alarm == LOW) {  // LOW = threshold exceeded
        Serial.println(" | ALARM: GAS DETECTED!");
        digitalWrite(buzzerPin, HIGH);
    } else {
        Serial.println(" | Status: Normal");
        digitalWrite(buzzerPin, LOW);
    }

    delay(500);
}

Gas Level Monitor with Categories

const int analogPin = A0;

void setup() {
    Serial.begin(115200);
    Serial.println("MQ-2 Warming up...");
    delay(20000);
    Serial.println("Ready!");
}

void loop() {
    int gasLevel = analogRead(analogPin);
    
    Serial.print("Gas Level: ");
    Serial.print(gasLevel);
    Serial.print(" | Status: ");
    
    if (gasLevel < 200) {
        Serial.println("Clean Air");
    } else if (gasLevel < 400) {
        Serial.println("Low Gas Detected");
    } else if (gasLevel < 600) {
        Serial.println("Moderate - Check Area!");
    } else {
        Serial.println("HIGH - DANGER!");
    }
    
    delay(1000);
}

5. Calibration Tips

  1. Run the sensor in clean air for 24-48 hours
  2. Note the baseline reading (typically 100-200)
  3. Set your alarm threshold above baseline + margin
  4. Test with a lighter (briefly release butane near sensor)
  5. Adjust potentiometer to set DO trigger point

6. Applications

  • Smoke detection: DIY fire alarm systems
  • Gas leak detection: Kitchen safety monitors
  • Industrial safety: Workshop ventilation triggers
  • Robotics: Hazardous environment detection

7. Troubleshooting

Always reads high value

  • Allow longer preheat time
  • Ensure good ventilation around sensor

Not detecting gas

  • Check 5V power supply (not 3.3V)
  • Verify sensor is heated (check for warmth)

8. Summary

  • Detects LPG, methane, propane, butane, hydrogen, smoke
  • Requires 5V and 20+ second preheat
  • Analog output (AO) for gas concentration level
  • Digital output (DO) for threshold alarm
  • Potentiometer adjusts DO trigger threshold