How to Use the MQ-2 Gas Sensor with Arduino Uno: LPG and Smoke Detector
How to Use the MQ-2 Gas Sensor with Arduino Uno: LPG and Smoke Detector
Intro
Almost every home in Pakistan cooks on an LPG cylinder, and a small gas leak has no colour and is easy to miss until it is dangerous. A gas sensor is the part that notices what our noses miss.
The MQ-2 is the most common gas sensor in student projects, and the cheapest way to learn gas detection. It is one small blue module with a metal mesh cap that senses LPG, smoke, alcohol vapour, propane, methane and hydrogen. It cannot tell you which gas it smells, and it is not a certified safety alarm, but it is a very good first sensor.
In this guide, you will build an LPG and smoke detector with an MQ-2 and an Arduino Uno. The Arduino reads the gas level twice a second, prints it on the Serial Monitor, and switches on an LED and a buzzer past a threshold. You will learn how the sensor works, what its two outputs mean, why it needs warm-up time, and why its numbers are relative, not calibrated ppm. The code uses millis() timing with no delay(), and the project is a solid base for a final-year project.
Components Required
| Part | Quantity | Purpose | Price |
|---|---|---|---|
| MQ-2 Gas Sensor Module | 1 | Detects LPG, smoke, alcohol vapour, propane, methane and hydrogen | Available on order — WhatsApp |
| Arduino Uno R3 DIP | 1 | Reads the sensor and runs the alarm code | Rs 1,200 |
| MB102 solderless breadboard, 830 points | 1 | Holds the LED, resistor and buzzer without soldering | Rs 250 |
| Active Piezo Buzzer 3V–12V | 1 | Sounds the alarm when gas is detected | Rs 35 |
| 5 mm LED (red) | 1 | Lights up when gas is detected | Rs 6 |
| 220 Ω resistor | 1 | Protects the LED from too much current | Available on order — WhatsApp |
| 40-pin male-to-male jumper wires | 8 | Connects the module, LED and buzzer to the Uno | Rs 180 |
The priced parts come to about Rs 1,671. The MQ-2 module and the 220 Ω resistor are available on order — message us on WhatsApp for the current price.
How an MQ-2 Gas Sensor Works
Under the metal mesh cap sits the sensing element: a tiny ceramic tube coated with tin dioxide (SnO2), with a heater coil inside that keeps the coating hot. When the coating is hot, gas molecules react on its surface and its electrical resistance falls — more gas means lower resistance.
The module turns that resistance into a voltage with a simple voltage divider: the sensor is one half and a fixed load resistor on the module is the other. As the sensor resistance falls, the voltage at the middle rises. That voltage is the analog output, A0 — low in clean air, higher when gas is present.
The module also has a second output, D0, made by an LM393 comparator chip. It compares the same analog voltage with a threshold you set by turning the small blue potentiometer with a screwdriver. When the gas level crosses that threshold, D0 switches from HIGH to LOW and a small LED on the module lights up. So you get two outputs: A0 for a continuous level, and D0 for a simple yes/no alarm.
Two honest limits matter before you trust the numbers. First, the MQ-2 is broadband: it reacts to LPG, propane, methane, hydrogen, alcohol and smoke all at once, and cannot tell you which one it is smelling — a reading of 500 could be LPG or alcohol vapour from hand sanitiser. Second, the analog reading is relative, not calibrated. A real ppm figure needs the sensor resistance Rs, the clean-air reference R0, and the Rs/R0 curves in the datasheet for one specific gas. This guide works with the raw reading and a threshold you set yourself.
Warm-up: why the sensor needs time
The heater needs time to reach a stable temperature. A brand-new sensor, or one that has sat unpowered for a long time, needs a long first burn-in — the datasheet characterises the sensor after more than 48 hours of preheat. After that, each power-on needs a few minutes before the readings stop drifting. This sketch waits 60 seconds after startup before it allows the alarm, and prints the readings during warm-up so you can watch them settle. Sixty seconds is a practical minimum for a classroom demo, not a substitute for the datasheet burn-in — leave the sensor powered much longer for stable numbers. The metal mesh gets hot, so do not touch it.
Key specifications of the MQ-2 sensor (Hanwei MQ-2 datasheet):
| Specification | Value |
|---|---|
| Operating voltage | 5 V DC |
| Heater voltage | 5.0 V ± 0.2 V |
| Heater resistance | 31 Ω ± 3 Ω at room temperature |
| Heater consumption | Up to 900 mW, about 150–160 mA at 5 V |
| Detected gases | LPG, propane, butane, methane, hydrogen, alcohol, smoke |
| Detection range | 300–10,000 ppm (depends on the gas) |
| Sensing resistance Rs | 2 kΩ–20 kΩ in 2,000 ppm propane |
| Preheat / burn-in time | Over 48 hours for a new sensor |
| Warm-up after power-on | A few minutes before readings settle |
| Operating temperature | −20 °C to +50 °C |
Because the heater draws about 150–160 mA, power the Uno from a proper 5 V USB supply, not a weak laptop port. A weak supply drops in voltage when the heater switches on, and the readings drift with it.
MQ-2 Module Pinout and Wiring with Arduino Uno
The module has four pins in a row, labelled on the board. Some modules print A0 and D0, others print AO and DO — they mean the same two outputs.
| MQ-2 module pin | Meaning | Arduino Uno pin | Wire colour used here |
|---|---|---|---|
| VCC | 5 V power for the heater and the circuit | 5V | Red |
| GND | Ground | GND | Black |
| A0 | Analog output — voltage rises with gas level | A0 | Blue |
| D0 | Digital output — LOW when gas crosses the threshold | D2 | Yellow |
The LED and the buzzer are the alarm. They sit on the MB102 breadboard with the Uno.
| Alarm part | Connection |
|---|---|
| LED anode (+) | D8, through the 220 Ω resistor |
| LED cathode (−) | GND |
| Active buzzer + | D9 |
| Active buzzer − | GND |
The resistor goes between D8 and the LED anode. The buzzer in this guide is the active type, which sounds when its pin is HIGH — a passive buzzer looks the same but needs a tone() signal, so check which type you have first.

