I am currently doing a Smoke Alarm System project using Python 3 and a Raspberry Pi. At the moment I am testing the MQ2 Smoke Sensor Code but when running it the smoke level always remains 0.

With the MQ2 Smoke Sensor I am using the MCP 3002 Analog to Digital Converter as mentioned in this tutorial. I have installed all necessary libraries for the converter to work but I don't know where the problem is. This is the code for the Sensor:

import time
import botbook_mcp3002 as mcp #

smokeLevel= 0

def readSmokeLevel():
    global smokeLevel
    smokeLevel= mcp.readAnalog()

def main():
    while True: #
        readSmokeLevel() #
        if smokeLevel > 120:
            print("Smoke detected")
        time.sleep(0.5) # s

  • 1
    The problem is most likely to be in the wiring. Could you add a photo of your wiring to the question. It needs to show the connections you have made between the Pi, ADC, and sensor. The wire routing must be visible. Please don't just refer to a wiring diagram as that will lead to your question being ignored. – joan May 9 '16 at 17:17
  • You don't seem to be saying what channel you're reading from on the analog (mcp). You don't have to do mcp.readAnalog(0) or something do you? I'm not familiar with the library you're using. If you still don't have any joy, can I suggest trying the GPIO Zero library which supports the A2D in question. – recantha May 9 '16 at 19:55
  • checkout this tutorial. MQ gas sensor series arduino – bruce May 10 '16 at 20:41

A procedure for troubleshooting is to check different points in the chain.

MQ2 -> analog value -> MCP3002 -> digital value -> Raspberry Pi
  1. Measure if the MQ2 sensors output have different values, measure with volt meter depending on the gas content.

  2. Add a potentiometer so you can simulate a input signal to the MCP3002. VCC to one end, input on MCP3002 to mid and GND to the other end.

Now you can simulate a voltage sent to the MCP3002 and see if you get any response in your python code by turning the potentiometer knob.

Ref.: http://www.learningaboutelectronics.com/Articles/MCP3002-analog-to-digital-converter-ADC-to-Raspberry-Pi.php


Have you set the sensor up correctly? The raw sensor needs to have a resistor to ground off the A or B pins to act as a voltage divider. The sensor in these varies in resistance depending on gas concentration. If you only look at the output of the raw device without creating the divider you won't see anything. A diagram from the datasheet shows the basic hookup: Basic setup for the MQ-2

If you've purchased the MQ-2 already soldered onto a board with some components, then this is probably already taken care of. Check the output in either case with a DVM- you should see a change in voltage in the presence of almost any volatile hydrocarbon (a little alcohol on a cotton swab near the sensor will give a definite reading)

Your Answer

