MQ-2 Gas Sensor with ESP8266 | Gas & Smoke Detection Tutorial

adminESP82661 week ago11 Views

Introduction

The MQ-2 gas sensor is one of the most popular sensors used in embedded systems and IoT projects for detecting harmful gases in the air. It can detect gases like LPG, methane, butane, smoke, alcohol, and hydrogen. Because of its low cost and wide availability, it is commonly used in home safety systems, industrial monitoring, and IoT-based air quality monitoring projects.

When we connect the MQ-2 gas sensor with the ESP8266 Wi-Fi module, we can easily monitor the gas levels and even send the data to an IoT cloud platform for remote monitoring. This combination is perfect for beginners and professionals who want to build smart gas leakage detection systems with real-time alerts.

The MQ-2 sensor has two outputs:

  • Analog output (A0): Gives variable voltage depending on the gas concentration.
  • Digital output (D0): Gives either HIGH or LOW signal when gas concentration crosses the preset threshold.

In this tutorial, we will learn how to interface the MQ-2 sensor with ESP8266 and read its values to detect gas leakage.

Components Required

  • ESP8266 (NodeMCU or Wemos D1 Mini)
  • MQ-2 Gas Sensor Module
  • Jumper Wires
  • Breadboard
  • USB cable for programming

Circuit Diagram

  1. MQ-2 → ESP8266 connections
    • VCC → 3.3V (ESP8266 works on 3.3V, so connect accordingly)
    • GND → GND
    • A0 → A0 (ESP8266 has only one ADC pin, so we connect here)

Arduino Code for MQ-2 and ESP8266

// MQ-2 Gas Sensor with ESP8266
int mq2_analog = A0;    // Analog pin for MQ-2

void setup() {
  Serial.begin(115200);   // Start Serial Monitor
}

void loop() {
  // Read analog value (0 - 1023)
  int analogValue = analogRead(mq2_analog);

  // Print values on Serial Monitor
  Serial.print("Analog Value: ");
  Serial.print(analogValue);

  delay(1000); // Wait for 1 second
}

Step-by-Step Code Explanation

Pin Declaration

int mq2_analog = A0;    
  • MQ-2 has two outputs: analog and digital.
  • We connect the analog output to ESP8266 A0 pin.

Setup Function

Serial.begin(115200);
pinMode(mq2_digital, INPUT);
  • Start the serial monitor at 115200 baud rate to display results.
  • Set digital pin as input, because the sensor sends signals to ESP8266.

Loop Function

int analogValue = analogRead(mq2_analog);

analogRead(A0): Reads values between 0–1023 depending on gas concentration.

Display Results

Serial.print("Analog Value: ");
Serial.print(analogValue);

Prints analog values on the serial monitor for real-time observation.

Applications

  • LPG gas leakage detection
  • Smoke detection in homes
  • Industrial air quality monitoring
  • IoT-based safety alarms

What is the MQ-2 gas sensor used for?

The MQ-2 gas sensor is used for detecting gases like LPG, methane, smoke, alcohol, hydrogen, and propane. It is commonly used in gas leakage and smoke detection systems.

Can I connect MQ-2 directly to ESP8266?

Yes, the MQ-2 can be directly connected to ESP8266. The analog output goes to A0 pin and the digital output can be connected to any digital GPIO.

What power supply is required for MQ-2 with ESP8266?

Both ESP8266 and MQ-2 can work on 3.3V, but the MQ-2 sensor may perform better at 5V. If using NodeMCU, you can connect MQ-2 VCC to the Vin pin (5V) safely.

How do I calibrate MQ-2 sensor?

The MQ-2 sensor has a small potentiometer that allows you to set a threshold for the digital output. You can rotate it clockwise or counterclockwise to adjust sensitivity.

Can I send MQ-2 readings to IoT cloud?

Yes, using ESP8266 Wi-Fi, you can send MQ-2 sensor data to Blynk, ThingSpeak, Arduino IoT Cloud, or MQTT for remote monitoring and alerts.

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

Signing-in 3 seconds...

Signing-up 3 seconds...