Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie(see steps 1-5 in the Introduction)
Hardware:A digital sensor, e.g. from the MQ-X series

Digital sensors and the Raspberry Pi with the smoke detector MQ-X as example

The MQ-x series of smoke detectors work well with Arduino and Raspberry Pi. This sensor, the MQ-7 FC-22, has analog and digital output. Herein, we’ll use the digital output pin labeled d0 on the sensor to output a warning if CO(i.e., smoke) is detected. Alternatively, the analog output could be used to measure the level of CO, but this will need an analog/digital converter like MCP3008.

Test the sensor output Voltage
Take a multimeter and ensure that the sensors’ digital output does not deliver more than 3.3V. If it does, switching the sensors power supply from 5 Volt to 3.3 Volt may work, as it does work well for the MQ-7 smoke detector in this tutorial. Another cheap and easy solution is building a voltage divider.

MQ-7 F22 Smoke  Sensor for Rapberry Pi

MQ-7 F22 Smoke Sensor

The smoke level which raises an alarm can be adjusted with the analog potentiometer on the backside of the smoke sensor(the tiny blue rectangle at the bottom of the picture).

Connect the sensors’ GND pin to pin 6, the 5V pin to pin 1 and the data pin d0 to pin 7 of the Raspberry Pi as counted per board numbering.

Python script

This uses edge detection as it is way more efficient than permanently asking the sensor for its state.

Test the sensor

While running, the script prints alive; if the sensor is triggered, it prints Sensor detected action!. Breath contains high enough CO levels for testing purposes. No need to apply a flamethrower, Mr. Musk!

Analog sensors
Many sensors of the MQ-X series only have an analog signal out. For reading that data with an RPi, an A/D converter like the MCP3008 is required: Reading analog data with the MCP3008 and SPI

