Built-in project · Raspberry Pi 5

MQ-2 gas sensor on a Pi with an MCP3008, on a Raspberry Pi 5

An MQ-2 on a Raspberry Pi 5: its 5 V output divided down to 3.3 V, read through an MCP3008 with gpiozero, and an LED that lights when gas passes the limit. The whole build, a Raspberry Pi 5 and 6 more parts, runs here in your browser on the Python below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $67. On Mokxi they're free.

MQ-2 gas sensor on a Pi with an MCP3008 · Raspberry Pi 5live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The Python exactly as the editor opens it. Change a line there and press Run: the board runs main.py in a Python engine in the page, with gpiozero and RPi.GPIO, and nothing to compile.

main.py · Raspberry Pi 5
# An MQ-2 gas sensor on a Raspberry Pi 5, through an MCP3008.
# A Pi has no analog inputs, so the MCP3008 reads the MQ-2's AO on SPI0
# (CLK GPIO11, MISO GPIO9, MOSI GPIO10, CE0 GPIO8), from 3.3 V.
# The MQ-2's heater needs 5 V, so AO can reach 5 V: a 10 k and 20 k divider
# brings it down to two thirds before CH0. The alarm LED is on GPIO18.
from gpiozero import MCP3008, LED
from time import sleep

gas = MCP3008(channel=0)    # reads 0 to 1 of 3.3 V
alarm = LED(18)
DIVIDER = 1.5               # AO is 1.5 times what CH0 sees
LIMIT = 0.4                 # volts on AO: clean air is about 0.05

print("Warming up the heater...")
sleep(20)                   # a cold MQ-2 reads like a room full of gas

while True:
    ao = gas.voltage * DIVIDER
    if ao > LIMIT:
        alarm.on()
        print(f"AO {ao:.2f} V  GAS!")
    else:
        alarm.off()
        print(f"AO {ao:.2f} V  clean")
    sleep(1)

Parts list

8 parts, plus the jumper wires. They retail for about $67; on Mokxi they're free.

How it is wired

12 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Raspberry Pi 5 pin 3V3; Eight-channel ADC, MCP3008 pin VREF; Eight-channel ADC, MCP3008 pin VDD
  • Raspberry Pi 5 pin 5V; Gas sensor, MQ-2 pin VCC
  • Ground: Raspberry Pi 5 pin GND; Eight-channel ADC, MCP3008 pin DGND; Eight-channel ADC, MCP3008 pin AGND; Gas sensor, MQ-2 pin GND; Resistor, 20k Ω pin 2
  • Raspberry Pi 5 pin GPIO18; Resistor, 330 Ω pin 1
  • Raspberry Pi 5 pin GPIO10; Eight-channel ADC, MCP3008 pin DIN
  • Raspberry Pi 5 pin GPIO9; Eight-channel ADC, MCP3008 pin DOUT
  • Raspberry Pi 5 pin GPIO11; Eight-channel ADC, MCP3008 pin CLK
  • Raspberry Pi 5 pin GPIO8; Eight-channel ADC, MCP3008 pin CS
  • Ground: Raspberry Pi 5 pin GND; LED, red pin C
  • Eight-channel ADC, MCP3008 pin CH0; Resistor, 10k Ω pin 2; Resistor, 20k Ω pin 1
  • Gas sensor, MQ-2 pin AO; Resistor, 10k Ω pin 1
  • Resistor, 330 Ω pin 2; LED, red pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.