The MQ-2 is a widely used gas sensor that detects combustible gases and smoke. It can sense LPG, methane, propane, hydrogen, alcohol vapors, and smoke -- making it useful for fire alarms, gas leak detectors, and air quality experiments.
The sensor module has two outputs: an analog output (AOUT) that gives you a voltage proportional to gas concentration, and a digital output (DOUT) that goes HIGH or LOW based on a threshold you set with the onboard potentiometer.
Safety warning: The MQ-2 is not a certified gas detection device. Do not rely on it for life-safety applications. It is suitable for hobby projects, learning, and experimentation only. For real gas detection, use certified commercial detectors.
🔗Key Specs
| Parameter | Value |
|---|---|
| Heater voltage | 5V (requires 5V supply) |
| Interface | Analog (AOUT) + Digital (DOUT) |
| Detectable gases | LPG, methane, propane, hydrogen, smoke, alcohol |
| Preheat time | ~20 seconds (minimum) |
| Burn-in time | 24 -- 48 hours (for stable readings) |
| Analog output range | 0V -- ~4V (at 5V supply) |
| Digital output | HIGH or LOW (threshold via pot) |
Important considerations:
- The MQ-2 heater requires 5V to operate properly. It will not work reliably at 3.3V.
- The analog output can exceed 3.3V, which is the maximum the ESP32's ADC can safely handle.
- The sensor draws significant current (~150 mA) due to the internal heater element.
MQ sensor family: The MQ-2 is a general-purpose combustible gas sensor. Sensirion also makes gas-specific variants: MQ-7 (carbon monoxide), MQ-4 (methane/natural gas), MQ-135 (air quality/NH3/benzene), and MQ-3 (alcohol). They all use the same wiring and code pattern — only the gas sensitivity curve differs. For VOC/CO2 measurement using I2C, see the SGP30.
🔗What You'll Need
| Component | Qty | Notes | Buy |
|---|---|---|---|
| ESP32 dev board | 1 | AliExpress | Amazon.de .co.uk .com | |
| MQ-2 gas/smoke sensor | 1 | AliExpress | Amazon.de .co.uk .com | |
| 10k ohm resistor | 1 | Voltage divider for analog output | AliExpress | Amazon.de .co.uk .com |
| 20k ohm resistor | 1 | Voltage divider for analog output | AliExpress | Amazon.de .co.uk .com |
| Breadboard | 1 | AliExpress | Amazon.de .co.uk .com | |
| Jumper wires | 5 | AliExpress | Amazon.de .co.uk .com |
Links marked Amazon/AliExpress are affiliate links. We may earn a small commission at no extra cost to you.
🔗Wiring
| MQ-2 Module Pin | ESP32 Pin | Notes |
|---|---|---|
| VCC | VIN (5V) | Powers the 5V heater |
| GND | GND | Common ground |
| DOUT | GPIO 13 | Digital threshold output (3.3V safe) |
| AOUT | GPIO 34 | Through a voltage divider (see below) |
Board variation note: Pin labels, GPIO numbers, and ADC channels vary between ESP32 boards. Always check your board's pinout diagram. The recommended starter board is the ESP32-WROOM-32 DevKit. GPIO 34 is on ADC1, which is important -- ADC2 (GPIOs 0, 2, 4, 12--15, 25--27) conflicts with WiFi. GPIOs 34--39 are input-only, which is fine for reading a sensor.
🔗Voltage Divider for Analog Output
The MQ-2's analog output can reach approximately 4V when powered at 5V. The ESP32's ADC input maximum is 3.3V. To safely read the analog output, use a simple voltage divider:
AOUT ---[10k]---+---[20k]--- GND
|
GPIO 34With $R_1 = 10\,\text{k}\Omega$ (from AOUT to GPIO) and $R_2 = 20\,\text{k}\Omega$ (from GPIO to GND):
$$V_{out} = V_{in} \cdot \frac{R_2}{R_1 + R_2} = 4\,\text{V} \cdot \frac{20}{30} \approx 2.67\,\text{V}$$
This keeps the voltage safely below 3.3V.
Simpler alternative: If you only need to detect gas presence (yes/no) rather than measure concentration, just use the DOUT digital output and skip the analog pin entirely. No voltage divider needed.
🔗Required Libraries
None -- both analogRead() and digitalRead() are built-in Arduino functions.
🔗Code Example
This sketch reads both the digital threshold output and the analog gas concentration value.
#define MQ2_DIGITAL_PIN 13
#define MQ2_ANALOG_PIN 34
void setup() {
Serial.begin(115200);
pinMode(MQ2_DIGITAL_PIN, INPUT);
Serial.println("MQ-2 sensor warming up...");
delay(20000); // Wait 20 seconds for heater to stabilize
Serial.println("MQ-2 sensor ready (full accuracy after 24-48hr burn-in).");
}
void loop() {
// Read digital output (HIGH = gas above threshold, LOW = below)
int gasDetected = digitalRead(MQ2_DIGITAL_PIN);
// Read analog output (0-4095 on ESP32's 12-bit ADC)
int analogValue = analogRead(MQ2_ANALOG_PIN);
// Convert ADC reading to approximate voltage
// Note: this is the divided voltage, not the raw sensor voltage
float voltage = analogValue * (3.3 / 4095.0);
Serial.print("Digital: ");
Serial.print(gasDetected == HIGH ? "GAS DETECTED" : "Clear");
Serial.print(" | Analog: ");
Serial.print(analogValue);
Serial.print(" (");
Serial.print(voltage, 2);
Serial.println(" V)");
if (gasDetected == HIGH) {
Serial.println(" >> WARNING: Gas concentration above threshold!");
}
delay(2000);
}🔗How It Works
The heating element -- Inside the MQ-2 is a tin dioxide ($\text{SnO}_2$) sensing layer wrapped around a small heater coil. The heater must reach its operating temperature (around 200--300 degrees C) for the sensor to work. This is why it needs a 5V supply and draws significant current.
Gas detection -- In clean air, the $\text{SnO}_2$ layer has a high resistance. When combustible gas molecules contact the heated surface, they react with absorbed oxygen, reducing the resistance. The module's circuit converts this resistance change into a voltage on the AOUT pin -- higher voltage means more gas.
Digital threshold -- The module includes a comparator with an adjustable potentiometer. When the analog signal exceeds the threshold you set, DOUT goes HIGH. Turn the potentiometer to adjust sensitivity.
Burn-in period -- Fresh MQ-2 sensors need 24--48 hours of continuous operation before their readings stabilize. During this time, the heater burns off manufacturing residues. After burn-in, the sensor still needs about 20 seconds of preheat each time it powers on.
ADC reading -- The ESP32's ADC returns a value from 0 to 4095 (12-bit resolution). With the voltage divider in place, you can convert this back to approximate sensor voltage if needed, though for most projects you will work with the raw ADC value or a simple threshold.
Tip: The MQ-2 is not selective -- it responds to many types of combustible gases. You cannot reliably distinguish between LPG and smoke using this sensor alone. For gas-specific detection, look at the MQ series variants (MQ-3 for alcohol, MQ-4 for methane, MQ-7 for carbon monoxide, etc.).
🔗Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Always reads HIGH (gas detected) | Threshold set too low on potentiometer | Turn the DOUT potentiometer to reduce sensitivity |
| Always reads LOW | Threshold set too high, or heater not powered | Check VCC is connected to 5V (VIN); adjust potentiometer |
| Analog readings are erratic | Sensor not burned in | Run the sensor continuously for 24--48 hours |
| Analog value always near max | No voltage divider and AOUT exceeds 3.3V | Add a voltage divider, or switch to digital output only |
| Analog reads 0 | Wrong GPIO pin, or pin not on ADC1 | Use a GPIO on ADC1 (GPIOs 32--39); verify wiring |
| Sensor gets very hot | Normal operation | The heater is designed to run hot; do not touch the metal can during operation |
| Readings drift over time | Temperature or humidity changes | MQ sensors are affected by ambient conditions; recalibrate periodically |
🔗Next Steps
- Set the digital threshold to trigger an LED or buzzer as a simple smoke alarm.
- Log analog readings over time to chart air quality trends (pair with WiFi and a dashboard).
- Combine with the SGP30 air quality sensor for both gas detection and VOC/CO2 monitoring.
- Build a kitchen safety monitor that alerts you via MQTT if gas or smoke is detected.