Gas & Air Quality
Interfacing MQ 2 Gas & Air Quality with Raspberry Pi
What is the MQ 2?
MQ 2 practical reference
A widely used analog gas sensor tuned for detecting LPG, smoke, and combustible gases in DIY safety and air-quality projects.
Key specifications
- Sensing element: SnO2 (tin dioxide) semiconductor — resistance drops in the presence of combustible gases
- Operating voltage: 5 V DC (heater and sensing circuit both run continuously on 5V, no duty-cycle required)
- Detection range: 300-10,000 ppm — LPG, propane, hydrogen, methane, and smoke
- Preheat time: Usable within ~20s of power-up; full baseline stabilization takes 24-48 hours of burn-in
- Current draw: ~150 mA, dominated by the heating element running continuously
- Output: Analog voltage (AOUT) proportional to gas concentration, plus a threshold digital output (DOUT) via onboard comparator
- Interface: Analog (AOUT) — requires an ADC and a datasheet Rs/Ro curve to convert to ppm; DOUT is a simple threshold trigger
Pinout and connection notes
Heater runs constantly at 5V — unlike the MQ-7/MQ-9, the MQ-2 doesn't need a duty-cycled heating voltage; a plain, constant 5V supply is correct per its datasheet. Just budget for the ~150mA continuous draw, which is easy to underestimate if your power rail is shared with other sensors.
AOUT is a raw voltage, not ppm — converting a reading to an actual ppm figure needs the Rs/Ro resistance ratio and the datasheet's gas-response curve, plus a clean-air calibration step to establish your specific sensor's Ro baseline. Don't treat raw analogRead() values as calibrated gas concentrations.
DOUT threshold needs manual calibration — the onboard potentiometer sets the trip point for DOUT and is not accurate out of the box; adjust it in known clean air before trusting it as an alarm trigger.
Don't touch the sensing element while powered — the metal mesh cap covers an actively heated element; it gets warm during normal operation.
Expect drift and burn-in behavior — readings during the first uses after unboxing will be noisier and less accurate than after ~24-48 hours of cumulative on-time; don't calibrate against a brand-new, un-burned-in sensor.
- VCC: Power, 5V DC — also powers the internal heating element continuously
- 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
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- 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.
Setting Up MQ 2 with Raspberry Pi
Library
Adafruit-MCP3008 by Adafruit
pip3 install adafruit-circuitpython-mcp3xxx
Wiring
| Component pin | Raspberry Pi pin | Note |
|---|---|---|
| VCC | 5V (physical pin 2) | Heater needs the full 5V rail |
| GND | GND (shared with MCP3008) | |
| AOUT | Through a divider into MCP3008 CH0 | The Pi has no analog input — route through an MCP3008 over SPI, and divide the sensor's ~5V swing down to the MCP3008's 3.3V reference |
| DOUT | GPIO27 (optional) | Digital threshold output — check your breakout's logic level before wiring straight to a GPIO |
Swipe sideways to see every column →
- The Pi has no analog input pins at all, so an external ADC (MCP3008 here) is mandatory for this sensor, not optional.
- Wire the divider for a ~5V source into the MCP3008 the same way as the ESP32 setup — the MCP3008 itself also tops out at 3.3V.
- Same relative-reading and burn-in caveats apply as on the other boards — see the Arduino Uno notes on this page.
Related Components
Related Guides & Projects
Raspberry Pi Pico W and Pico 2 W: Install MicroPython with Thonny or UF2
Hold BOOTSEL, drag the correct UF2 file, open the REPL and blink the onboard LED. Includes the Pico vs Pico W vs Pico 2 W firmware differences and what to do when the drive does not appear.
Raspberry Pi Pico W Web Server in MicroPython: Control an LED and Read a Sensor
Connect the Pico W to WiFi, serve a responsive control page, switch an LED and display the onboard temperature or an external sensor, with timeouts and reconnect handling.
Raspberry Pi Pico Oscilloscope with MicroPython
Convert a Raspberry Pi Pico into a basic 500 kSPS oscilloscope using its 12-bit ADC and MicroPython. Displays waveforms on an SSD1306 OLED or streams samples over USB serial to a Python plotter on your PC.
Self-Hosted Nextcloud NAS on Raspberry Pi 4
Build a private, durable Nextcloud NAS on a Raspberry Pi 4 -- SSD boot instead of a failure-prone SD card, Docker Compose with Redis caching, Tailscale for remote access with zero ports forwarded, and an automated backup strategy that actually runs.