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.
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.
analogRead(4)
ADC Read
Components Used
| # | Component | Purpose | Type |
|---|---|---|---|
| 1 | ESP32 DevKit C V4 | Main microcontroller with built-in WiFi & ADC | MCU |
| 2 | MQ Gas Sensor | Detects LPG, methane, smoke, CO, and other gases | Sensor |
| 3 | Red LED | Visual danger alert indicator | Output |
| 4 | Jumper Wires | Component interconnection | Passive |
Hardware Connections
💡 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.
/* 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
Serial.begin(115200);
pinMode(2, OUTPUT);
int gasvalue = analogRead(4);
if (gasvalue <= 700)
digitalWrite(2, HIGH);
Serial.println("Danger!");
delay(2000);
Serial Monitor Output
Open the Serial Monitor at 115200 baud to see real-time sensor data. Here's what different readings look like:
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.
{
"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
What You Will Learn & How to Expand
- ESP32 analog input handling
- Sensor calibration basics
- Threshold-based automation
- Serial communication debugging
- GPIO digital output control
- IoT safety system design
- 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.
Comments
Post a Comment