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
14

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

On using sensors with digital output:
Use a multimeter to ensure that the sensors’ digital output does not deliver more than 3.3V. If it does, switching the sensors power supply from 5V to 3.3V 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-x smoke detectors

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 CO2(i.e., smoke) is detected. Alternatively, the analog output could be used to measure the level of CO2, but this will need an analog/digital converter like MCP3008.

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!. No need to burn things to test the sensor as breath contains high co2 levels for testing purposes.

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.

14 Comments

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

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

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

    • Hey Jake,

      i’m sorry but i don’t know/own a mq-3 ethanol sensor.
      Generally, it should work similar to the other MQ-series sensors. Do you want to measure the ethanol level directly or just a digital signal with a treshold like in the tutorial?

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

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

Comment

Time limit is exhausted. Please reload CAPTCHA.