Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-----------------------------------------------------
  3. #
  4. # This is a program for MQ-2 Gas Sensor Module.
  5. # It could detect danger gas and smokes.
  6. #
  7. # This program depend on ADC0832 ADC chip. Follow
  8. # the instruction book to connect the module and
  9. # ADC0832 to your Raspberry Pi.
  10. #
  11. #-----------------------------------------------------
  12.  
  13. import RPi.GPIO as GPIO
  14. import ADC0832
  15. import time
  16.  
  17. BEEP = 16 # Set buzzer pin
  18. TS = 100 # You can set the Threshold by yourself (0-255)
  19.  
  20. def setup():
  21. GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
  22. GPIO.setup(BEEP, GPIO.OUT) # Set pins' mode is output
  23. ADC0832.setup() # Setup ADC0832
  24.  
  25. def loop():
  26. while True:
  27. tmp = ADC0832.getResult() # Get analog value from ADC0832
  28. print tmp # Print analog value
  29. if tmp > TS : # Beep when read value greater than threshold
  30. print ' ****************'
  31. print ' * !! DANGER !! *'
  32. print ' ****************'
  33. print ''
  34.  
  35. GPIO.output(BEEP, GPIO.HIGH) # (0, means detect danger gas)
  36. time.sleep(0.25)
  37. GPIO.output(BEEP, GPIO.LOW)
  38. time.sleep(0.25)
  39. else :
  40. time.sleep(0.5) # Else delay printing.
  41.  
  42. def destory():
  43. GPIO.cleanup() # Release resource
  44.  
  45. if __name__ == '__main__': # Program start from here
  46. setup()
  47. try:
  48. loop()
  49. except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
  50. destory()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement