Recently I have been learning Python implementations on Raspberry Pi's GPIO system. I'm interested in wireless sensor network examples and decided to make a fire alarm system using MQ-2 sensor as the gas detector and Raspberry Pi as the main server (i think). My first lead to build this project was this link.


After I bought everything I need to build the circuit according to the tutorial, the program won't run itself. To make the sensor works, I finally realized that I had to install wiringPi and Spidev, which was actually not mentioned in the tutorial. In order to fully run the smoke sensing program, the user must implement the botbook_mcp3002 library that I found on internet, which was also not provided in the tutorial. I'm assumming that the writer of this short tutorial made it unwillingly and was being lazy too.

My realization of spidev on mcp3002 was helped by this second link -> http://raspberry.io/projects/view/reading-from-a-mcp3002-analog-to-digital-converter/ . It helped me a lot to try the mcp3002 ADC and explain everything clearly. The circuit diagram on the first link didn't work so now I'm using the mcp3002 picture on the second link. I replaced the trim potentiometer with the MQ-2 sensor (removed the resistor too). By joining the two tutorials, I was able to build the first phase of my desired fire alarm system and later develope it into a mobile app. Here is the simple code from the tutorial.

import time
import botbook_mcp3002 as mcp

smokeLevel= 0

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

def main():
    while True:
        print ("Current smoke level is %i " % smokeLevel) #
        if smokeLevel > 120:
            print("Smoke detected")


Some people said that sensors work better with arduino devices, since it is made to work in real-time with the needed voltage current for the sensor. But here I want to prove that Raspbery Pi can work with sensors too. That's why the MCP3002 analog-to-digital converter is necessary for this project. My problem now is the output value of smoke sensing system with Raspberry Pi based on the code above only represents one single value. It doesn't distinguish between smoke level, LPG level, and other flammable gases. Some youtube videos show me that arduino can actually show different values at the same time. So what should I add or do to the python code above in order to distinguish the values in Raspberry Pi? Any suggestion or ideas will be appreciated.

1 Answer 1


I'm afraid that you can't distinguish between the different gasses with that sensor. The sensor has only one analog output and is 'sensitive' to the different gases all at the same time. Those sensors tend to be used in buildings where they know that only one type of gas is likely to be present.

  • so it means that I need to use three mq-2 sensors to detect one of each gas?? will that be possible too??
    – Evorio
    Apr 14, 2015 at 14:21
  • @Evorio See playground.arduino.cc/Main/MQGasSensors for a description of the MQ-x series sensors. You'd need an analogue channel per sensor. So perhaps an MCP3004/8 would be a better choice.
    – joan
    Apr 14, 2015 at 14:26
  • The problem isn't the A2D he's using. The problem is the sensor itself. The sensor detects all the gases it detects all at the same time. It has only one analog output, so only one reading can be taken. Having multiple sensors won't help either because the sensor cannot be configured as to which gas will be detected.
    – recantha
    Apr 14, 2015 at 14:32
  • @joan thanks maybe that will work. I'll just have to switch from mcp3002 to mcp3004 then.
    – Evorio
    Apr 14, 2015 at 15:03
  • @recantha maybe you're right, when i read the mq-2 capabilities on flammable gases i thought it can separate the values for each gas. I was mistaken after all. thanks for the reply.
    – Evorio
    Apr 14, 2015 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.