Skip to content
Home > Components > Sensors > MQ-2 Gas Sensor

MQ-2 Gas Sensor

An analog and digital gas sensor module used for detecting smoke and combustible gases in the air.

Component Preview

MQ-2 Gas SensorMQ-2 Module

The MQ-2 is a versatile gas sensor capable of detecting LPG, smoke, alcohol, propane, hydrogen, methane and carbon monoxide concentrations anywhere from 200 to 10000 ppm. It provides both an analog output (proportional to gas concentration) and a digital output (triggered when a threshold is exceeded).

SensorsGasAnalog

Overview

Inside the MQ-2 is a small heater that must warm up before the sensor can take accurate readings (in real life, this takes a few minutes). The electrical resistance of the sensor drops in the presence of target gases. The breakout board includes an LM393 comparator to provide a clean boolean digital output.

Pin Reference

PinTypeDescription
VCCpowerPower Supply. Connect to Arduino 5V.
GNDpowerGround. Connect to Arduino GND.
D0digitalDigital Output. Goes LOW when gas concentration exceeds the set threshold.
A0analogAnalog Output. Outputs a voltage (0-5V) proportional to the gas concentration.

Configurable Attributes

AttributeTypeDefaultDescription
thresholdnumber300The internal comparator threshold (0-1023) at which D0 goes LOW.

Wiring Diagram

  1. Connect VCC to Arduino 5V.
  2. Connect GND to Arduino GND.
  3. Connect A0 to Arduino A0 (to read concentration).
  4. Connect D0 to Arduino D2 (to read the trigger state).

Wiring Diagram

Example Arduino Code

This sketch reads both the analog and digital outputs simultaneously. The analog output provides a raw concentration value, while the digital output acts as a simple boolean trigger.

cpp
// Define Sensor Pins
const int analogPin = A0;  // Analog pin for raw reading
const int digitalPin = 2;  // Digital pin for threshold reading

void setup() {
  pinMode(digitalPin, INPUT); // Set digital pin as input
  Serial.begin(9600);         // Start serial monitor
  Serial.println("MQ-2 Heating up... (Wait 20s in real life)");
}

void loop() {
  // Read the raw analog gas concentration value (0 to 1023)
  int gasLevel = analogRead(analogPin);
  
  // Read the boolean digital threshold state
  // LOW means the gas concentration is above the danger threshold!
  int dangerLevel = digitalRead(digitalPin);
  
  Serial.print("Raw Gas PPM Value: ");
  Serial.print(gasLevel);
  
  if (dangerLevel == LOW) {
    Serial.println(" --- ⚠️ DANGER: HIGH GAS DETECTED!");
  } else {
    Serial.println(" --- Environment Safe.");
  }
  
  delay(1000); // Read every second
}

Simulation Notes

  • The analog value generated in the simulator will default to a clean "air" reading. You can adjust the simulated gas concentration using the sensor's context menu.

Released under the MIT License.