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. Pingback: small bullet vibe
  2. Hi i am kavin, its my first occasion to commenting anywhere, when i read this paragraph i thought i could also create
    comment due to this brilliant post.

  3. I like the valuable information you provide in your articles.
    I will bookmark your weblog and check again here frequently.
    I am quite certain I’ll learn lots of new stuff right
    here! Best of luck for the next!

  4. Pingback: Bandra Escorts
  5. Pingback: HUNTA 652
  6. Pingback: midnight rabbit
  7. Pingback: daftar joker123
  8. I do not know if it’s just me or if everybody else experiencing problems with your blog.
    It looks like some of the written text on your content are
    running off the screen. Can someone else please comment and let me
    know if this is happening to them as well? This could
    be a problem with my internet browser because I’ve had this happen before.
    Many thanks

  9. Pingback: PPPD-788
  10. Hey There. I found your blog using msn. This is an extremely well written article.
    I will be sure to bookmark it and return to read extra of your helpful info.

    Thank you for the post. I will certainly return.

  11. Pingback: SEO Vancouver
  12. Wow, amazing blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is
    excellent, let alone the content!

  13. Pingback: text your ex day
  14. Hi! This post could not be written any better!
    Reading this post reminds me of my previous room mate! He always kept chatting about this.
    I will forward this post to him. Pretty sure he will have a good read.
    Thank you for sharing!

  15. Pingback: IPX-363
  16. Hi there! This is kind of off topic but I need some help from
    an established blog. Is it very difficult to set up your
    own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about making my own but I’m not sure where to begin. Do you have any points or suggestions?

  17. Can I just say what a comfort to uncover an individual who really knows what they’re discussing over the internet.
    You definitely know how to bring a problem to light and make it important.
    More people need to read this and understand this side
    of your story. I was surprised that you’re not more popular since
    you certainly possess the gift.

  18. Howdy! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked
    hard on. Any recommendations?

  19. Pingback: properties UK
  20. Pingback: MyWegmansConnect
  21. Pingback: buy xanax online
  22. Pingback: iTools 4 Crack
  23. Pingback: Blockchain
  24. Pingback: sports
  25. Pingback: 618media.com
  26. Pingback: Artsy t shirts
  27. Pingback: runelite
  28. Pingback: ABP 897
  29. Pingback: marketing
  30. Pingback: Order weed online
  31. Pingback: Vape Pens For Sale
  32. Pingback: Family Lawyer
  33. Pingback: supreme cartridge
  34. Pingback: Dankwoods
  35. Pingback: stump removal
  36. Pingback: Minyak VCO
  37. Pingback: numbing spray
  38. Pingback: Eweka
  39. Pingback: Boston Car Service
  40. Pingback: virtual number buy
  41. I do not know if it’s just me or if everyone else encountering issues with your site.
    It seems like some of the written text within your posts are running off the screen. Can somebody else
    please provide feedback and let me know if this is happening to them as well?
    This could be a issue with my browser because I’ve had this happen before.

  42. I simply couldn’t depart your site before suggesting that I extremely enjoyed the
    usual info an individual provide to your guests? Is gonna
    be back continuously to check up on new posts

  43. Pingback: pc games download
  44. Hello, any idea how can i send these sensor data to AZURE cloud?

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

  46. 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.

  47. 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

  48. 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…

  49. 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?

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

  51. 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


  52. 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.

  53. 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.

  54. 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?

  55. 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

  56. 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…

  57. 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.

  58. 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?

  59. 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.

  60. 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.