MQ2 Gas Sensor with Arduino — Wiring, Code & Alarm Project

Welcome to this beginner-friendly Arduino tutorial on the mq2 combustible gas. By the end of the guide, you'll wire the module to an Arduino UNO, flash a short sketch, and read live values on the Serial Monitor — no prior electronics experience required.
Need this built for you? I do custom Arduino, ESP32 and Proteus projects from $15.
What you'll learn
- How the module works in plain language
- The exact parts you need and how to wire them safely
- The full Arduino IDE sketch with comments
- Common issues and how to fix them


Arduino code
Open the Arduino IDE, paste the sketch below into a new file, install any libraries the sketch #includes (Tools → Manage Libraries), select your board and COM port, then click Upload.
// Sensor pins pin D6 LED output, pin A0 analog Input
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to calling function
int readSensor() {
unsigned int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 10-bit data to 8-bit data
if (outputValue > 65)
analogWrite(ledPin, outputValue); // generate PWM signal
else
digitalWrite(ledPin, LOW);
return outputValue; // Return analog moisture value
}

How it works
The sketch initialises serial communication and the mq2 combustible gas driver in setup(), then in loop() it samples the sensor at a regular interval and prints the result to the Serial Monitor at 9600 baud. Open the Serial Monitor (Ctrl+Shift+M) after upload to see live readings.
Troubleshooting checklist
- No readings: verify the baud rate in Serial Monitor matches the sketch (usually 9600).
- Garbage characters: wrong baud rate or loose GND wire.
- Library not found: install the exact library referenced in the
#includeline via Library Manager. - Sensor not detected (I²C): run an I²C scanner sketch to confirm the address.
What to build next
Once the basic readout works, try logging values to an SD card, sending them over Wi-Fi with an ESP32, or pushing them to a Blynk IoT dashboard. Pair this module with our simulator round-up to prototype the circuit before soldering.
Stuck on your own build? Send me your Arduino / ESP32 / Proteus project on Fiverr — from $15, you don't pay if it doesn't work.
You may also like
Frequently Asked Questions
Q.How do I wire the MQ2 gas sensor to Arduino?
Connect VCC to 5V, GND to GND, and A0 (analog out) to Arduino A0. Optionally wire D0 to a digital pin if you want a simple threshold output instead of analogRead().
Q.Why do MQ2 readings drift or saturate at 1023?
The sensor needs a 24–48 hour burn-in on first power-up, and its heater draws ~150 mA — powering it from the Arduino's 5V pin with other loads can starve it. Give it 2–3 minutes to stabilise each session before trusting values.
Q.Can the MQ2 give parts-per-million (ppm) values?
Not directly. The MQ2's analog output is proportional to concentration, but converting to true ppm needs the sensor's log-log curve from its datasheet and per-module calibration. Most hobby projects use a relative threshold instead.
Q.What gases does the MQ2 detect?
Smoke, LPG, propane, methane, hydrogen, and alcohol vapours. It is a broad-spectrum sensor — good for alarms, not for identifying which gas triggered them.
The Engineer Post
Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.
Related projects

Water Level Sensor with Arduino
Step-by-step Arduino tutorial: wire the water level to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Rain Detection Sensor with Arduino
Step-by-step Arduino tutorial: wire the rain detection to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Pulse Sensor with Arduino
Step-by-step Arduino tutorial: wire the pulse to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Interfacing SR04T/SR04M Waterproof UltraSonic Sensor with Arduino Uno
Step-by-step Arduino tutorial: wire the the sr04t, sr04m waterproof ultrasonic to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.

Interfacing TSL25911 Ambient Light Sensor with Arduino
Step-by-step Arduino tutorial: wire the tsl25911 ambient light to an Arduino UNO, upload the example sketch, and read live values on the Serial Monitor.
