Skip to main content

ESP32 Gas Leak Detection System with MQ Sensor and LED Alarm – DIY Project

ESP32 Gas Leak Detection System with MQ Sensor & LED Alert | Wokwi Tutorial
⚠ Safety Project Beginner Arduino C Wokwi Simulator ESP32 DevKit C V4 MQ Gas Sensor

ESP32 Gas Leak Detection
System with MQ Sensor

Monitor LPG, methane, smoke, and harmful gases in real-time. A red LED alert fires when gas concentration crosses the danger threshold. Fully simulatable in Wokwi — no hardware required to get started.

📅 February 2026 ⏱ ~15 min read 🔗 Wokwi Project #459444455463334913 ⚡ 115200 baud

Project Overview & Working Principle

This project continuously polls the MQ gas sensor's analog output through ESP32's GPIO 4 and compares it against a fixed threshold. When gas concentration is dangerously high, the sensor voltage drops — the ESP32 detects this, lights the LED, and prints a warning to the Serial Monitor every 2 seconds.

01 Gas concentration increases in the environment Input
02 MQ sensor output voltage changes — lower voltage = more gas Sensor
03 ESP32 reads analog value from GPIO 4 via analogRead(4) ADC Read
04 Value ≤ 700 → Gas Leak Detected 🚨 DANGER
05 Value > 700 → Environment is safe ✅ SAFE
06 Red LED turns ON (danger) or OFF (safe) via GPIO 2 Output
07 Serial Monitor logs readings every 2 seconds Logging
📊 Threshold Visualisation — analogRead(4) Range: 0 → 4095
0 — DANGER Threshold: 700 4095 — CLEAR
0–700: Gas Leak Alert 700–2000: Caution Zone 2000–4095: Safe Environment

Components Used

#ComponentPurposeType
1ESP32 DevKit C V4Main microcontroller with built-in WiFi & ADCMCU
2MQ Gas SensorDetects LPG, methane, smoke, CO, and other gasesSensor
3Red LEDVisual danger alert indicatorOutput
4Jumper WiresComponent interconnectionPassive

Hardware Connections

🟤 MQ Gas Sensor → ESP32
VCC 5V (VBUS)
GND GND
DOUT / A0 GPIO 4
🔴 LED Connection → ESP32
Anode (+) GPIO 2
Cathode (−) GND

💡 Tip: The ESP32 reads gas values through an analog-capable GPIO pin (GPIO 4). The LED is controlled digitally via GPIO 2. No external resistor is strictly required for the LED in simulation, but use a 220 Ω resistor on real hardware.

Arduino C Code

The entire sketch is under 20 lines. Copy it into your Wokwi project's sketch.ino or your Arduino IDE editor.

sketch.ino
Arduino C
/*
  ESP32 Gas Leak Detection System
  MQ Gas Sensor + Red LED Alert
  Threshold: gasvalue <= 700 → DANGER
*/

void setup() {
  Serial.begin(115200);        // Start serial at 115200 baud
  pinMode(2, OUTPUT);          // GPIO 2 → LED output
}

void loop() {
  int gasvalue = analogRead(4); // Read MQ sensor on GPIO 4

  Serial.print("Gas Sensor Value: ");
  Serial.print(gasvalue);

  if (gasvalue <= 700) {
    digitalWrite(2, HIGH);       // LED ON — danger
    Serial.println("  Danger ! Gas leak Detected!");
  } else {
    digitalWrite(2, LOW);        // LED OFF — safe
    Serial.println("  Environment safe");
  }

  delay(2000);                   // Poll every 2 seconds
}

Code Explanation

