ESP32-based gas and smoke monitoring system using an MQ-2 sensor, SSD1306 OLED display, LEDs, and buzzer with multi-state alert logic.
The ESP32 MQ-2 Gas Leak Monitor is an embedded environmental safety monitoring system designed to detect gas and smoke concentration levels in real time.
The system continuously reads analog values from an MQ-2 gas sensor and evaluates environmental conditions using a three-state alert model:
- SAFE
- WARNING
- DANGER
Measured gas levels are displayed on an SSD1306 OLED display, while LEDs and a buzzer provide immediate visual and audible feedback.
This project demonstrates practical applications of:
- Analog sensor interfacing
- ADC measurements
- OLED display communication
- Real-time monitoring
- State-machine design
- Embedded firmware development
- Real-time gas monitoring
- MQ-2 gas sensor integration
- SSD1306 OLED display interface
- Three-level safety indication
- Green LED for SAFE state
- Orange LED for WARNING state
- Blue LED for DANGER state
- High-tone buzzer alarm
- Serial Monitor debugging output
- Clean and documented firmware
- ESP32 compatible
| Component | Quantity |
|---|---|
| ESP32 Development Board | 1 |
| MQ-2 Gas Sensor Module | 1 |
| SSD1306 OLED Display (128×64 I2C) | 1 |
| Green LED | 1 |
| Orange LED | 1 |
| Blue LED | 1 |
| Active / Passive Buzzer | 1 |
| 220Ω Resistors | 3 |
| Breadboard / Jumper Wires | Several |
| MQ-2 Pin | ESP32 Pin |
|---|---|
| VCC | VIN (5V) |
| GND | GND |
| AO | GPIO34 |
| OLED Pin | ESP32 Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GPIO21 |
| SCL | GPIO22 |
| LED | ESP32 GPIO |
|---|---|
| Green LED | GPIO15 |
| Orange LED | GPIO2 |
| Blue LED | GPIO4 |
| Buzzer | ESP32 GPIO |
|---|---|
| Positive (+) | GPIO14 |
| Negative (-) | GND |
The monitoring system uses a simple finite-state approach.
Condition:
Gas Value < 300
Outputs:
Green LED : ON Orange LED : OFF Blue LED : OFF Buzzer : OFF OLED : SAFE
Condition:
300 ≤ Gas Value < 500
Outputs:
Green LED : OFF Orange LED : ON Blue LED : OFF Buzzer : OFF OLED : WARNING
Condition:
Gas Value ≥ 500
Outputs:
Green LED : OFF Orange LED : OFF Blue LED : ON Buzzer : ON OLED : DANGER
The ESP32 initializes:
- OLED display
- MQ-2 gas sensor
- LEDs
- Buzzer
The MQ-2 contains an internal heating element.
During startup the OLED displays:
MQ-2 GAS MONITOR
Sensor Warming Please Wait...
The sensor is allowed to stabilize before monitoring begins.
The ESP32 continuously reads the analog output of the MQ-2 sensor.
gasValue = analogRead(MQ2_PIN);
The ADC converts sensor voltage into a digital value.
The firmware compares the measured value against predefined thresholds.
SAFE WARNING DANGER
The OLED updates the gas concentration and system status.
LEDs indicate the current safety level.
The buzzer activates when dangerous gas concentrations are detected.
The monitoring process repeats indefinitely.
MQ-2 GAS MONITOR
Gas: 223
STATUS: SAFE
MQ-2 GAS MONITOR
Gas: 387
STATUS: WARNING
MQ-2 GAS MONITOR
Gas: 645
STATUS: DANGER ``
Watch the project in action:
docs/demo.mp4
or
Recommended:
Arduino IDE 2.x
Install the ESP32 board package from Arduino Boards Manager.
Install the following libraries using Arduino Library Manager:
Adafruit GFX Library Adafruit SSD1306 Library Wire Library
Open:
src/ESP32_MQ2_Gas_Leak_Monitor.ino
in Arduino IDE.
Tools → Board → ESP32 Dev Module
Connect ESP32 via USB and upload the sketch.
115200 Baud
Observe live gas readings.
MQ-2 sensors vary between modules and environments.
Before deployment:
- Power the sensor.
- Allow warm-up time.
- Observe readings in clean air.
- Record the average value.
- Adjust thresholds if necessary.
Example:
Clean Air: 190 - 250
Warning: 300 - 500
Danger:
500
The thresholds used in this project were calibrated using actual sensor measurements.
---
## Project Structure
ESP32-MQ2-Gas-Leak-Monitor
│
├── docs
│ ├── wiring.png
│ ├── circuit_diagram.png
│ ├── oled_active.png
│ └── demo.mp4
│
├── src
│ └── ESP32_MQ2_Gas_Leak_Monitor.ino
│
├── LICENSE
│
└── README.md
---
## Educational Objectives
This project demonstrates:
- Analog-to-Digital Conversion (ADC)
- Sensor calibration
- Embedded state machines
- OLED display programming
- Real-time monitoring systems
- Embedded alarm systems
- GPIO control
- Safety monitoring applications
---
## Future Improvements
Potential upgrades include:
- Wi-Fi notifications
- MQTT integration
- Mobile alerts
- Cloud dashboard
- Data logging
- SD card storage
- Historical gas graphs
- FreeRTOS implementation
- Multi-sensor integration
- Automatic calibration
---
## Repository Topics
esp32
arduino
iot
embedded-systems
mq2
gas-sensor
smoke-detector
oled-display
ssd1306
environment-monitoring
embedded-firmware
safety-system
real-time-monitoring
---
## Author
**Milad Mohseni**
Embedded Systems & IoT Engineer
Areas of Interest:
- Embedded Systems
- ESP32 Development
- IoT Applications
- Sensor Interfacing
- Firmware Development
- Real-Time Monitoring Systems
---
## License
This project is licensed under the MIT License.
See the LICENSE file for more information.


