Community project

Gas Leak Alert

Photo of Gas Leak Alert
Generated with AI

ΠΑΥΛΟΣ ΠΑΠΑΝΙΚΟΛΑΟΥ

Published November 28, 2026

This project builds a gas leak detection system using an ESP32 microcontroller paired with an MQ-5 gas sensor module and a DHT11 temperature sensor. When combustible gas levels exceed a threshold, the system triggers an active buzzer alarm to alert nearby occupants. The guide includes a wiring diagram showing how to connect the gas sensor analog output through an NPN transistor switch to drive the buzzer, a complete parts list, and step-by-step assembly instructions.

The firmware implements a 15-minute warm-up period for sensor stabilization, continuous gas monitoring with hysteresis to prevent false triggers, and real-time serial output of sensor readings in millivolts. Builders will learn how to interface analog sensors with the ESP32's ADC, drive an active buzzer safely using a transistor and flyback diode, and implement reliable alarm logic. This is a demonstration project and not a certified safety device.

Wiring diagram

Wiring diagram for Gas Leak Alert
Loading diagram…

Gather all the parts

QtyComponent
1

DHT11

Digital temperature and humidity sensor (lower accuracy than DHT22)

1

MQ-5 gas sensor module

5 V MQ-5 module with analog output; heater requires burn-in and warmup.

1

5 V two-pin active buzzer

Self-oscillating 5 V buzzer, maximum 30 mA, switched by an NPN transistor.

1

NPN Transistor (2N2222 / BC547)

General-purpose NPN bipolar junction transistor (BJT) in TO-92 package. Used as a low-side switch to drive loads (e.g. buzzers) whose current exceeds ESP32 GPIO source capability (~12 mA). A ~1 kΩ base resistor is placed between the ESP32 GPIO and the transistor base. The collector connects to the load negative terminal (buzzer –), and the emitter connects to GND. When the GPIO goes HIGH (3.3 V), the transistor saturates and switches the load on. Compatible with both 2N2222/2N2222A and BC547/BC547B variants. Add a flyback diode (e.g. 1N4148) across inductive load terminals.

1

1N4001 Flyback Diode

General-purpose silicon rectifier diode used as a flyback / freewheeling diode to suppress back-EMF voltage spikes from inductive loads (DC motors, relays). Rated 50 V reverse voltage and 1 A forward current. Connected in parallel across the motor terminals with cathode (striped band) to the motor positive (+) / 5 V supply side and anode to the motor negative (−) / transistor collector side. Requires no firmware or library — purely passive protection component. Higher-voltage 1N400x rectifier variants are compatible substitutes when the current and package fit the design.

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

1 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

10 kohm

Through-hole resistor (current-limiting in series with an LED)

Assemble it in 5 steps

1. Prepare with USB unplugged

Use an ESP32 DevKit v1, a four-pin MQ-5 module, DHT11 and a two-pin 5 V active buzzer rated at most 30 mA. Use a reliable USB supply and cable; allow at least 500 mA total. Join all grounds.

  • This is an educational gas monitor, not a certified life-safety alarm. Never release fuel gas to test it.

2. Connect the temperature sensor

DHT11 VCC goes to 3V3, GND to GND and DATA to GPIO4. Connect the 10 kohm pull-up between DATA and 3V3. Use the printed pin labels; bare four-pin DHT11 sensors have an unused pin.

3. Protect the gas input

MQ-5 VCC goes to 5V and GND to GND. AO goes through the top 10 kohm resistor to GPIO32. Connect the bottom 10 kohm resistor from GPIO32 to GND. Leave DO disconnected. The two resistors halve the output voltage.

  • Never connect the 5 V analog or digital output directly to an ESP32 pin. The MQ-5 heater becomes hot; keep it away from the DHT11 and flammable materials.

4. Connect the buzzer switch

Connect GPIO25 through 1 kohm to the transistor BASE. Connect a 10 kohm resistor from BASE to GND. EMITTER goes to GND, COLLECTOR to buzzer minus, and buzzer plus to 5V. Place the diode across the buzzer with its striped end at plus and other end at minus.

  • Check the transistor's exact leg order against its supplier datasheet: 2N2222 variants do not all share the same leg order.

5. Warm up and check readings

