Interfacing MQ-2 Gas Sensor with ESP32 DOIT DevKit v1

MuhammadMuhammadESP322 months ago22 Views

Introduction

The MQ-2 Gas Sensor is one of the most popular sensors for detecting gases like LPG, propane, methane, hydrogen, alcohol, and smoke. It works on the principle of resistance change—the sensor’s internal resistance decreases when exposed to gas, which changes the output voltage.

The MQ-2 module provides both:

  • Analog output (AO) → voltage proportional to gas concentration.
  • Digital output (DO) → HIGH/LOW signal depending on threshold set by the onboard potentiometer.

In this tutorial, we will connect the MQ-2 sensor to ESP32 (GPIO 33 for analog reading) and monitor gas levels via the Serial Monitor.

Components Required

  • ESP32 DOIT DevKit v1
  • MQ-2 Gas Sensor Module
  • Breadboard & jumper wires
  • USB cable

Circuit Diagram

  • MQ-2 VCC → 3.3V (works best with 5V, but ESP32 tolerant modules usually support 3.3V)
  • MQ-2 GND → GND
  • MQ-2 AO → GPIO 33 (Analog input)
  • MQ-2 DO → (optional, any digital pin if threshold output is needed)

Arduino Code

// MQ-2 Gas Sensor with ESP32 DOIT DevKit v1
#define MQ2_PIN 33   // MQ-2 Analog output connected to GPIO 33

void setup() {
  Serial.begin(115200);
  Serial.println("MQ-2 Gas Sensor Test with ESP32");
}

void loop() {
  int gasValue = analogRead(MQ2_PIN);  // Read analog value
  Serial.print("Gas Sensor Value: ");
  Serial.println(gasValue);

  // Simple threshold detection
  if (gasValue > 2000) { // Adjust threshold based on calibration
    Serial.println("Warning! Gas Detected ");
  }

  delay(1000); // 1 second delay
}

Step-by-Step Code Explanation

Define Pin

#define MQ2_PIN 33

MQ-2 analog pin connected to GPIO 33.

Setup

Serial.begin(115200);

Initialize Serial Monitor for output.

Read Sensor Value

int gasValue = analogRead(MQ2_PIN);

Reads raw ADC value (0–4095 for ESP32).

Print Data

Serial.print("Gas Sensor Value: ");
Serial.println(gasValue);

Displays gas level on Serial Monitor.

Threshold Detection

if (gasValue > 2000) {
  Serial.println("Warning! Gas Detected");
}

If value exceeds threshold, a warning is printed.

Output

MQ-2 Gas Sensor Test with ESP32
Gas Sensor Value: 350
Gas Sensor Value: 780
Gas Sensor Value: 2200
Warning! Gas Detected

Real-Life Applications

  • LPG / Gas Leak Detection – Home and industrial safety systems.
  • Smoke Detection – Fire alarms.
  • Breath Analyzers – Detect alcohol vapors.
  • IoT Projects – Send air quality data to cloud dashboards.
  • Smart Kitchens – Gas leakage detection systems.

Troubleshooting

ProblemCauseSolution
Constant HIGH valuesSensor not warmed upMQ-2 needs 24–48 hours initial burn-in
Unstable readingsPower issuesUse stable 5V supply for best results
Always LOW valuesWrong wiringDouble-check VCC, GND, AO connections
Threshold too sensitivePoor calibrationAdjust values experimentally
Library errorNot neededMQ-2 works with analogRead(), no library required

Why does the MQ-2 need calibration?

The sensor’s resistance changes based on environment; calibration ensures accurate readings.

Can MQ-2 detect CO2?

No ❌, it mainly detects LPG, propane, methane, hydrogen, smoke, and alcohol vapors.

How long does MQ-2 take to warm up?

At least 24 hours for initial burn-in, but 1–2 minutes for normal operation after that.

Can I use MQ-2 digital pin instead of analog?

Yes ✅, but it only gives HIGH/LOW output based on threshold, not actual gas concentration.

Is MQ-2 suitable for outdoor air quality monitoring?

Not really ❌, it’s better for indoor gas leak detection. For air quality, use sensors like MQ-135.

Leave a reply

Loading Next Post...
Follow
Search Trending
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...