Reading analog data with the MCP3008 and SPI: http://www.knight-of-pi.org/reading-an-analog-potentiometer-with-the-ad-converter-mcp3008-and-spi/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. Hello, I am using MQ-2 smoke sensor with same coding and connection(as i am using DO connection) it is giving output “alive” only even when there is smoke and it is giving “Sensor detected action!” only when i am adjusting a potentiometer. what could be the possible reason for this problem?
    Kindly help.

    • Hello,

      i thought about it, i have no idea what’s happening there. That is indeed very very strange. Independent of any wiring, the while loop that prints alive will always be called.
      There are no exception messages in the terminal?
      And if you disconnect the sensor and start the script, it prints alive repeatedly, right?

  2. Hey, thanks for this tutorial. I used this code for my project and it works, however when i constantly blow CO gas on the sensor, it doesn’t constantly detect the gas. Do you know what I can do to rectify this issue. Thanks in advance.

    • Hello!

      Well, that’s difficult to fix, but i’d probably implement a software based solution which handles misreads, like some sort of rolling average.

  3. hey just a quick question. how can i connect multiple MQ-7s to a single raspberry pi? like if i was to install an array of the sensors across the room just so that it is easily detected. how might i be able to do that? do i have to use like a WIFI/ GSM module ? is that even supported by the sensors ? like MQ-2 or MQ-9 etc ?

    • Hello!

      You can attach other sensors to other pins of the Raspberry Pi, e.g. pin 3 and 5 in BOARD numbering. You’d just have to run wires accross the room, i think that’s the cheapest and most simple solution.

  4. Hello, any idea how can i send these sensor data to AZURE cloud?

  5. Hi. I want to ask do you have tutorial on MQ2 using mcp3002 ADC?

  6. knight. when i run this code i only get alive. i am using lighter for testing for the gas. i am using mq2 sensor itself.

  7. when i run the code it gives me error in line 18 i e gpio.add.event.detect(7,gpio.rising)
    runtimeError: failed to add edge detection

  8. Hi
    I used this code on MQ-5. It doesn`t work. It always shows ‘alive’…
    How can I do? should I have MCP3008 ?
    MQ-5 has A0, D0 both…

  9. hy ,
    i have followed your tutorial to read out the MQ-6 sensor by pi , but when i start the python , its only print alive , after i tested it using a gas from a lighter , it doesn’t make an effect its only print alive , ,
    for note i am using a MCP3002 for ADC , is it the phyton code is different if i use the MCP 3002 ADC ,
    hope you can help me ,

    • Hi alfin,

      i thought Adafruit had something on the MCP3002 but i can’t find it any more. Maybe use an MCP3008 instead?

  10. my MQ-7 only detects gas but no smoke – wrong sensor for smoke?

  11. hi I’m using MQ2 and the code keeps printing alive as i see in the comments u told someone who used mq2 to us A/D converter… so how is it used ? and whats the code for it


  12. I have written this code to print message gas detected if MQ-6 sensor triggers otherwise no gas.. but its always displaying no gas. There is led built with MQ-6 sensor which is turning on when some gas is detected but the condition g==0 is not resulting as true. I have connected optically isolated MQ-6 sensor which is active low.

    can any one tell me what could be the problem in my code.

    import sys
    import RPi.GPIO as GPIO
    from time import sleep
    gas_pin = 3
    GPIO.setup(gas_pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
    while True:
    g = GPIO.input(gas_pin)
    if g==0:
    print “gas detected”, g
    print “No Gas”, g
    except KeyboardInterrupt:

    • Are you using the breakout board recommended by Sparkfun or something similar? Without an accessory board, the sensor can’t be read.

  13. on my raspberry pi 3
    it gives an error: Failed to add edge detection
    what is the problem?

    • Hmm, is it a recent Raspbian Jessie you’re using?
      I tried the script and it works fine. You could try a different pin, e.g. pin 3 or pin 5 in BOARD numbering, and change the script accordingly.

  14. Hello! I am using a raspberry Pi 3 and an MQ-6 sensor, it seems that it is able to detect gas from a lighter when The Vcc (Vin) is attatched to a 5 volts and not on an 3.3Volts pin! I am afraid that the sensor’s digital input in the raspberry Pi is at 5V which as you know is above the maximum voltage the Pi can have which can cause permanent damage (yikes!)! Is there a way to get around this? Also the ground pin the raspbery Pi is allowed to accept 5 volts right?

  15. Hi Knight,
    Should I add a resistor to the sensor while hooking it up with the Pi? Also I am using a Gas sensor MQ6 .
    Thanks in advance

  16. Hi

    In this example the raspberry takes only rising edge and falling edge, right? How could I read the exact value of the sensor? I own a sensor that will 10-1000 PPM. I need the exact amount. Could you help me please?

    • Andre,
      you’ll need to read the data from the analog output of the sensor. It’s probably labeled A0. Since the Raspberry Pi does not have an analog input, you’ll need an analog-to-digital converter (ADC) like the MCP3008 or MCP3002.

    • Hi,
      as i see it that’s just the ionisation chamber, again, so i guess that would be difficult or at least requires an A/D converter…

  17. i am using smoke sensor HIS-07 i bought it in egizmo, that code has a problem the problem is there is no error but while running the program the only output is alive. even though there is a smoke detected, the “sensor detected action” is not appearing. only the “Alive” appear can you please help me to solve this problem ?

    • Hey Ernest,
      i’m pretty sure this can’t work, sadly. You just have a ionisation chamber without the accessory electronics coming with the MQ series types of sensors. It’s probably the best you buy another one like the MQ FC-22. They are quite cheap.

  18. Hi Jake,
    I am using a MQ 135 gas sensor and in my output it only says alive.

    My other request would be I need to measure all the gases and I need the readings on a 16 by 2 LCD Display, could you please let me know if I need to code it for individual gases or I can use one variable.

    • Hi Manisha,
      i’m sorry but i don’t know this MQ 135 gas sensor. But you made me curious, i’ll buy one and update when i find the time.

      Just saw a pic of the sensor on Amazon. It has a Dout which is a digital output and should work for data(pin 7 in BOARD) like the MQ7 in the tutorial. Maybe you do not have enough gas to produce a signal? For measuring the amount of gas, you’ll need to use the analog pin and a analog-to-digital converter.

    • Hi i’m using the same smoke sensor(MQ135), printing ‘alive’, but only detects when i place the digital pin in 3.3V or 5V, but never detects on direct smoke, though I adjust the trimmer on it. What may be solution to it?

  19. Hi,
    How can I get sound when alarm is trigerred (Sensor detected action!) ?
    Lets say I have wav file on my rpi and would like it to play if alarm is triggered.

  20. Hi
    Thanks for this! I have one question, if I was looking to output a switch (turn something on) when smoke was detected how would I do that? Thank you

Leave a Reply to Knight of Pi Cancel reply

Time limit is exhausted. Please reload CAPTCHA.