-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmq-2.py
50 lines (43 loc) · 1.39 KB
/
mq-2.py
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
#!/usr/bin/env python
#-----------------------------------------------------
#
# This is a program for MQ-2 Gas Sensor Module.
# It could detect danger gas and smokes.
#
# This program depend on ADC0832 ADC chip. Follow
# the instruction book to connect the module and
# ADC0832 to your Raspberry Pi.
#
#-----------------------------------------------------
import RPi.GPIO as GPIO
import ADC0832
import time
BEEP = 16 # Set buzzer pin
TS = 100 # You can set the Threshold by yourself (0-255)
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(BEEP, GPIO.OUT) # Set pins' mode is output
ADC0832.setup() # Setup ADC0832
def loop():
while True:
tmp = ADC0832.getResult() # Get analog value from ADC0832
print tmp # Print analog value
if tmp > TS : # Beep when read value greater than threshold
print ' ****************'
print ' * !! DANGER !! *'
print ' ****************'
print ''
GPIO.output(BEEP, GPIO.HIGH) # (0, means detect danger gas)
time.sleep(0.25)
GPIO.output(BEEP, GPIO.LOW)
time.sleep(0.25)
else :
time.sleep(0.5) # Else delay printing.
def destory():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destory()