MQ4 Gas Leak Detection System using ESP32 and MicroPython is a runnable microcontroller simulation example on SensBench — open the wired circuit, inspect the code, and compile real firmware in your browser, no install needed. Last updated Aug 14, 2026.

FREE SIMULATOR EXAMPLE · BEGINNER

MQ4 Gas Leak Detection System using ESP32 and MicroPython

This example runs a gas leak detector on the ESP32 — the MQ4 methane sensor, a warm-up delay, and a threshold alert loop. The kitchen gas-alarm pattern in MicroPython.

Board ESP32 DevKit (Xtensa LX6, 240 MHz)
Difficulty beginner
Category basics
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
ESP32 MQ4 gas leak detection system in the Sensbench simulator

The exact circuit you get when you open this example — already wired, ready to run.

The sketch

# main.py
from machine import ADC, Pin
import time

# MQ4 gas sensor setup
mq4 = ADC(Pin(34))  # Analog input pin
mq4.atten(ADC.ATTN_11DB)  # Full voltage range
mq4.width(ADC.WIDTH_10BIT)  # 10-bit resolution

# Buzzer setup
buzzer = Pin(15, Pin.OUT)

# Threshold for gas alert (tune this based on testing)
GAS_THRESHOLD = 240

while True:
    gas_value = mq4.read()
    voltage = gas_value * 3.3 / 1023
    
    print("Gas reading (raw):", gas_value)
    print("Voltage:", round(voltage, 2), "V")

    # Alert condition
    if gas_value > GAS_THRESHOLD:
        buzzer.on()
        print("⚠️ Gas leak detected! Buzzer ON")
    else:
        buzzer.off()

    print("----------------------------")
    time.sleep(2)

How it works

The firmware reads the MQ4’s analog output after a warm-up delay and compares it to a threshold. Above it, the alert path triggers — an LED and serial message, with the buzzer or relay being the real-world actuator. The loop samples continuously so a leak is caught within one sample period.

The threshold is the calibration point: the MQ4’s baseline varies with clean-air conditions, so the code’s threshold must be set against the sensor’s actual baseline, not a datasheet number. The serial output logs the raw reading, which is how the threshold gets tuned.

Before you build it for real

The MQ4’s warm-up is mandatory — 20-30 seconds on real hardware, and readings taken early are garbage; the simulator’s instant readiness hides a real deployment’s startup window. The threshold drifts with humidity and temperature — the sensor’s baseline shifts with the weather, so a threshold set in winter alarms falsely in summer; real designs use a self-calibrating baseline. And the sensor alone is not a safety system — gas detectors need redundancy, alarm persistence and placement near the ceiling (methane rises), none of which the firmware can provide.

Get the parts

Related examples

FAQ

What does the MQ4 detect?

Methane and natural gas (CNG/LNG) — the MQ4’s tin-oxide element changes resistance in the presence of combustible gas. It is the classic kitchen gas-leak sensor, and the firmware’s job is the threshold-and-alert loop around it.

Why does the sensor need warm-up?

The MQ4’s heater element must reach operating temperature before the sensing surface stabilizes — typically 20-30 seconds on real hardware. Readings taken during warm-up are meaningless, which the firmware’s startup delay accounts for.