Complete Tutorial: Smoke Detection & Gas Leak Alarm
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.
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.
| 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 |
The MQ-2 uses a tin dioxide (SnO2) semiconductor that changes resistance when exposed to gases:
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.
| 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 |
Figure 1: MQ-2 sensor wiring with Arduino
Potentiometer: Adjusts the digital output (DO) threshold
LEDs: Power indicator + alarm indicator
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);
}
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);
}