MQ-2 Gas Leak Alarm with Arduino and Buzzer
Build a learning gas and smoke alarm with the MQ-2 sensor, Arduino Uno and buzzer. Wiring, warm-up, calibration, code and safety notes.
On this page
LPG cylinders are common in Nepali kitchens, so a gas alarm is a project people instantly understand. With an MQ-2 sensor module, an Arduino Uno and a buzzer you can build an alarm that beeps when the sensor reading rises above a safe level. This tutorial shows the wiring, the warm-up, how to set a threshold from your own readings, and how to test safely.
Important safety warning
This is a hobby learning project. An MQ-2 module is not a certified safety device. It can drift, fail without warning, and respond to other vapours. Never use it as the only protection in a home, kitchen, shop or workplace. Buy a certified gas detector for real protection, and keep your LPG regulator and pipe in good condition.
If you smell gas, avoid flames and electrical switches, leave the area and call for help from outside.
What you need
Scroll sideways to see all columns.
| Item | Qty | Notes |
|---|---|---|
| Arduino Uno (or clone) + USB cable | 1 | |
| MQ-2 gas sensor module | 1 | 4 pins: VCC, GND, DO, AO, with a blue potentiometer |
| Active piezo buzzer, 5 V | 1 | Rated at 20 mA or less for direct D8 drive |
| LED 5 mm | 1 | Status indicator |
| Resistor 220 Ω | 1 | |
| Jumper wires | 8 | |
| 10 kΩ potentiometer | 1 | Optional, for simulating the sensor output |
| Digital multimeter | 1 |
How it works
Inside the metal cap of the MQ-2 is a small heater and a sensing layer. When gases such as LPG, propane, methane, hydrogen or smoke touch the heated layer, its resistance drops. The module turns that into a voltage on the AO (analog output) pin: more gas, higher voltage. The DO (digital output) pin uses an onboard comparator and goes LOW or HIGH when the level crosses the threshold set by the blue potentiometer. Most modules give DO LOW on gas, but check yours.
Three practical facts:
- The heater uses power. It draws up to about 190 mA at 5 V, plus the module electronics, and the module gets warm. A USB port can supply this, but a weak hub may not.
- It needs warm-up. Winsen specifies at least 48 hours of preheating for its standard test. This sketch waits three minutes for a demo, but the baseline can still drift. Let the reading stabilise in clean air before using it for comparisons.
- It is not selective. Smoke, alcohol vapour and cooking fumes also raise the reading. Expect false alarms near a stove.
The sketch measures the clean-air baseline at start-up and sets the alarm threshold to a margin above it, so it adapts to your module.
Connection table
Scroll sideways to see all columns.
| Component pin | Arduino Uno pin |
|---|---|
| MQ-2 VCC | 5V |
| MQ-2 GND | GND |
| MQ-2 AO | A0 |
| Buzzer + | D8 |
| Buzzer − | GND |
| LED anode via 220 Ω | D7 |
| LED cathode | GND |
Using only AO is enough here. DO can stay unconnected.
Signal path in one line:
Gas → MQ-2 heated sensor → AO → A0 → code compares with threshold → D8 → buzzer
Step-by-step build
- Connect VCC, GND and AO of the MQ-2 to 5V, GND and A0.
- Connect the buzzer to D8 and GND, and the LED through the resistor to D7.
- Place the module in fresh, clean air, away from the kitchen, and power it from USB.
- Upload the sketch and open Serial Monitor at 9600 baud.
- Leave it running for the warm-up time. The sketch waits 3 minutes (the LED blinks), measures the baseline, and then arms the alarm.
- Watch the numbers for a while in clean air to see how much they vary, then adjust
MARGINif needed.
The code
// ElectroDai - MQ-2 gas alarm (LEARNING PROJECT, not a certified safety device)
// MQ-2 AO -> A0, buzzer -> D8, LED (via 220 ohm) -> D7
const int GAS_PIN = A0;
const int BUZZER_PIN = 8;
const int LED_PIN = 7;
const unsigned long WARMUP_MS = 180000; // 3 minutes of heater warm-up
const int MARGIN = 120; // alarm when reading > baseline + MARGIN
int baseline = 0;
int threshold = 0;
int readAverage() {
long sum = 0;
for (int i = 0; i < 20; i++) { // average 20 samples to smooth noise
sum += analogRead(GAS_PIN);
delay(25);
}
return sum / 20;
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Warming up MQ-2. Keep in clean air...");
unsigned long start = millis();
while (millis() - start < WARMUP_MS) {
digitalWrite(LED_PIN, (millis() / 500) % 2); // blink while warming
delay(20);
}
digitalWrite(LED_PIN, LOW);
baseline = readAverage();
threshold = baseline + MARGIN;
if (threshold >= 1023) {
Serial.println("Baseline too high. Check sensor and restart in clean air.");
while (true) {
digitalWrite(LED_PIN, (millis() / 250) % 2);
delay(20);
}
}
Serial.print("Baseline: ");
Serial.print(baseline);
Serial.print(" Alarm threshold: ");
Serial.println(threshold);
}
void loop() {
int value = readAverage();
Serial.print("Gas reading: ");
Serial.println(value);
if (value > threshold) {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
}
The alarm turns off by itself when the reading falls back below the threshold.
Testing with a multimeter and a safe trigger
- Supply: 5 V between VCC and GND. The heater can draw about 190 mA, so the USB supply must hold 5 V under load.
- AO in clean air: after warm-up the AO voltage should be fairly steady. Note it. Divide by 5 and multiply by 1023 to compare it with the Serial Monitor number.
- Safe test: Switch off power and disconnect AO from A0. Wire the outer legs of a 10 kΩ potentiometer to 5V and GND and its centre leg to A0. Set it low before restarting. Once the sketch arms, turn it up until the alarm sounds. This tests the code and outputs. Keep gas, smoke and flammable vapours away from the build.
Troubleshooting
Scroll sideways to see all columns.
| Symptom | Likely cause | Fix |
|---|---|---|
| Reading very high right after power-on | Heater still warming | Wait several minutes; for a new module, burn it in for many hours |
| Alarm at random | Threshold too close to the baseline or noisy supply | Increase MARGIN; use a stable USB supply |
| Reading never changes | Wrong pin or damaged module | Check AO goes to A0; check VCC |
| Alarm near the stove | Cooking fumes and steam | Move the sensor or accept it is not selective |
| Readings drift over days | Normal ageing and humidity | Restart the sketch in clean air to re-measure the baseline |
| Arduino resets | Weak USB port | Use a better USB cable or port |
FAQ
Can the MQ-2 tell me which gas is present?
No. It reacts to several gases and smoke. It gives one number, not an identification.
Can I use it to protect my home?
No. Use a certified gas detector for real safety. This project is for learning.
Why does it smell or get warm?
The built-in heater makes the sensor warm, and a new module may give off a slight smell during the first burn-in. This is normal, but if it gets too hot to touch or smells burnt, unplug it.
Do I need the DO pin?
Not for this sketch. AO gives a better picture, and the code sets its own threshold.
Next step: switch bigger loads from your alarm with Arduino Relay Module Guide: Switch Loads Safely.