STANNUM
Open the simulator

Tutorials / Sensors

MQ-2 gas and smoke sensor

An analog reading that rises with gas, plus a digital alarm output with a threshold you set on the module.

Parts in the example:
  • MQ-2 (gas)
  • Active buzzer
  • LED
The MQ-2 (gas) block as it appears on the canvas.

What it is

The MQ-2 has a heated element whose resistance drops when flammable gas or smoke reaches it. The module gives two outputs: A0, the raw analog value, and D0, the output of a comparator with a threshold set by the blue trimmer on the board — LOW when gas is detected on most modules. The reading is relative: an MQ sensor is not calibrated in ppm.

In the simulator

The block has a Gas slider (0 to 100 %) and shows the ADC count and the level of D0. In Settings, the Comparator threshold is the trimmer (40 % by default) and D0 output chooses the polarity. Notice the reading never reaches zero: clean air already gives a floor, like the real part.

Pins

PinWhat it is
A0A0, the analog reading — to a GPIO with an ADC
D0D0, the comparator's alarm output — to a GPIO (optional)
VCCpower — to a 3V3 symbol
GNDground — to a GND symbol

Wiring

The circuit below is the example Gas alarm — MQ-2 from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
MQ-2 (gas) pinGoes to
A0D34 on the board (GPIO 34)
D0left unconnected
VCCa 3V3 symbol
GNDa GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the MQ-2 (gas).
SettingAcceptsDefaultNotes
Comparator threshold (D0)0 to 100 %40 %On the real module this is the screw on the blue trimmer.
D0 outputLOW on detection (usual) · HIGH on detectionLOW on detection (usual)

Code

sketch.cpp
#include <Arduino.h>

// The MQ-2 does not come calibrated in ppm: the reading is RELATIVE. In real
// life you record the clean-air value and alarm on the change — here a fixed
// threshold is enough for the exercise.
const int GAS = 34, BUZZER = 26, LED = 4;
const int LIMIAR = 1800;

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER, OUTPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  int leitura = analogRead(GAS);
  bool alarme = leitura > LIMIAR;
  digitalWrite(BUZZER, alarme);
  digitalWrite(LED, alarme);
  Serial.printf("gas=%4d %s\n", leitura, alarme ? "*** ALARM ***" : "ok");
  delay(300);
}

Only A0 is used: analogRead, compared with 1800, drives the buzzer and the LED. The comment in the sketch says the honest thing about MQ sensors: record the clean-air value and alarm on the change.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Drag the slider up: past the threshold the buzzer and the LED go on and the serial monitor prints “*** ALARM ***”.

How the simulation models it

Try this

  1. Wire D0 to a GPIO and alarm on it instead of on A0 — then move the threshold in Settings and see the code follow without a rebuild.
  2. Print the reading relative to the value seen at start-up (“+12 %”), the way a real installation would.

See also