STEP 1 — INIT
Serial & GPIO Setup
Serial.begin(115200);
pinMode(2, OUTPUT);
Opens the serial port at 115200 baud for monitoring. Configures GPIO 2 as a digital output to drive the LED.
STEP 2 — READ
Sensor Reading
int gasvalue = analogRead(4);
Reads the 12-bit ADC value (0–4095) from the MQ sensor's analog output wired to GPIO 4. A lower value means higher gas concentration.
STEP 3 — COMPARE
Threshold Check
if (gasvalue <= 700)
Compares the raw ADC value against the danger threshold of 700. Adjust this constant to recalibrate for your specific MQ sensor and gas type.
STEP 4 — ALERT
LED & Serial Output
digitalWrite(2, HIGH);
Serial.println("Danger!");
When danger is detected: LED is turned ON via HIGH signal, and a warning is printed to the Serial Monitor. Otherwise, LED is OFF and "safe" is logged.
STEP 5 — LOOP
Repeat Every 2 Seconds
delay(2000);
A 2-second delay between readings. Lower this value for more frequent polling, or replace with a non-blocking timer for advanced builds.

Serial Monitor Output

Open the Serial Monitor at 115200 baud to see real-time sensor data. Here's what different readings look like:

Serial Monitor — 115200 baud
Gas Sensor Value: 0 Danger ! Gas leak Detected!
Gas Sensor Value: 342 Danger ! Gas leak Detected!
Gas Sensor Value: 700 Danger ! Gas leak Detected!
Gas Sensor Value: 701 Environment safe
Gas Sensor Value: 2450 Environment safe
Gas Sensor Value: 4095 Environment safe

A reading of 0 in Wokwi simulation means the gas sensor is reading maximum concentration — the LED alarm will activate immediately on boot. Adjust the simulated distance slider in Wokwi to change the sensor output.

diagram.json — Wokwi Circuit File

Paste this into your Wokwi project's diagram.json to instantly wire up the ESP32, MQ gas sensor, and red LED.

📄 diagram.json
{
  "version" 1,
  "author" "Syed Vajith",
  "editor" "wokwi",
  "parts" [
    {
      "type" "board-esp32-devkit-c-v4",
      "id" "esp",
      "top" 48,
      "left" 62.44,
      "attrs" {}
    },
    {
      "type" "wokwi-gas-sensor",
      "id" "gas1",
      "top" 89.1,
      "left" -108.2,
      "attrs" {}
    },
    {
      "type" "wokwi-led",
      "id" "led1",
      "top" 25.2,
      "left" 234.2,
      "attrs" { "color" "red" }
    }
  ],
  "connections" [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "gas1:GND", "esp:GND.1", "black", [ "h28.8", "v76" ] ],
    [ "gas1:VCC", "esp:5V", "red", [ "h19.2", "v114.3" ] ],
    [ "gas1:DOUT", "esp:4", "green", [ "h19.2", "v-0.3", "h9.6", "v-86.4", "h124.8", "v163.2" ] ],
    [ "led1:C", "esp:GND.3", "green", [ "v0" ] ],
    [ "led1:A", "esp:2", "green", [ "v144" ] ]
  ],
  "dependencies" {}
}

Applications

🏠
Smart Home Alarm
Gas leak detection for kitchens and utility rooms
🛢️
LPG Safety System
Protect households from cylinder leaks
🏭
Industrial Monitor
Continuous gas monitoring in factories
🔥
Fire Detection IoT
Early fire warning via smoke detection
🍳
Kitchen Safety
Alert when cooking gas builds up
🌬️
Air Quality Monitor
Log and visualise air pollution data
🏗️
Smart Factory
Worker safety in hazardous environments

What You Will Learn & How to Expand

📚 Skills You'll Gain
  • ESP32 analog input handling
  • 📐Sensor calibration basics
  • 🔁Threshold-based automation
  • 📡Serial communication debugging
  • 💡GPIO digital output control
  • 🔐IoT safety system design
🚀 Upgrade Ideas
  • ☁️WiFi cloud alerts (MQTT / Blynk)
  • 🔔Buzzer alarm for audible alert
  • 📺OLED display for live readings
  • 📱SMS notification via Twilio
  • 📊IoT dashboard monitoring
  • 📲Mobile app integration

Perfect for STEM learners, IoT enthusiasts, engineering students, and smart home developers looking to build real-world safety applications.

© 2026 MakeMindz · ESP32 & IoT Project Tutorials

Built for makers, learners, and IoT safety enthusiasts everywhere.

Comments

try for free