Gas Sensor

MQ-135 Gas Sensor

The MQ-x series of smoke detectors work well with Raspberry Pi. This sensor, the MQ-135 and MQ-7, 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.

This is the Schematics and its potential use. 

This is the location of the Gas Sensor on the board. 

Following python code can be used to use the gas sensor.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# the sensor has to be connected to pin 1 for power, pin 6 for ground
# and pin 7 for signal(board numbering!).
 
import time, sys
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
 
def action(pin):
    print('Sensor detected action!')
    return
 
GPIO.add_event_detect(7, GPIO.RISING)
GPIO.add_event_callback(7, action)
 
try:
    while True:
        print('alive')
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.cleanup()
    sys.exit()