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
120

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

Connection
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

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

120 Comments

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

  2. 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!

  3. Pingback: Bandra Escorts
  4. Pingback: HUNTA 652
  5. Pingback: midnight rabbit
  6. Pingback: daftar joker123
  7. 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

  8. Pingback: PPPD-788
  9. 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.

  10. Pingback: SEO Vancouver
  11. 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!

  12. Pingback: text your ex day
  13. 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!

  14. Pingback: IPX-363
  15. 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?
    Cheers

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

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

  18. Pingback: properties UK
  19. Pingback: MyWegmansConnect
  20. Pingback: buy xanax online
  21. Pingback: iTools 4 Crack
  22. Pingback: Blockchain
  23. Pingback: sports
  24. Pingback: 618media.com
  25. Pingback: Artsy t shirts
  26. Pingback: runelite
  27. Pingback: ABP 897
  28. Pingback: marketing
  29. Pingback: Order weed online
  30. Pingback: Vape Pens For Sale
  31. Pingback: Family Lawyer
  32. Pingback: supreme cartridge
  33. Pingback: Dankwoods
  34. Pingback: stump removal
  35. Pingback: Minyak VCO
  36. Pingback: numbing spray
  37. Pingback: Eweka
  38. Pingback: Boston Car Service
  39. Pingback: virtual number buy
  40. 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.
    Thanks

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

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

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

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

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

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

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

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

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

    thanks

  51. 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
    GPIO.setmode(GPIO.BCM)
    gas_pin = 3
    GPIO.setup(gas_pin, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
    try:
    while True:
    g = GPIO.input(gas_pin)
    if g==0:
    print “gas detected”, g
    else:
    print “No Gas”, g
    except KeyboardInterrupt:
    GPIO.cleanup()

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

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

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

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

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

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

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

      EDIT:
      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?

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

  59. 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 Susheel Varma Cancel reply

Time limit is exhausted. Please reload CAPTCHA.