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:
In this tutorial, we will learn how to interface the MQ-2 sensor with ESP8266 and read its values to detect gas leakage.
// 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
}
Pin Declaration
int mq2_analog = A0;
Setup Function
Serial.begin(115200);
pinMode(mq2_digital, INPUT);
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.
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.
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.
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.
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.
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.