Gas sensor, MQ-2
A heated tin-oxide element on a comparator board. It reads like a gas leak until it has warmed up.
- 4 pins
- 5 settings
- In 1 project
- In 1 lesson
The Gas sensor, MQ-2 in brief
What it is, and every setting the properties panel gives it.
The MQ-2 is a heated tin dioxide gas sensor, sold on an FC-22 style comparator board with four pins: VCC, GND, DO and AO. Its heater wants 5 volts and 150 mA, so it runs from an Uno’s 5 V pin, or from VIN or USB 5 V beside an ESP32, never from 3.3 V; a cold sensor’s AO can sit above 3.3 volts, so an ESP32 should read it through a divider. More gas lowers the sensor’s resistance and raises AO, and the LM393 pulls DO low and lights the lamp when AO passes the trimmer. Mokxi follows the datasheet’s log-log sensitivity curves for LPG, methane, hydrogen, smoke, alcohol and CO, with r0, rl, threshold and warmup properties and a slider to 10 000 ppm. With the 1 k load, 10 000 ppm of LPG reads only about 1.4 volts. A cold sensor reads as full of gas and settles over a 20 second warm-up, then follows the gas with time constants of 2 seconds arriving and 8 clearing, so an alarm on the first reading fires at every power-up. Humidity and gas mixtures are not modeled.
- Family
- Sensors
- Pins
- 4
- gas
- lpg (or methane, hydrogen, smoke, alcohol, co)
- r0
- 10000
- rl
- 1000
- threshold
- 0.1
- warmup
- 20
A circuit built around it, running and explained: the MQ-2 gas sensor simulator.
4 pins
Named as the editor names them, so a wire lands where you expect it.
How to use the MQ-2 gas sensor with an Arduino
Wire VCC to 5 V, GND to GND and AO to A0; DO can go to any digital pin if you want the trimmer to decide. A higher analogRead() means more gas. On the board’s 1 k load clean air reads only about 11 counts once warm, so pick an alarm level from your own clean-air reading rather than from a tutorial’s number.
Ignore the first 20 seconds. A cold element reads as if the room were full of gas, so a sketch that alarms on its first reading goes off at every power-up. Add a little hysteresis too: the gas leak detector sounds at 100 and only stops below 80, so a reading on the line does not chatter the buzzer.
The heater needs 5 V on every board. On a 3.3 V board such as an ESP32, run it from the 5 V or VIN pin and bring AO down through a divider before the ADC.
What is true about the Gas sensor, MQ-2, here
In its help article’s own words: what the simulation gets right, and where it stops.
Modeled
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.
Not modeled
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.
From MQ-2 gas sensor, in full.
See the Gas sensor, MQ-2 in a project
One built-in project uses it. It opens in the editor, already wired.
Where it turns up in a lesson
One lesson places it in its own circuit.
Arduino libraries for the Gas sensor, MQ-2
Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.
The rest of the bench
Every one of these is drawn and simulated the same way.
Wire up the Gas sensor, MQ-2
Open the editor and push it into the breadboard. It is free, and it runs on your own machine.