Index
Introduction
The MQ-2 gas sensor is an essential tool for detecting gases like LPG, methane, and smoke, making it ideal for safety and air quality projects.
It works by measuring changes in electrical resistance based on gas concentration and outputs an analog signal corresponding to the detected levels.
This guide will help you connect the MQ-2 sensor to an ESP32 and read gas or smoke concentration values accurately using the Arduino IDE.
Required Components
- ESP32 Board
- MQ2 Gas Sensor
- Jumper Wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- MQ2 SENSOR VCC → Vin (5V) (ESP32)
- MQ2 SENSOR GND → GND (ESP32)
- MQ2 SENSOR AO (Analog Output) → Pin 34 (ESP32)

Code / Programming
/*
Filename: mq2_gas_detector.ino
Description: Detects presence of gas using MQ-2 sensor and ESP32's ADC pin
Author: www.oceanlabz.in
Modification: 1/4/2025
*/
#define MQ2_PIN 34 // MQ-2 analog output connected to GPIO34
#define GAS_THRESHOLD 600 // Gas detection threshold (adjust as needed)
void setup() {
Serial.begin(115200); // Initialize serial communication
}
void loop() {
int gasValue = analogRead(MQ2_PIN); // Read analog value from MQ-2 sensor
// Print sensor reading
Serial.print("MQ-2 Sensor Value: ");
Serial.println(gasValue);
// Check gas level
if (gasValue > GAS_THRESHOLD) {
Serial.println("Gas Detected!");
} else {
Serial.println("No Gas Detected.");
}
delay(1000); // Wait before the next reading
}
Explanation
- The MQ-2 gas sensor detects LPG, methane, smoke, and other gases by changing its internal resistance.
- It outputs an analog signal based on gas concentration, which the ESP32 reads via an ADC pin.
- Ideal for air quality monitoring and safety alarms in IoT or embedded projects.
Troubleshooting
- Ensure the sensor is powered with 5V, and the analog pin is connected to an ESP32 ADC-compatible pin (e.g., GPIO34, 35).
- If readings are unstable, allow 2–3 minutes of warm-up time for accurate results.
- Use Serial Monitor to verify readings, and check for loose or incorrect wiring.
Project 1: Gas Safety Monitoring System MQ2, Buzzer, LEDs
Introduction
The Gas Detection and Alert System using ESP32 is a safety device designed to detect harmful gases using an MQ-2 sensor. With its real-time processing capabilities, the ESP32 triggers alerts through a buzzer and LEDs, ensuring immediate warnings in case of gas leaks. This system is ideal for enhancing safety in homes, industries, and laboratories, leveraging the ESP32’s Wi-Fi support for potential IoT integration and remote monitoring.
Required Components
- ESP32 Board
- MQ2 Gas Sensor
- Buzzer Module
- LEDs (tow) with Resistor 220 Ohm (tow)
- Jumper Wires
- Breadboard
Circuit Diagram / Wiring
- MQ2 Gas Sensor
- MQ2 SENSOR VCC → VIN (ESP32)
- MQ2 SENSOR GND → GND (ESP32)
- MQ2 SENSOR AO (Analog Output) → GPIO 34 (ESP32)
- Buzzer Module
- Buzzer VCC → VIN (ESP32)
- Buzzer GND → GND (ESP32)
- Buzzer SINGNAL → GPIO 27 (ESP32)
- LEDs
- Connect the red LED anode to GPIO 26 (via a 220-ohm resistor) and cathode to GND.
- Connect the green LED anode to GPIO 25 (via a 220-ohm resistor) and cathode to GND.

Code / Programming
/*
Filename: esp32_mq2_gas_smoke_alert.ino
Description: Gas and smoke detection using MQ-2 sensor, buzzer, and LEDs on ESP32
Author: www.oceanlabz.in
Modification: 1/4/2025
*/
// Define ESP32 GPIO pins
const int mq2Pin = 34; // Analog pin for MQ-2 sensor
const int buzzerPin = 27; // Buzzer connected to GPIO27
const int redLED = 26; // Red LED (Alert)
const int greenLED = 25; // Green LED (Normal)
// Detection thresholds
const int detectionThresholdGas = 600; // Gas threshold
const int detectionThresholdSmoke = 30; // Smoke threshold
// PWM channel for buzzer
const int buzzerChannel = 0;
void setup() {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Setup PWM for buzzer
ledcSetup(buzzerChannel, 1500, 8); // Channel 0, 1500Hz, 8-bit resolution
ledcAttachPin(buzzerPin, buzzerChannel); // Attach buzzer pin to channel
Serial.begin(115200); // Start serial communication
}
void loop() {
int sensorValue = analogRead(mq2Pin);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
if (sensorValue > detectionThresholdGas) {
Serial.println("Gas Detected!");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
ledcWriteTone(buzzerChannel, 1500); // Turn on buzzer
}
else if (sensorValue > detectionThresholdSmoke) {
Serial.println("Smoke Detected!");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
ledcWriteTone(buzzerChannel, 1500); // Turn on buzzer
}
else {
Serial.println("No
Explanation
Sensor Setup: The MQ-2 gas and smoke sensor is connected to GPIO pin 34. The buzzer, red LED, and green LED are connected to GPIO pins 25, 26, and 27 respectively.
Thresholds for Detection: The code defines two detection thresholds:
detectionThresholdGas
: 600 for detecting gas.detectionThresholdSmoke
: 30 for detecting smoke.
Detection Logic:
- If the sensor value exceeds the gas threshold, the red LED is turned on, the green LED is turned off, and a buzzer tone is emitted.
- If the sensor value exceeds the smoke threshold, the same actions are performed for smoke detection.
- If neither is detected, the red LED is off, the green LED is on, and the buzzer is off.
Serial Output: The sensor value and detection status (gas or smoke) are displayed on the serial monitor for real-time feedback.
Troubleshooting
- Set up the sensor and open the Serial Monitor.
- Expose it to gas (e.g., butane) and smoke and note the sensor values.
- Set thresholds based on your observations:
- Gas threshold: e.g., 600.
- Smoke threshold: e.g., 30.
Adjust the values in your code:
const int detectionThresholdGas = 600;
const int detectionThresholdSmoke = 30;
Test and adjust as needed for more accuracy.