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

Gather all the parts
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"
2. Connections between "dht_pullup" and "ESP32"
3. Connections between "gas" and "ESP32"
4. Connections between "divider_top" and "ESP32"
5. Connections between "divider_bottom" and "ESP32"
6. Connections between "buzzer" and "ESP32"
7. Connections between "q1" and "ESP32"
8. Connections between "base" and "ESP32"
9. Connections between "pulldown" and "ESP32"
10. Connections between "diode" and "ESP32"
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.




