MQ 135 Gas Sensor with ESP32 (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 · ADVANCED

MQ 135 Gas Sensor with ESP32 (MicroPython)

This example reads air quality on the ESP32 with the MQ135 sensor — a broad-spectrum gas detector with a threshold alert. The general air-quality monitor pattern in MicroPython.

Board ESP32 DevKit (Xtensa LX6, 240 MHz)
Difficulty advanced
Category sensors
Libraries None — just the Arduino core
Runs in Sensbench browser simulator — free, no account
ESP32 MQ135 air quality sensor 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

# MQ-135 connected to GPIO 34
mq135 = ADC(Pin(34))
mq135.atten(ADC.ATTN_11DB)      # Allow 0–3.3V
mq135.width(ADC.WIDTH_12BIT)    # 12-bit ADC (0–4095)

print("MQ-135 Sensor Test")
print("Warming up sensor...")

time.sleep(10)   # short warm-up for testing

while True:
    total = 0
    samples = 10

    for i in range(samples):
        val = mq135.read()
        total += val
        time.sleep(0.1)

    avg_val = total // samples

    print("MQ-135 ADC Value:", avg_val)
    time.sleep(2)

How it works

The firmware reads the MQ135’s analog output after warm-up and compares it to a quality threshold. The loop samples continuously, and the alert fires when the air crosses the set point. The raw reading is logged, so the threshold can be tuned against the room’s baseline.

The sensor’s broad sensitivity is the firmware’s context: the threshold is a pollution level, not a gas concentration. The code’s job is the comparison and alert, exactly like the MQ4 example, with the sensor’s generality as the only difference.

Before you build it for real

The MQ135 is sensitive to everything, specific about nothing — the reading rises for cooking fumes, alcohol and humidity alike, so the alert means “air changed” not “gas X present”. The baseline shifts with temperature and humidity, and the sensor’s warm-up (minutes for full stability on this family) means the first readings after power-on are not trustworthy. And calibration requires a known gas source — without one, the threshold is a relative knob, not an absolute air-quality measurement.

Get the parts

Related examples

FAQ

What does the MQ135 measure?

A broad family of gases — ammonia, benzene, alcohol, smoke, CO2 — with one analog output. It is the general air-quality sensor: sensitive to many things, specific about none. The firmware’s threshold is a general pollution indicator, not a gas-specific measurement.

Why is the MQ135 called an air-quality sensor?

Because its wide sensitivity profile makes it a reasonable stand-in for general indoor pollution — the reading rises when the air gets worse, without saying exactly what got worse. For specific gases, the MQ4/MQ7 siblings are the right tools.