Reference
MQ
MQ-series analog gas sensors (MQ-2, MQ-3, MQ-4, MQ-5, MQ-7, MQ-8, MQ-9, MQ-135...): sensor resistance, Rs/R0 ratio, clean-air calibration and ppm from the datasheet curve.
- Connects over
- GPIO pins
- Made by
- Winsen / Hanwei (generic modules)
- Datasheet
- winsen-sensor.com
Use it
hardware {
device mq: MQ(pin: 16)
}
every 1s {
let value: float = mq.r0()
log("mq", "r0={value}")
}
The MQ is wired to ordinary GPIOs: give each of its pins (pin) as a named argument. The compiler checks every pin can do its job.
Methods
-
mq.calibrate(clean_air_ratio: float) -> float
Sample for ~5 s in clean air and set R0 = Rs / clean_air_ratio (ratios in tips); returns R0 in kOhm.
-
mq.ppm(a: float, b: float) -> float
Concentration from the datasheet curve ppm = a * (Rs/R0)^b (constants in tips).
-
mq.r0() -> float
Current R0 in kOhm (save it to storage after calibrating).
-
mq.ratio() -> float
Rs/R0: 1 at the calibration point, falls as gas increases.
-
mq.raw() -> int
ADC reading 0-4095, averaged over 16 samples.
-
mq.resistance() -> float
Sensor resistance Rs in kOhm.
-
mq.set_divider(factor: float)
Voltage divider factor between AO and the pin, e.g. 1.5 for 10k over 20k (default 1 = no divider).
-
mq.set_load(rl_kohm: float)
Load resistor RL on the module in kOhm (default 10).
-
mq.set_r0(r0: float)
Restore a saved R0 in kOhm.
-
mq.set_supply(volts: float)
Sensor supply voltage in V (default 5).
-
mq.voltage() -> float
Module output voltage in V (corrected for the divider).
Wiring
| Pin | Connect to |
|---|---|
VCC | 5V - the heater needs it |
GND | Ground |
AO | Analog out to an ADC pin through a divider (e.g. 10k/20k -> set_divider(1.5)); 5V on an ESP32 pin damages it |
DO | Comparator output, threshold set by the trimmer (not used by this driver) |
Specifications
- Load resistor
- RL usually 1k (newer) or 10k (older) on modules - check the SMD marked 102 or 103
- Output
- 0 - 5V analog (AO); use a divider for the 3.3V ADC
- Preheat
- 24-48 h burn-in when new, then ~1-3 min after each power-up
- Voltage
- 5V heater (~150 mA, ~800 mW)
Tips
- Curve constants for ppm(a, b) from the datasheet log-log graphs: MQ-2 LPG a=574.25 b=-2.222, smoke a=3616.1 b=-2.675; MQ-3 alcohol (mg/L) a=0.3934 b=-1.504; MQ-4 CH4 a=1012.7 b=-2.786; MQ-5 LPG a=80.897 b=-2.431; MQ-7 CO a=99.042 b=-1.518; MQ-8 H2 a=976.97 b=-0.688; MQ-9 CO a=599.65 b=-2.244; MQ-135 CO2 a=110.47 b=-2.862, NH3 a=102.2 b=-2.473.
- Clean-air Rs/R0 for calibrate(): MQ-2 9.83, MQ-3 60, MQ-4 4.4, MQ-5 6.5, MQ-7 27.5, MQ-8 70, MQ-9 9.6, MQ-135 3.6.
- Calibrate outdoors or in fresh air after warm-up, save r0() to storage, and restore it with set_r0() at boot.
- Readings shift with temperature and humidity; treat ppm as an estimate, good for thresholds and trends.
- MQ-7 and MQ-9 are specified with a cycling 5V/1.4V heater; on a constant 5V module their CO reading is only indicative.
Alternatives worth knowing:
- SCD4x: Calibrated CO2 in ppm.
- ENS160: Digital, low-power VOC / eCO2 / AQI.