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-S3 and read gas or smoke concentration values accurately using the Arduino IDE.
Required Components
- ESP32-S3 Board
- MQ2 Gas Sensor
- Jumper Wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- MQ2 SENSOR VCC → Vin (5V) (ESP32-S3)
- MQ2 SENSOR GND → GND (ESP32-S3)
- MQ2 SENSOR AO (Analog Output) → Pin 34 (ESP32-S3)
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-S3 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-S3 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.