MQ7 CO Gas Detection ESP32 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

MQ7 CO Gas Detection ESP32

This example detects carbon monoxide on the ESP32 with the MQ7 sensor — heater cycling, phase-timed sampling, and a threshold alarm. The CO-detector pattern, including the MQ7’s special timing.

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 MQ7 carbon monoxide detection 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-7 analog output on GPIO 35
mq7 = ADC(Pin(35))
mq7.atten(ADC.ATTN_11DB)       # 0–3.3V
mq7.width(ADC.WIDTH_12BIT)     # 12-bit ADC

print("MQ-7 CO Sensor Test")
print("Warming up MQ-7 sensor (60 seconds)...")

# Warm-up phase
time.sleep(60)

print("Start reading MQ-7 values")

while True:
    readings = []
    for i in range(10):
        readings.append(mq7.read())
        time.sleep(0.1)

    avg_val = sum(readings) // len(readings)
    print("MQ-7 ADC Value:", avg_val)

    time.sleep(2)

How it works

The firmware cycles the MQ7’s heater and samples the sensing element at the valid phase, comparing the reading against the CO threshold. The alternating heater cycle is the MQ7’s signature — the datasheet’s timing defines when a sample means anything.

The threshold-and-alert loop mirrors the MQ4 example, with the heater cycling as the added complexity. The serial output logs the phase and the reading, making the sensor’s timing behavior visible in the console.

Before you build it for real

The MQ7’s heater cycle timing is the datasheet’s law — sample at the wrong phase and the reading is meaningless; the simulator models the correct cycle, real wiring must reproduce it exactly. CO detection is life-safety territory: a hobby sensor and a threshold are not a certified detector, and real deployments need calibrated sensors, redundancy and loud local alarms. And the baseline drifts with humidity and age — the MQ7’s sensitivity degrades over its lifetime, so annual recalibration is the maintenance no hobby project documents.

Get the parts

Related examples

FAQ

Why is the MQ7 different from the MQ4?

Different target gas: the MQ7 is for carbon monoxide, the MQ4 for methane. Both are tin-oxide sensors with heaters, but the MQ7’s heater cycle is special — it alternates between heating and cooling phases, and the reading is only valid at the right phase.

Where should a CO detector be placed?

Unlike methane, carbon monoxide mixes evenly with air, so the sensor goes at breathing height — often near bedrooms. The firmware does not care, but the placement is the difference between a detector and a decoration.