Library Installation
No library is needed. The MQ-2 is read with plain analogRead() and digitalRead(), so this project uses only the built-in Arduino functions.
Complete Arduino Code
/*
MQ-2 Gas Sensor with Arduino Uno - LPG and Smoke Detector
The MQ-2 module reads gas level two ways at once:
- Analog output (A0): a raw value from 0 to 1023 that rises
as gas concentration rises. This is a RELATIVE reading, not
a calibrated ppm figure. Turning it into ppm needs Rs/R0
calibration against the datasheet sensitivity curves.
- Digital output (D0): goes LOW when the gas level crosses
the threshold set by the small potentiometer on the module.
When gas is detected, the LED lights and the buzzer beeps in
a non-blocking pattern. All timing uses millis() - there is
no delay() anywhere in loop().
Warm-up: the MQ-2 heater needs time before readings settle.
The datasheet preheats a brand-new sensor for over 48 hours;
in practice, give it at least 2 to 5 minutes after power-on
before trusting the readings. This sketch waits 60 seconds
after startup before it allows the alarm, and it prints the
readings during warm-up so you can watch them settle.
The sensor body gets hot while it runs - do not touch the
metal mesh. The MQ-2 cannot tell gases apart, and it is not
a certified safety alarm.
Wiring:
MQ-2 VCC -> 5V (red)
MQ-2 GND -> GND (black)
MQ-2 A0 -> A0 (blue)
MQ-2 D0 -> D2 (yellow)
LED anode (+) -> D8 through a 220 ohm resistor
LED cathode (-) -> GND
Active buzzer + -> D9, buzzer - -> GND
*/
#define MQ2_ANALOG_PIN A0 // MQ-2 A0 (analog output)
#define MQ2_DIGITAL_PIN 2 // MQ-2 D0 (digital output, LOW = gas over threshold)
#define LED_PIN 8 // LED anode through a 220 ohm resistor
#define BUZZER_PIN 9 // Active buzzer + (sounds on HIGH)
const unsigned long READ_INTERVAL_MS = 500; // Read the sensor twice a second
const unsigned long WARMUP_MS = 60000; // 60 s warm-up before the alarm arms
const int GAS_ALERT_LEVEL = 400; // Raw analog level that triggers the alarm
const unsigned long BEEP_ON_MS = 200; // Buzzer beep length
const unsigned long BEEP_OFF_MS = 400; // Silence between beeps
unsigned long lastRead = 0;
unsigned long warmupStart = 0;
unsigned long lastBeepToggle = 0;
bool buzzerOn = false;
bool alertActive = false;
bool warmupDone = false;
void setup() {
Serial.begin(9600);
pinMode(MQ2_DIGITAL_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
warmupStart = millis();
Serial.println("MQ-2 Gas Detector ready");
Serial.println("Warming up for 60 seconds - readings will drift at first");
Serial.println("Keep the sensor away from gas while it warms up");
}
void loop() {
unsigned long now = millis();
// End the warm-up period once 60 seconds have passed.
if (!warmupDone && now - warmupStart >= WARMUP_MS) {
warmupDone = true;
Serial.println("Warm-up done - sensor ready");
}
// Read the sensor on a non-blocking 500 ms schedule.
if (now - lastRead >= READ_INTERVAL_MS) {
lastRead = now;
int gasValue = analogRead(MQ2_ANALOG_PIN); // 0 to 1023, relative level
int digitalState = digitalRead(MQ2_DIGITAL_PIN); // LOW = over threshold
// Alarm if the analog level is high OR the module's own
// comparator says the threshold is crossed. During warm-up
// we only print; the alarm stays off.
alertActive = warmupDone &&
(gasValue >= GAS_ALERT_LEVEL || digitalState == LOW);
Serial.print("Gas level: ");
Serial.print(gasValue);
Serial.print(" D0: ");
Serial.print(digitalState == LOW ? "GAS" : "clear");
Serial.print(" Status: ");
if (!warmupDone) {
Serial.println("WARMING UP");
} else if (alertActive) {
Serial.println("ALERT - gas detected!");
} else {
Serial.println("Air clear");
}
// LED mirrors the alert state.
digitalWrite(LED_PIN, alertActive ? HIGH : LOW);
}
// Beep pattern, also non-blocking: 200 ms on, 400 ms off.
if (alertActive) {
if (buzzerOn && now - lastBeepToggle >= BEEP_ON_MS) {
buzzerOn = false;
digitalWrite(BUZZER_PIN, LOW);
lastBeepToggle = now;
} else if (!buzzerOn && now - lastBeepToggle >= BEEP_OFF_MS) {
buzzerOn = true;
digitalWrite(BUZZER_PIN, HIGH);
lastBeepToggle = now;
}
} else if (buzzerOn) {
// Alert ended: make sure the buzzer is silent.
buzzerOn = false;
digitalWrite(BUZZER_PIN, LOW);
}
}Code Explanation
The four #define lines name the pins so the code reads like the wiring table — change the wiring, and you change one line.
setup() opens the Serial port at 9600, sets D2 as an input, and sets the LED and buzzer pins as outputs, both starting LOW so the alarm is silent at power-on. It records the warm-up start time and prints three lines telling you what is happening.
loop() does three jobs, all on millis() timing. First, it ends the warm-up once 60 seconds have passed. Second, every 500 ms it reads the sensor: analogRead(A0) gives a raw value from 0 to 1023, and digitalRead(2) gives the comparator’s verdict. The alarm turns on when the analog value reaches GAS_ALERT_LEVEL (400) or when D0 reads LOW — either one is enough — but only after warm-up, so drifting readings cannot set it off. Each reading is printed with the D0 state and a status word, and the LED matches the alert state.
Third, the beep pattern runs on its own millis() timer: while the alert is active, the buzzer sounds for 200 ms and stays silent for 400 ms. When the alert ends, the buzzer is switched off. There is no delay() anywhere, so the loop keeps reading on schedule even while the buzzer beeps.
The one number you will probably change is GAS_ALERT_LEVEL. It is a raw analog value, not ppm, and the right value depends on your room and sensor. Note your clean-air reading after warm-up, then set the threshold clearly above it. If the alarm sounds in clean air, the threshold is too low; if it never sounds, it is too high — or the potentiometer needs adjusting (see troubleshooting tip 4).
Demonstration: Expected Serial Monitor Output
If you have never uploaded code before, see our guide on how to upload code in Arduino Uno.
Upload the code, then open the Serial Monitor with Tools > Serial Monitor and set the baud rate to 9600. You should see output like this — your exact numbers will be different, because every sensor and every room has its own clean-air baseline:
MQ-2 Gas Detector ready Warming up for 60 seconds - readings will drift at first Keep the sensor away from gas while it warms up Gas level: 118 D0: clear Status: WARMING UP Gas level: 121 D0: clear Status: WARMING UP Warm-up done - sensor ready Gas level: 124 D0: clear Status: Air clear Gas level: 486 D0: GAS Status: ALERT - gas detected! Gas level: 512 D0: GAS Status: ALERT - gas detected! Gas level: 130 D0: clear Status: Air clear
To test it safely, hold an unlit gas lighter near the sensor and press it for a second without sparking a flame — the gas alone pushes the reading up and sets the alarm off. Keep the sensor away from any flame. When the gas clears, the reading falls back and the alarm stops.
Troubleshooting
- The readings keep drifting for the first few minutes. That is normal warm-up — wait at least 2 to 5 minutes after power-on before judging them, and give a brand-new sensor a much longer first burn-in. Do not set your threshold from first-minute readings.
- The reading is stuck near 0 and never moves. Check the wiring: A0 on the Uno’s A0 pin, VCC on 5V, GND on GND. A reading of 0 usually means the analog pin is not connected, or the module has no power — its small power LED should be lit.
- The reading is very high all the time, even in clean air. Alcohol vapour, cigarette smoke, cooking fumes and cleaning sprays all trigger the MQ-2. Move the sensor to clean air and watch the reading fall; if it stays high, it may still be warming up.
- D0 never goes LOW, even when the analog reading is high. The threshold potentiometer is set too high. While gas is present, turn the small blue potentiometer slowly until the module’s D0 LED changes state — a quarter turn is a big change.
- The buzzer sounds and the LED stays on in clean air. Your GAS_ALERT_LEVEL is below your room’s clean-air reading — note the clean-air value after warm-up and set the threshold clearly above it. Also check the potentiometer: if D0 is LOW in clean air, the module’s threshold is set too low.
- The LED works but the buzzer never sounds. Check the polarity: the + pin goes to D9 and the − pin to GND. Also check the type — this guide uses an active buzzer, which sounds on a plain HIGH signal, while a passive buzzer needs tone() and stays silent on digitalWrite().
- The code uploads but the Serial Monitor is empty. Check the baud rate — it must be 9600, the same as Serial.begin(9600) — and that you selected Arduino Uno and the right COM port under Tools.
Project Ideas and FYP Ideas Using an MQ-2 Gas Sensor
- LPG leak detector for a home kitchen. Mount the MQ-2 near the floor by the gas cylinder — LPG is heavier than air and collects low down. Add a relay to switch on an exhaust fan when gas is detected, and an LCD to show the level.
- Hostel room smoke alarm. Combine the MQ-2 with a buzzer and a bright LED, powered by a 9 V battery through the Uno’s barrel jack so it works during a power cut. Add a push button to silence it after checking the room.
- Kitchen safety system with SMS alerts. Add a SIM900 GSM module so the Arduino sends an SMS to the homeowner when gas is detected and nobody is home — a strong final-year project.
- Indoor air quality monitor. Combine the MQ-2 with a DHT11 sensor and an LCD1602 to show gas level, temperature and humidity on one screen, and log the readings over Serial to plot how the air changes through the day.
- CNG vehicle gas leak detector. Mount the MQ-2 near the CNG cylinder in a car or rickshaw and put the buzzer and LED on the dashboard — the same circuit as this guide, with longer wires and a 12 V to 5 V supply for the Uno.
Frequently Asked Questions
What gases can the MQ-2 detect?
LPG, propane, butane, methane, hydrogen, alcohol vapour and smoke — all at once, without telling you which one it is.
What is the MQ-2 gas sensor price in Pakistan?
The MQ-2 module is available on order from QKZee Technologies — message us on WhatsApp at +92-302-8577025 for the current price. The rest of this project costs about Rs 1,671 with the parts listed above.
Does the MQ-2 need a library?
No — plain analogRead() and digitalRead() are all it needs.
Can the MQ-2 measure exact ppm?
No, not on its own. The analog reading is a relative level from 0 to 1023. A real ppm figure needs Rs/R0 calibration against the datasheet curves for one specific gas. For learning and for an alarm, the raw reading and a threshold are enough.
Can I use the MQ-2 as a real gas safety alarm at home?
No, not as your only safety device. The MQ-2 is a learning sensor, not a certified alarm: it cannot tell gases apart, its readings drift, and it needs warm-up time. Keep a certified gas alarm for real home safety.
Conclusion
The MQ-2 turns an invisible problem into a number on the Serial Monitor and a buzzer you can hear. You have learned the three things that matter: the heated tin dioxide layer that makes the sensor work, the two outputs — A0 for a level and D0 for an alarm — and the honest limits, that the readings are relative, that the sensor cannot tell gases apart, and that it needs warm-up time. The detector you built is a complete project, and the base for bigger ones: add a relay and it switches on a fan, add a GSM module and it sends an SMS, add a DHT11 and an LCD and it becomes an air quality monitor.
Components for this project are available at QKZee Technologies, with nationwide Cash on Delivery across Pakistan. Message us on WhatsApp at +92-302-8577025 to order the MQ-2 module and the rest of the parts, or visit our shop at https://qkzeetech.com/shop-2/.
If you missed them, our HC-SR04 ultrasonic sensor guide, our DHT11 temperature and humidity guide and our relay module guide build on the same wiring and millis() skills.





