The MQ2 gas sensor outputs two distinct signals: an analog voltage (0V to VCC) that scales inversely with the sensor's internal resistance, and a digital HIGH/LOW TTL signal triggered by an onboard comparator. To convert the raw analog reading into a meaningful parts-per-million (PPM) value, you cannot rely on simple linear mapping. You must calculate the sensor's current resistance (Rs), compare it against a clean-air baseline resistance (R0), and apply a logarithmic regression curve specific to your target gas.
How the MQ2 Gas Sensor Actually Works
The MQ2 relies on a tin dioxide (SnO2) semiconductor layer heated by an internal nichrome coil. In clean air, oxygen molecules adsorb onto the SnO2 surface, trapping electrons and creating a high-resistance depletion region. When reducing gases like LPG, propane, methane, hydrogen, or carbon monoxide are introduced, they react with the adsorbed oxygen, releasing electrons back into the conduction band and dropping the sensor's internal resistance.
This resistance drop forms a voltage divider with the fixed load resistor (RL) on the sensor module's PCB. As the SnO2 resistance falls in the presence of gas, the voltage at the analog output pin rises. The module also routes this analog voltage to an LM393 comparator, which flips a digital output pin LOW when the gas concentration crosses the threshold set by the blue trimpot.
Wiring the MQ2 to Arduino and ESP32
A critical mistake makers frequently make is conflating the sensor's power requirements with its logic levels. The MQ2 heater requires a stable 5V supply drawing roughly 150mA to reach its 300°C operating temperature. However, the analog output will swing from 0V to 5V, which will instantly destroy the 3.3V ADC pins on an ESP32 or Raspberry Pi Pico.
Pinout and Supply Specifications
| Module Pin | Signal Type | Supply / Voltage Range | Connection Target (ESP32) |
|---|---|---|---|
| VCC | Power | 4.5V to 5.5V (5V nominal) | 5V Pin (VIN) |
| GND | Ground | 0V | GND |
| AOUT | Analog Output | 0V to VCC (Voltage) | GPIO 34 (via 10k/10k divider) |
| DOUT | Digital Output | 0V to VCC (TTL Logic) | GPIO 32 (or any digital pin) |
The digital output (DOUT) is largely useless for PPM tracking but excellent for simple fire or gas leak alarms. It outputs a standard TTL HIGH (5V) when gas is below the trimpot threshold, and pulls LOW (0V) when the threshold is exceeded. Note that 5V TTL is generally tolerated by ESP32 digital input pins, but a 3.3V Zener diode clamp is recommended for long-term reliability.
Converting Raw ADC Readings to PPM
Translating the ADC reading into PPM requires extracting the sensor's internal resistance. According to the Hanwei Electronics MQ-2 datasheet, the relationship between the sensor resistance (Rs) and the load resistor (RL) dictates the output voltage.
Step 1: Identify Your Load Resistor (RL)
Look at the SMD resistor on your module's PCB. If it reads 103, RL = 10,000Ω (10kΩ). If it reads 102, RL = 1,000Ω (1kΩ). Cheap clone modules almost universally use 1kΩ to maximize the voltage swing for 5V Arduinos, but assuming 10kΩ will completely break your math.
Step 2: Calculate Sensor Resistance (Rs)
First, convert your raw ADC value back into voltage. For a 10-bit Arduino ADC: V_RL = ADC_raw * (5.0 / 1023.0). For a 12-bit ESP32 ADC (with the 10k/10k divider mentioned above): V_RL = (ADC_raw * (3.1 / 4095.0)) * 2.0.
Next, calculate Rs using the voltage divider formula:
Rs = RL * ((Vc / V_RL) - 1)
Note: Vc is your actual measured supply voltage. Do not assume it is exactly 5.00V; measure it with a multimeter and hardcode that exact value (e.g., 4.85V) into your sketch for accuracy.
Step 3: Establish the Clean Air Baseline (R0)
In perfectly clean air, the ratio of Rs to R0 is approximately 9.8. To find your specific R0, power the sensor on in a known clean environment (outdoors or near an open window) and let it burn in for 15 minutes. Calculate Rs using the formula above, then divide by 9.8:
R0 = Rs_clean_air / 9.8
Step 4: Apply the Log-Log PPM Regression
The MQUnifiedsensor library and standard datasheet curves use the formula PPM = a * (Rs/R0)^b. The coefficients (a, b) change depending on the gas you are targeting. Here are the standard regression coefficients derived from the datasheet log-log graphs:
- LPG: a = 200, b = -2.1
- Carbon Monoxide (CO): a = 300, b = -2.3
- Smoke: a = 400, b = -2.4
For example, if your calculated Rs/R0 ratio is 2.5, and you are measuring LPG: PPM = 200 * (2.5)^-2.1 = 29.4 PPM.
Common Interference Sources and Failure Modes
The MQ2 is a broadband combustible gas sensor, meaning it lacks chemical specificity. If you are trying to measure natural gas leaks, a nearby butane lighter, or even high concentrations of alcohol vapor from hand sanitizer will trigger identical resistance drops. This cross-sensitivity is a hardware limitation of the SnO2 chemistry, not a software bug.
Environmental factors heavily skew the Rs/R0 ratio. The Espressif ADC documentation notes that temperature and humidity affect both the sensor chemistry and the microcontroller's ADC reference voltage. High ambient humidity (above 70% RH) artificially lowers the SnO2 resistance, mimicking a gas leak. For serious deployments, you must pair the MQ2 with an SHT31 or BME280 sensor and apply a software compensation curve based on relative humidity.
Finally, voltage sag is a frequent failure mode. The internal heater draws a 150mA spike upon power-up. If you are powering your ESP32 and the MQ2 from a weak laptop USB port, this spike will cause a brownout, resetting your microcontroller or corrupting the R0 calibration stored in EEPROM.
Frequently Asked Questions
Why is my MQ2 gas sensor getting extremely hot to the touch?
This is completely normal and required for operation. The internal nichrome heating coil maintains the SnO2 sensing element at roughly 300°C (572°F). The metal mesh cap is designed to dissipate this heat. The sensor will draw a continuous 150mA to 180mA just to maintain this temperature, which is why it feels hot enough to be uncomfortable to hold for more than a few seconds.
Can I power the MQ2 sensor directly from the ESP32's 3V3 pin?
No. The MQ2 heater strictly requires 4.5V to 5.5V to reach the necessary catalytic temperature. If you power it from 3.3V, the sensor will remain cold, the SnO2 layer will not react to gases, and your analog output will remain stuck at zero. Always power the VCC pin from a 5V source, and use a voltage divider for the AOUT signal pin.
How do I calibrate the MQ2 R0 value for my specific room?
Place the sensor outdoors or in a well-ventilated area away from exhaust, paint fumes, or cooking gases. Power it on and let it heat up for at least 15 minutes (or 24 hours if it is brand new). Read the analog pin, convert it to voltage, calculate Rs using your module's specific RL value, and divide that Rs value by 9.8. Hardcode this resulting number into your sketch as your R0 variable.
Why does my MQ2 digital pin (DOUT) never change state when exposed to gas?
The DOUT pin is controlled by the LM393 comparator and the blue trimpot on the module. If it never changes state, the threshold is set too high or too low. Expose the sensor to a known gas source (like a clicking butane lighter without igniting it), and use a small Phillips screwdriver to slowly turn the blue trimpot until the onboard LED toggles and the DOUT pin flips from HIGH to LOW.






