Skip to content

Gas & Air Quality · Espressif ESP32 guide

Interfacing MQ-7 Carbon Monoxide Sensor with Espressif ESP32

Dedicated CO sensor for indoor safety monitors and air-quality projects.

Updated November 21, 2026 Arduino C++Also forArduino Uno

MQ 7 practical reference

A gas sensor calibrated specifically for carbon monoxide detection, used in DIY CO alarm and indoor air-safety projects.

Key specifications

  • Sensing element: SnO2 (tin dioxide) semiconductor, sensitivity decreases as CO concentration decreases in clean air
  • Operating voltage: 5 V DC (heater circuit and sensing circuit both run on 5V)
  • Detection range: 20-2000 ppm carbon monoxide (CO)
  • Heating cycle: 5 V for 60s (high-temp burn-off), then 1.4V for 90s (low-temp sensing) — 150s total cycle for spec-accurate CO readings
  • Current draw: ~150 mA during the 5V heating phase — noticeably more than typical digital sensors, budget accordingly
  • Preheat time: Usable within minutes, but full stabilization for accurate baseline readings takes up to 48 hours of burn-in
  • Output: Analog voltage (AOUT) proportional to gas concentration, plus a threshold digital output (DOUT) via onboard comparator
  • Interface: Analog (AOUT) — requires ADC and Rs/Ro curve conversion to get ppm; DOUT is a simple threshold trigger

Pinout and connection notes

The heating cycle is not optional for spec accuracy — many breakout boards simply tie VCC straight to a constant 5V, which the sensor will tolerate and still respond to CO, but the datasheet's accuracy figures assume the 60s-high/90s-low duty cycle. If CO-selectivity actually matters for your project (vs. just "some gas is present"), drive the heater pin with a MOSFET or relay on a timer/PWM to reproduce that cycle rather than running it at a constant voltage.

AOUT is raw voltage, not ppm — converting to a ppm figure requires computing the Rs/Ro resistance ratio and mapping it against the sensor's datasheet curve, plus a clean-air calibration step to establish your board's Ro baseline. Treat raw analogRead() values as relative/qualitative unless you've done this calibration.

DOUT needs calibration too — the trigger threshold is set by the onboard potentiometer and is not accurate out of the box. Adjust it while exposing the sensor to a known-safe CO level (i.e., clean air) before trusting DOUT as an alarm trigger.

Power budget — the ~150mA heater draw is easy to overlook if you're used to sub-10mA digital sensors. On a 3.3V-logic board reading AOUT through a voltage divider, make sure your 5V supply rail (not just logic) can actually source this current, especially if several MQ-series sensors run simultaneously.

Sensor lifetime and drift — SnO2 elements degrade over 1-3 years of use and drift with humidity and temperature; periodic recalibration against known-clean air is necessary for any long-term deployment, not just a one-time setup step.

  • VCC: Power, 5V DC — also powers the internal heating element
  • GND: Ground
  • AOUT: Analog voltage output, proportional to gas concentration — read with an ADC, not a direct ppm value
  • DOUT: Digital threshold output — trips based on the onboard comparator, threshold set by the potentiometer on the module

Integration checklist

  1. Confirm the exact part number and breakout-board revision.
  2. Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
  3. Check controller pin assignments and bus address or interface mode against the example code.
  4. Power up with external loads disconnected, then test signals before full integration.
  5. If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.

Troubleshooting and reproducibility

Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.

Specifications

Sensor
MQ-7 tin-dioxide semiconductor CO sensor
Target
Carbon monoxide response; cross-sensitivity exists
Heater
Specified method uses alternating high/low temperature phases
Outputs
Bare sensor needs load circuit; modules may provide AO and DO
Power
Heater draws substantial current; separate rated supply/driver needed
Calibration
Requires conditioning and calibration against known CO concentration
Safety
Not a certified CO alarm; never replace a listed life-safety detector

Pinout

PinNameDescription
VCCModule supplyBoard-specific supply for sensing and heater circuit.
GNDGroundCommon ground.
AOAnalog outputRaw analog signal, not direct ppm.
DOComparator outputOptional threshold output; polarity depends on board.
H terminalsHeater driveHeater connections vary; use a rated driver for datasheet cycle.

Wiring & code

Setting up MQ 7 with Espressif ESP32

The MQ-7 module detects carbon monoxide (CO). A heated sensing element changes its resistance with the gas level, and the module turns that into two outputs: AO, an analog voltage that rises with the concentration, and DO, a digital signal from an onboard comparator that switches at a level you set with the blue trimmer. This guide wires it to an ESP32, reads AO with averaging, and builds a buzzer alarm that learns the clean-air level at start-up.

On an ESP32 there is one extra job: the module runs from 5 V, so its AO pin can swing close to 5 V, above the 3.3 V the ESP32's pins accept. A two-resistor divider fixes that.

Wiring

Component pinESP32 pinNote
VCC5V (VIN)The heater draws about 150 mA and needs 5 V, so not 3V3
GNDGNDShared with the ESP32
AOGPIO34 via divider10 kΩ from AO to GPIO34, 20 kΩ from GPIO34 to GND: AO can reach about 5 V
DONot connectedPulled up to 5 V on most modules; use a divider too if you need it
MQ-7 on an ESP32 in the SolderHub Simulator: VCC to 5V, GND to GND, AO through a 10 kΩ / 20 kΩ divider to GPIO34, and an active buzzer on GPIO25

Warning

This is a hobby sensor, not a certified CO alarm. Don't rely on it to protect people; use a certified carbon monoxide detector for that.

Warm-up and burn-in

