PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Sensors

Smell in ppm

HardBudget $14Python · gpiozero

An MQ-2 gas sensor powered from 5 V. Its analog AO (0–5 V) goes through a 10 kΩ / 20 kΩ divider (10 kΩ from AO to the tap, 20 kΩ from the tap to GND) into channel 0 of an MCP3008 on the SPI pins (VREF on 3.3 V). A red LED on GPIO17.

The maths:

  • AO voltage = ADC voltage × 1.5 (undo the divider).
  • The sensing element: Rs = 5000 × (5.0 − AO) / AO ohms (5 kΩ load resistor, 5 V supply).
  • In clean air Rs / R0 = 9.8. The air is clean for the first 3 s: measure Rs then and work out R0.
  • With gas, ppm = (Rs / R0 / 21) ^ (−1 / 0.47).

From 4 s on, every second print the concentration rounded to the nearest 100 ppm — Gas: 2000 ppm — and light the LED while it is above 1000 ppm. The test leaks 2000 ppm at 4 s and airs the room down to 500 ppm at 8 s.

More in Sensors