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

ESP32 Gas Leak Detection System | Wokwi
⚠ 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.

📅 February 2026 ⏱ ~15 min read 🔗 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.

01Gas concentration increases in the environmentInput
02MQ sensor output voltage changes — lower voltage = more gasSensor
03ESP32 reads analog value from GPIO 4 via analogRead(4)ADC Read
04Value ≤ 700 → Gas Leak Detected🚨 DANGER
05Value > 700 → Environment is safe✅ SAFE
06Red LED turns ON (danger) or OFF (safe) via GPIO 2Output
07Serial Monitor logs readings every 2 secondsLogging
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

MCU: ESP32 DevKit C V4 Sensor: MQ Gas Sensor Output: Red LED Extras: Jumper Wires Threshold: ≤ 700 = DANGER Baud rate: 115200
#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
VCC5V (VBUS)
GNDGND
DOUT / A0GPIO 4
Red LED → 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.

Try it free in Wokwi No hardware needed — simulate instantly in your browser
▶ Open Simulation

Arduino C Code

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

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 turns 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 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:

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.

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

Product Cards
Buddy Bot eBook
⭐ New 2026 Release
Build Your
Own Robot!
3D design, wiring &
Arduino coding.
Young inventors love it!
🖨️
3D Print
All parts
Wire it
Circuit guide
💻
Code it
Arduino IDE
🤖
Watch it
Walk & react
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Website Offer
₹499 300
🌍 International: $5 USD
One-time · Instant digital delivery
🔒 Secured by Razorpay · Your data is safe
📄 Download Free Sample Copy
🔒 Secured by Razorpay · Your data is safe
🍓
Raspberry Pi Pico Mastery
21 Projects
⚡ Launch Price — 80% OFF
Learn Pico
Build 21 Projects!
MicroPython · Wokwi
IoT · Certificate
Perfect for beginners!
🖥️
Wokwi
No hardware
🐍
MicroPy
From zero
🔨
21 Projects
IoT + sensors
📄
Certificate
Verified cert
📋 Your Details
Enter your name
Valid 10-digit no.
Enter a valid email
Special Launch Offer
₹999 200 80% OFF
🌍 International: $5 USD
One-time · Lifetime access · No subscription
🔒 Secured by Razorpay · UPI · Cards · NetBanking
🎉

You're in!

Payment successful! Your Buddy Bot eBook is ready. Time to build!

📖 Access Your eBook Now
🎉

Enrolled!

Payment successful! Lifetime access to all 21 Pico Projects is yours!

🍓 Go to My Course