All articles

    MQ Gas Sensors (MQ-2, MQ-135, MQ-7): How They Work and How to Read Them

    How MQ gas sensors work and how to use them with Arduino: popular types, heater current, warm-up and burn-in, outputs, calculating sensor resistance and R0, practical alarm code, where to mount them, real CO2 sensors, relay risks, and safety warnings.

    Updated 7 min read

    MQ sensors are the most popular way to build "gas detector" and "air quality" projects. They're cheap and easy, but they have details that, if you miss them, will make the readings meaningless.

    The sensor contains a heater, and gas changes the resistance of the sensing layer
    The sensor contains a heater, and gas changes the resistance of the sensing layer

    How do they work?

    Inside the metal mesh there's a tin oxide layer and a small heater. When the layer is hot and comes into contact with certain gases, its resistance drops. The module converts that to a voltage with a voltage divider that you read with analogRead.

    Popular types

    SensorMost sensitive to
    MQ-2Cooking gas (LPG), smoke and methane
    MQ-135General air quality (ammonia, approximate CO2, vapours)
    MQ-7Carbon monoxide (CO); needs heating cycled between two voltages
    MQ-3Alcohol (as in breath testers)
    MQ-4Methane (natural gas)
    MQ-9Carbon monoxide and flammable gases

    They all respond to more than one gas, so the reading doesn't tell you "exactly which gas this is".

    The heater: watch the current

    • The heater takes about 150 mA at 5 V (≈ 0.8 W).
    • That's far more than an Arduino pin can supply. Connect the module's VCC to 5V from a good source, not to a pin.
    • ESP32: the module needs 5 V, and its analog output can exceed 3.3 V, so bring it down with a voltage divider.
    • The sensor really does get hot, so don't mount it pressed against plastic.
    • A battery isn't suitable: 150 mA continuously means a 2000 mAh battery is empty in under 14 hours. The calculation is in the battery life article. MQ projects run from an adapter.

    Warm-up

    • When you first power it, the reading drifts until it heats up (a minute or more).
    • A new sensor needs a long burn-in (24–48 hours running continuously) for the reading to settle.

    The outputs

    • A0: analog, and the most important one.
    • D0: digital, switching when the reading crosses a threshold you set with the potentiometer on the module.

    From voltage to sensor resistance

    The module has a fixed load resistor (RL, usually 1 kΩ and sometimes 10 kΩ; check the marking on the SMD resistor next to the sensor). From the voltage you read, you can calculate the sensor's own resistance:

    Rs = RL × (Vcc − Vout) ÷ Vout

    And the datasheet doesn't give the sensor's resistance in ohms; it gives the ratio between its resistance at a given concentration and its resistance in clean air (Rs ÷ R0), because every sensor differs from the next. So the steps are:

    1. Run the sensor in clean air until it settles (after the burn-in).
    2. Calculate R0: for the MQ-2, for example, the datasheet says the ratio in clean air is about 9.8, so R0 = Rs ÷ 9.8.
    3. In operation: calculate Rs ÷ R0, and compare it with the curve for the gas you care about in the datasheet to estimate the concentration.

    And even after all that, the figure is an estimate. Temperature and humidity shift it, and sensors vary from batch to batch.

    Practical alarm code

    Instead of complicated figures, the easier and steadier approach: record a baseline after warm-up, and alarm if the reading rises well above it:

    const int WARMUP_MS = 120000;   // two minutes of warm-up
    int baseline = 0;
    bool alarm = false;
    
    void setup() {
      pinMode(8, OUTPUT);           // buzzer
      delay(WARMUP_MS);
      long sum = 0;
      for (int i = 0; i < 50; i++) { sum += analogRead(A0); delay(100); }
      baseline = sum / 50;
    }
    
    void loop() {
      int v = analogRead(A0);
      if (!alarm && v > baseline * 1.6) alarm = true;   // big rise: alarm
      if (alarm && v < baseline * 1.3)  alarm = false;  // back near baseline: stop
      digitalWrite(8, alarm);
      delay(500);
    }

    The two figures 1.6 and 1.3 create a gap between switching on and off so the buzzer doesn't keep chattering at the threshold, the same hysteresis idea as in the light and temperature sensors article. Test with an unlit gas lighter near the sensor (without lighting it, and away from any flame) and tune the figures to your sensor.

    Where to mount it

    • Cooking gas (LPG) is heavier than air, so it sinks and collects near the floor. Mount the sensor low, about 30 cm off the floor near the stove.
    • Natural gas (methane) is lighter than air and rises. Mount the sensor high, near the ceiling.
    • Carbon monoxide mixes with the air, so it's mounted at roughly breathing height.

    And away from direct steam and grease, because they coat the sensing layer and spoil its readings over time.

    Reading it properly

    The reading is relative: read the sensor in clean air after it's warmed up, and record that value as the baseline. After that, if the reading moves far from the baseline, something's there. Temperature and humidity affect it too.

    Converting the reading into an accurate ppm figure requires calibration with a known gas and the datasheet curves, and even then the accuracy is limited.

    If you really want to measure CO2 or air quality

    The MQ-135 is widely sold as a "CO2 sensor", but it responds to almost anything, and its CO2 reading is a rough estimate. If the project needs a real CO2 figure (say, ventilation for a classroom or a room):

    • NDIR sensors such as the MH-Z19 or SCD40 measure CO2 by infrared; they're accurate and don't need a long warm-up.
    • Digital air quality sensors (over I2C) give you a ready-made index for volatile organic compounds, and they're easier than calibrating an MQ.

    Be careful with relays and fans

    A popular idea: if the gas rises, a relay switches on an extractor fan. But ordinary relays and motors produce a spark when switching on and off, and a spark where gas is leaking could be what ignites it. If you do this, keep the relay and fan outside the leak area; better still, settle for an audible alarm, and close the valve and open the windows by hand. The rules for working with 220 V are in the mains safety article.

    Important warning

    MQ sensors are not a certified alarm. As a learning project or an extra warning, fine, but don't rely on them to protect lives from cooking gas or carbon monoxide. For real protection, use commercial alarms certified to safety standards.

    FAQ

    The sensor smells strange or gives off a little smoke when I first power it? During the first few hours of burn-in this can happen as the layer cleans itself. If it continues, or the module gets abnormally hot, disconnect it and check the supply voltage.

    The reading creeps up by itself over time with no gas? Temperature and humidity change the reading, and the sensor itself changes a little with age. That's why we re-record the baseline periodically, or each time the device starts after warm-up, as long as the air is clean.

    Can I use an MQ-2 for smoke instead of an ordinary smoke detector? It does respond to smoke, and it's a nice learning project. But a commercial smoke detector is designed and tested to catch a fire early, and that's what should be installed at home.

    Can I run several MQ sensors on one Arduino? Yes, each on its own analog pin, but the supply must handle the total heater current: 3 sensors means about half an amp at 5 V, which is a lot for the Uno's 5V pin when powered from USB. Use a separate 5 V supply with a common ground. Read the powering the Arduino article.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.