Build a gas and smoke leak detector that sounds a buzzer and lights an LED the moment it senses danger. It uses the popular MQ-2 sensor, which reacts to LPG, propane, methane and smoke — a practical safety project for any kitchen or lab bench.
Below you’ll find the wiring diagram, the Arduino code, and how to set the alarm threshold correctly. All parts are in stock at Arduino Corner.
What you’ll need
- Arduino Uno R3 (with USB cable) — runs the code
- MQ-2 Gas & Smoke Sensor — detects LPG, methane and smoke
- Active Buzzer — sounds the alarm
- Red LED — the warning light
- Solderless Breadboard — holds the LED and wiring
- Jumper Wires — connect the parts
You’ll also want a 220Ω resistor for the LED (included in most starter kits).
Wiring diagram

The same connections as a quick reference table:
| From (module) | To (Arduino Uno) |
|---|---|
| MQ-2 — VCC | 5V |
| MQ-2 — GND | GND |
| MQ-2 — AO | A0 |
| Buzzer — + | D8 |
| Buzzer — − | GND |
| LED — Anode (+) | D13 |
| LED — Cathode (−) | GND |
Tip: Put the LED’s anode (long leg) in series with a 220Ω resistor to D13, and the short leg to GND, so it doesn’t burn out.
How it works
The MQ-2 has a tiny heated sensing element whose resistance drops when gas or smoke touches it. That changes the voltage on its AO (analog out) pin, which the Arduino reads on A0. When the value climbs above your threshold, the code lights the LED and sounds the buzzer. The sensor needs a short warm-up after power-on to give stable readings.
The Arduino code
No external libraries are needed — only built-in Arduino functions.
#define GAS_PIN A0
#define BUZZER 8
#define LED 13
const int THRESHOLD = 400; // raise or lower this after testing
void setup() {
Serial.begin(9600);
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
delay(20000); // give the MQ-2 about 20s to warm up
}
void loop() {
int gas = analogRead(GAS_PIN);
Serial.println(gas);
if (gas > THRESHOLD) { // gas or smoke detected
digitalWrite(LED, HIGH);
tone(BUZZER, 1000); // sound the alarm
} else {
digitalWrite(LED, LOW);
noTone(BUZZER);
}
delay(200);
}
Understanding the code
The delay(20000) in setup() gives the MQ-2 about 20 seconds to warm up. In the loop, tone() makes the buzzer beep and noTone() silences it. Set THRESHOLD by watching the Serial Monitor in clean air, then holding a lighter (unlit, gas only) near the sensor and picking a value in between.
Troubleshooting
- Alarm is always on: your
THRESHOLDis too low, or the sensor hasn’t warmed up. Wait a minute and re-check the clean-air reading. - Buzzer stays silent: check that
+goes to D8 and−to GND — and that it’s an active buzzer. - Readings jump around: that’s normal for the first minute. Give it time to stabilise.
Take it further
- Add an I2C LCD that shows “SAFE” or “GAS!”.
- Use the relay to cut a gas valve or switch an exhaust fan automatically.
- Swap in an MQ-135 for general air-quality monitoring.
Frequently asked questions
What gases does the MQ-2 detect?
It responds to LPG, propane, methane, hydrogen, alcohol and smoke — ideal for a general kitchen gas-leak alarm.
Why does it need to warm up?
The internal heater has to reach temperature before readings are stable. Give it 20–60 seconds after power-on.
Can I use the digital (DO) pin instead?
Yes — the DO pin flips HIGH/LOW at a level you set with the onboard potentiometer, but the analog AO pin gives you finer control in code.
Related project guides
- Build a digital countdown timer with a TM1637
- Control home appliances with a relay module
- Build a temperature & humidity meter
Get the parts
Everything for this build is in stock at Smart Home | Arduino Corner — Shaheen Market, DAV College Road, Rawalpindi — and ships across Pakistan with cash on delivery. Grab the MQ-2 sensor, an active buzzer and some red LEDs. Message us on WhatsApp and we’ll put the whole kit together for you.