11. Gas Sensor

The MQ-9 Analog Gas Sensor is useful for gas leakage detection (in home and industry). It has high sensitivity and is suitable for a variety of applications. When temperature rises (heated by 5.0V), it detects Methane, Propane, combustible gas, etc and cleans the other gases adsorbed under low temperature. Due to its high sensitivity and fast response time, measurements can be taken instantly. The sensitivity of the sensor can be adjusted by using the potentiometer.

NOTE: The sensor value only reflects the approximated trend of gas concentration. It does not represent the exact gas concentration.

Warning

Before proceeding with this sensor, you will need to learn about the MCP3008 ADC.


11.1. Connecting the Sensor

The gas sensor is an analog sensor and cannot be connected directly to the Raspberry Pi pins. It needs an analog to digital converter, MCP3008 ADC in this case, in order to collect proper data.

../_images/gassensor.jpg

  • Black wire is connected to GND
  • Red wire to RPi VCC(3.3V)
  • Blue wire (green in this Figure) to one of MCP3008’s left pins.

Warning

This Sensor gets hot when connected to RPi, so do not touch it after it is connected.


11.2. Python Program Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import spidev
import time

delay = 0.5
ldr_channel = 0

spi = spidev.SpiDev()
spi.open(0,0)

def readadc(num):
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer([1, 8+adcnum << 4, 0])
    data = ((r[1]&3)<<8 + r[2])
    return data

while True:
    Sum=0
    Sum2=0
    ldr_value=readadc(i)
    if ldr_value != 0:
        result = ldr_value/1024;
        Sum=Sum+10
        while result != (ldr_value%1023):
            result = result/1024
            Sum = Sum + 10
            if Sum > 1024:
                break
            
        result = ldr_value%1023
        while result != 1:
            result = result/2
            Sum2 = Sum2 + 1
            if Sum2 > 1024:
                break

    result = ((Sum + Sum2) * 5.0)/1024

    print ("_______________________")
    print (result)
    time.sleep(delay)

The Value read from the sensor is displayed. This value can be used as a threshold to detect any increase/decrease in gas concentration. For example, if the first values read ranges between 0.4 and 0.5, this means that this range is the threshold. Any increase/decrease in gas concentration will increase/decrease these values.

To test it, bring a lighter close to the sensor, and start emitting gas (JUST GAS). Beware not to light it.

In this next code we added a LED (on pin 7), so that any increase/decrease will turn the LED ON

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import spidev
import time

delay = 0.5
ldr_channel = 0

spi = spidev.SpiDev()
spi.open(0,0)

def readadc(num):
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer([1, 8+adcnum << 4, 0])
    data = ((r[1]&3)<<8 + r[2])
    return data

temp = 0

while True:
    Sum=0
    Sum2=0
    ldr_value=readadc(i)
    if ldr_value != 0:
        result = ldr_value/1024;
        Sum=Sum+10
        while result != (ldr_value%1023):
            result = result/1024
            Sum = Sum + 10
            if Sum > 1024:
                break
            
        result = ldr_value%1023
        while result != 1:
            result = result/2
            Sum2 = Sum2 + 1
            if Sum2 > 1024:
                break

    result = ((Sum + Sum2) * 5.0)/1024

    if temp == 0:
        temp = result
        
    print ("_______________________")
    print (result)
    print (temp)
    
    if (result > (temp*2) ) or (result < (temp/2)):
        GPIO.output (7, True)
    elif(result < (temp*2)) or (result > (temp/2)):
        GPIO.ouput (7, False)
    
    time.sleep(delay)