Parts

MQ-2 gas sensor

The MQ-2 on its FC-22 comparator board: a steel mesh can on six legs, two lamps, a trimmer and four pins. Aliases: gas sensor, smoke sensor, LPG sensor.

This part has a page of its own, with a note on using it and a circuit to try: the part page.

Inside the can is a ceramic tube coated with tin dioxide, with a heater coil down the middle. Hot tin dioxide holds oxygen on its surface and conducts poorly in clean air; a gas that burns (LPG, methane, hydrogen, alcohol, smoke) reacts with that oxygen and the resistance falls. The board puts the sensor over a load resistor to ground, so more gas is a higher voltage on AO. An LM393 compares AO with the trimmer and pulls DO low, lighting the second lamp, when the gas passes it.

Pins

Pin What it does
VCC Supply, 5 V. The heater is specified at 5.0 V.
GND Ground.
DO Low when the gas is past the trimmer. Open collector with a 10 k pull-up.
AO The sensor over the load resistor: higher means more gas.

Properties

gas picks which gas is in the room: lpg (the default), methane, hydrogen, smoke, alcohol or co. r0 is the sensor's resistance in 1000 ppm of hydrogen, 10 k by default. rl is the board's load resistor, 1 k on the FC-22. threshold is the trimmer as a fraction of the supply, 0.1 by default. warmup is how long the heater takes to settle after power, 20 seconds by default.

While it runs

Drag the slider to let gas into the room, 0 to 10 000 ppm. The slider is squared, so its middle is 2500 ppm and the low end has room to move. The can glows and the board says "warming up" while the heater settles.

What the model gets right

The curve. The datasheet plots Rs / R0 against concentration on log-log paper, one straight line per gas, with a flat line for clean air at about 9.8. Each gas here is two numbers read off that chart, the ratio at 200 ppm and the slope:

Gas Rs / R0 at 200 ppm Slope
hydrogen 2.1 -0.47
LPG 1.6 -0.47
methane 3.0 -0.38
alcohol 2.9 -0.37
smoke 3.4 -0.44
CO 5.2 -0.34

They are readings of a printed chart, good to the width of its lines; hydrogen comes out at a ratio of 1.0 at 1000 ppm, as the definition of R0 says it must. With the 1 k load resistor clean air reads only a few tens of millivolts, and even 10 000 ppm of LPG reads about 1.4 V: the useful range is the bottom third of the ADC.

The warm-up. A cold MQ-2 reads low resistance, as if the room were full of gas. Switch it on and AO jumps up, DO trips, and over the warm-up the reading sinks to clean air. A sketch that raises the alarm on its first reading goes off every time it is powered, on the bench and here.

Slow response. Once warm, the reading follows the gas with a time constant of 2 seconds when gas arrives and 8 seconds when it clears. A whiff is seen in a few seconds and takes half a minute to go away.

What it does not model

The datasheet asks for a 24 hour burn-in before any figure it gives is to be trusted; the 20 second warm-up is the part of that anyone watches, and it is a property. The heater draws about 150 mA, which is why a real one should not run from a board's 3.3 V pin; the supply here is not loaded by it. Humidity and temperature, which move a real reading by tens of percent, are not modeled, and there is one gas at a time. The comparator has no hysteresis, as on the real board.

On 3.3 V

An ESP32 or a Pico has a 3.3 V pin next to the 5 V one, and many circuits wire the sensor to it. The board runs: the load resistor and the comparator work on any supply from 2.5 V, so AO is a real divider output and an ADC on it reads a steady value that follows the gas. The element is cool, though. The heater gets (V / 5)^2 of its rated power, 44 percent on 3.3 V, so the reading moves only that fraction of the way, in the log of the resistance, from clean air toward the datasheet's value, and the warm-up takes that much longer (45 seconds instead of 20). On 3.3 V the same gas reads lower and flatter than on 5 V. The fraction is an approximation; the datasheets say nothing about a cool element. Below 2.5 V the board is off and both outputs float.

Common mistakes

Trusting the first minute. Wait for the warm-up, or ignore DO until it has passed.

Reading DO as "high means gas". It is the other way around: DO goes low and the lamp lights.

See it in action

Comparator boards has an MQ-2, a flame sensor, a TCRT5000 and an FC-51 each lighting a lamp from DO, with no board at all. Open it at /templates.

The Gas leak detector with an MQ-2 waits out the warm-up, then shows the level on an I2C LCD and a traffic light and beeps past a reading of 100. The MQ-2 gas sensor on a Pi reads AO through a divider and an MCP3008.