MQ-2 Gas Sensor
An analog and digital gas sensor module used for detecting smoke and combustible gases in the air.
Component Preview
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).
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
| Pin | Type | Description |
|---|---|---|
| VCC | power | Power Supply. Connect to Arduino 5V. |
| GND | power | Ground. Connect to Arduino GND. |
| D0 | digital | Digital Output. Goes LOW when gas concentration exceeds the set threshold. |
| A0 | analog | Analog Output. Outputs a voltage (0-5V) proportional to the gas concentration. |
Configurable Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
| threshold | number | 300 | The internal comparator threshold (0-1023) at which D0 goes LOW. |
Wiring Diagram
- Connect VCC to Arduino 5V.
- Connect GND to Arduino GND.
- Connect A0 to Arduino A0 (to read concentration).
- Connect D0 to Arduino D2 (to read the trigger state).

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