Source: https://mokxi.com/parts/mq2
Updated: 2026-11-10

Part

# Gas sensor, MQ-2

A heated tin-oxide element on a comparator board. It reads like a gas leak until it has warmed up.

See every part

- 4 pins
- 5 settings
- In 1 project
- In 1 lesson

Drawn live by the editor's own code, at the size you see it.

At a glance

## 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.

Pins

## 4 pins

Named as the editor names them, so a wire lands where you expect it.

Pin

Role

What it does

VCC

Input or output

Supply, 5 V. The heater is specified at 5.0 V.

GND

Input or output

Ground.

DO

Output

Low when the gas is past the trimmer. Open collector with a 10 k pull-up.

AO

Output

The sensor over the load resistor: higher means more gas.

How to use 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.

Part pin

Board pin

VCC

5 V

GND

GND

AO

A0

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.

Click to open it in the editor

This is the simulator itself, running here. Click anything to open it in the editor.

Open this project in the editor Read the full tutorial

How it is modeled

## 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.

Projects

## See the Gas sensor, MQ-2 in a project

One built-in project uses it. It opens in the editor, already wired.

Lessons

## Where it turns up in a lesson

One lesson places it in its own circuit.

### Learn articles

1 lesson

- Arduino MQ-2 Gas Sensor: Wiring, Warm-Up and Code

Libraries

## 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.

- MQUnifiedsensor

More parts

## The rest of the bench

Every one of these is drawn and simulated the same way.

Full-size breadboard

A real 0.1 inch grid with the rails and the center channel, 63 columns wide.

Jumper wires

Drag from any pin or hole to any other. Corners snap, and you can drag them.

Power

A supply rail at the voltage you choose.

Ground

The other end of every circuit.

LED

Lights when current flows. Five colors, and the brightness is what your eye would see.

Filament lamp

A tungsten bulb in a holder: an inrush when it is switched on cold, tens of milliseconds to warm up, and dim on half its voltage.

Resistor

Any value you like, with the color bands drawn to match.

ESP32-C3-DevKitM-1

A RISC-V board that runs your firmware at 160 MHz on the real memory map.

Pushbutton

A 12 mm tactile switch. Hold it while the simulation runs.

Raspberry Pi Pico

The RP2040 board on our own Cortex-M0+ core. Pick a program and press Run.

Raspberry Pi Pico W

Raspberry Pi Pico W (RP2040). The same board and the same pinout as a Pico, with the CYW43439 on it. The WiFi is simulated (no radio; real internet through the Cloud panel’s switch), and the on-board LED, which hangs off that chip rather than off GP25, is driven through the same path. Everything else is the Pico.

Raspberry Pi 4 Model B

Raspberry Pi 4 Model B: the 40-pin header, driven by main.py with RPi.GPIO or gpiozero.

See every part

## 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.

Start building free Open the editor