The datasheet runs the MQ-7's heater in a cycle: 5 V for 60 seconds to clean the element, then 1.4 V for 90 seconds, with the CO reading taken at the end of the low phase. Most breakout modules simply hold the heater at 5 V, so they never run that cycle. Their readings still rise with CO, but treat them as relative, not as ppm. To follow the datasheet, switch the heater with a MOSFET and PWM it down to about 1.4 V for the low phase.

  • Every power-up: the heater takes a few minutes to reach a stable temperature. Readings drift down during that time, so ignore the first minutes.
  • A new sensor: the datasheet asks for a burn-in of about 48 hours powered before the readings are stable. Leave it running once before you calibrate anything.
  • It gets hot. The heater draws about 150 mA at 5 V and the mesh cap gets warm to the touch. That is normal; keep it away from anything that melts.

Read the sensor

sketch.ino
/*
  MQ-7 gas sensor on an ESP32: averaged reading of the analog output
  MQ-7 VCC -> 5V (the heater needs 5 V), GND -> GND
  MQ-7 AO  -> 10 kOhm -> GPIO34, and 20 kOhm from GPIO34 to GND
           (AO can reach ~5 V; the divider keeps GPIO34 under 3.3 V)
*/
const int AO_PIN = 34;                        // ADC1 pin: still works with Wi-Fi on
const float DIVIDER = (10.0 + 20.0) / 20.0;    // (R1 + R2) / R2 = 1.5
const int SAMPLES = 20;

int readAverage() {
  long sum = 0;
  for (int i = 0; i < SAMPLES; i++) { sum += analogRead(AO_PIN); delay(5); }
  return sum / SAMPLES;
}

void setup() {
  Serial.begin(115200);
  Serial.println("MQ-7 warming up: readings settle after a few minutes");
}

void loop() {
  int raw = readAverage();                     // 0..4095 (12-bit)
  float pinVolts = raw / 4095.0 * 3.3;
  float sensorVolts = pinVolts * DIVIDER;      // voltage at the module's AO pin
  Serial.print("raw ");
  Serial.print(raw);
  Serial.print("   AO ");
  Serial.print(sensorVolts, 2);
  Serial.println(" V");
  delay(500);
}

How it works. Inside the MQ-7, the sensing element's resistance (Rs) falls as the gas level rises. On the module it forms a voltage divider with a load resistor, so AO rises with more gas. Averaging 20 readings smooths out the noise from the heater and the air. The voltage alone is not a concentration: converting it to ppm needs the sensor's own clean-air resistance (R0) and the sensitivity curve from its datasheet, and each sensor differs. Treat it as more or less gas than before. On the ESP32 the reading is scaled back up by the divider ratio of 1.5 to give the voltage at the module's AO pin.

Try it in the simulator

This is the alarm build from the next section, wired as in the diagram. Press Run, wait for the baseline, then raise the gas level from the MQ-7's Settings: the buzzer sounds once the reading climbs past the baseline plus the margin.

Try this circuit in the SolderHub Simulator

Run this exact circuit in your browserReady-wired, code loaded

Confirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.

Buzzer alarm with a clean-air baseline

A fixed threshold breaks as soon as the room, the temperature or the sensor itself changes. This sketch instead measures the clean-air level right after warm-up and alarms when the reading rises a set margin above it. Add an active buzzer: + to GPIO25, − to GND. Power it up in clean air so the baseline is right.

sketch.ino
/*
  MQ-7 alarm: learn the clean-air level at start-up, sound a buzzer above it
  MQ-7 VCC -> 5V, GND -> GND, AO -> GPIO34 through a 10k/20k divider
  Active buzzer + -> GPIO25, - -> GND
*/
const int AO_PIN = 34;
const int BUZZER_PIN = 25;
const unsigned long WARMUP_MS = 5000;  // a real sensor needs a few minutes; 5 s keeps the demo quick
const int MARGIN = 200;               // ADC counts above the baseline that count as an alarm
const int SAMPLES = 20;
int baseline = 0;

int readAverage() {
  long sum = 0;
  for (int i = 0; i < SAMPLES; i++) { sum += analogRead(AO_PIN); delay(5); }
  return sum / SAMPLES;
}

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.println("Warming up in clean air...");
  delay(WARMUP_MS);
  baseline = readAverage();
  Serial.print("Clean-air baseline: ");
  Serial.println(baseline);
}

void loop() {
  int raw = readAverage();                    // 0..4095
  bool alarm = raw > baseline + MARGIN;
  digitalWrite(BUZZER_PIN, alarm ? HIGH : LOW);
  Serial.print(raw);
  Serial.println(alarm ? "   ALARM" : "");
  delay(250);
}
  • Tune the margin. Watch the numbers in the Serial Monitor with the sensor in clean air, then bring a source near it (only safe sources: never create CO on purpose; test with the simulator instead) and set MARGIN between the two.
  • Use a longer warm-up in a real build: replace 5 seconds with two or three minutes.
  • Prefer the trimmer? Turn the blue potentiometer until the DO LED just goes off in clean air, then read DO instead of AO for a simple on/off alarm. On an ESP32, DO needs its own divider because the comparator output is pulled up to 5 V.

Troubleshooting

SymptomFix
Reading falls steadily for minutes after power-upNormal heater warm-up: wait before trusting it
Reading never changesCheck the 5 V supply: the heater needs about 150 mA. A sensor fed from 3.3 V barely heats
Reading stuck near 4095The divider is missing or reversed: 10 kΩ from AO to the pin, 20 kΩ from the pin to GND
Reading drops when Wi-Fi startsKeep AO on an ADC1 pin (GPIO32–39); ADC2 stops when Wi-Fi is on
DO never changesTurn the blue trimmer: the threshold may be set past the range you are testing
Alarm triggers at power-upThe baseline was taken while the sensor was still warming up: lengthen WARMUP_MS

Keep learning

Related guides & projects