Double-check the divider and polarity before connecting USB. Initially burn in the MQ-5 according to its supplier's instructions (typically 48–168 hours in ventilated clean air). Each restart has a 15-minute software warmup with gas alarm disabled. Read the serial output at 115200 baud. Send b to test a half-second beep.

  • The default alarm threshold is only a demonstration value, not a calibrated gas concentration or safe exposure limit. Do not assume silence means safe air.

Review all connections

1. Connections between "dht" and "ESP32"

FunctiondhtESP32
powerVCC3V3
groundGNDGND
dataDATAGPIO 4

2. Connections between "dht_pullup" and "ESP32"

Functiondht_pullupESP32
digitalP1 → DHT11 DATAEXT
powerP23V3

3. Connections between "gas" and "ESP32"

FunctiongasESP32
powerVCC5V
groundGNDGND
analogAO → Resistor P1EXT

4. Connections between "divider_top" and "ESP32"

Functiondivider_topESP32
digitalP2GPIO 32

5. Connections between "divider_bottom" and "ESP32"

Functiondivider_bottomESP32
digitalP1 → Resistor P2EXT
groundP2GND

6. Connections between "buzzer" and "ESP32"

FunctionbuzzerESP32
powerPLUS5V
digitalMINUS → NPN Transistor (2N2222 / BC547) COLLECTOREXT

7. Connections between "q1" and "ESP32"

Functionq1ESP32
groundEMITTERGND

8. Connections between "base" and "ESP32"

FunctionbaseESP32
digitalP1GPIO 25
digitalP2 → NPN Transistor (2N2222 / BC547) BASEEXT

9. Connections between "pulldown" and "ESP32"

FunctionpulldownESP32
digitalP1 → NPN Transistor (2N2222 / BC547) BASEEXT
groundP2GND

10. Connections between "diode" and "ESP32"

FunctiondiodeESP32
passiveCathode → 5 V two-pin active buzzer PLUSEXT
passiveAnode → 5 V two-pin active buzzer MINUSEXT

Deploy the firmware

#include <Arduino.h>
#include <DHT.h>
constexpr uint8_t DHT_PIN=4, GAS_PIN=32, BUZZER_PIN=25;
constexpr uint32_t WARMUP_MS=15UL*60UL*1000UL;
// Demonstration thresholds at the ESP32 input, NOT ppm or a safety limit.
constexpr int ALARM_MV=1500, CLEAR_MV=1350;
DHT dht(DHT_PIN,DHT11);
bool alarmActive=false;
uint32_t bootTime,lastReport=0;
void setup(){
  digitalWrite(BUZZER_PIN,LOW); pinMode(BUZZER_PIN,OUTPUT);
  Serial.begin(115200); dht.begin();
  analogReadResolution(12); analogSetPinAttenuation(GAS_PIN,ADC_11db);
  bootTime=millis();
  Serial.println("Gas demo: 15 minute warmup. NOT a certified gas alarmActive.");
  Serial.println("Threshold 1500 mV at ADC (about 3.0 V at MQ5 AO); adjust after burn-in. Send b to test buzzer.");
}
void loop(){
  uint32_t now=millis();
  static uint32_t testStart=0; static bool testing=false;
  if(Serial.available() && Serial.read()=='b'){testStart=now; testing=true;}
  if(testing && now-testStart>=500) testing=false;
  if(now-lastReport>=2000){
    lastReport=now;
    uint32_t sum=0; for(int i=0;i<16;i++) sum+=analogReadMilliVolts(GAS_PIN);
    int mv=sum/16;
    bool ready=now-bootTime>=WARMUP_MS;
    if(!ready) alarmActive=false;
    else if(mv>=ALARM_MV) alarmActive=true;
    else if(mv<=CLEAR_MV) alarmActive=false;
    float t=dht.readTemperature(),h=dht.readHumidity();
    Serial.printf("MQ5 ADC=%d mV, AO~%d mV | %s | ",mv,mv*2,ready?(alarmActive?"ALARM":"monitoring"):"warming up");
    if(isnan(t)||isnan(h)) Serial.println("DHT read failed: check wiring");
    else Serial.printf("Temperature %.1f C, humidity %.1f %%\n",t,h);
  }
  digitalWrite(BUZZER_PIN,(alarmActive||testing)?HIGH:LOW);
  delay(5);
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik