This project is a prototype of Homer Simpson's brilliant invention, "The Everything's OK" Alarm. The alarm will sound off every three seconds, letting you know that everything is OK! If gas or fire is detected, things are NOT OK and the alarm will stop.
I got all my sensors from a kit by SunFounder. This kit came with some hardware specific libraries for my ADC Converter and my LCD Screen. Other companies should provide equivalent libraries, or you can try the SunFounder ones which can be found here. If you download the zip, the two you'll need are 'PCF8591.py' and 'LCD1602.py'.
I use FileZilla and an ssh connection to develop from my desktop. To get started create a directory on your pi called 'FlameDetector' or whatever you'd like to call it, and move 'PCF8591.py' and 'LCD1602.py' into that directory. Now lets get started with the Gas Sensor
Raspberry Pi Gas Sensor Setup
To set up the gas and flame sensor I used the PCF8591 ADC converter to generate numerical readings. The Gas sensor and Buzzer were wired as shown below
To test your wiring you can use the code below. If you have a lighter, and press the gas portion down without igniting it you should see a change in the Gas Sensor's reading.
After Running 'GasTest.py' your output might look something like this.
Your values might be way different depending on your sensor but you can clearly see where I released some gas from the lighter. You can also adjust the sensitivity of your sensor using the potentiometer soldered to it.
Flame Sensor Setup
Wiring the flame sensor is very similar to the set up of the gas sensor. They both use the ADC Converter, so I assigned 'AIN0' to the flame sensor and 'AIN1' to the gas sensor.
You can test your flame sensor using the code below. The flame sensor I used was an IR based flame sensor meaning it looks for a specific wavelength of light, which it interprets as being from a flame. I was reading and this can be problematic based on your environment as there may be enough IR light noise that it may think there is a flame constantly, but you can again adjust the sensitivity using the on-board potentiometer.
The output of this test should be similar to the output of 'GasTest.py', if you can get your digital value to change in the presence of a flame than you are good to go. If not try a darker environment.
Raspberry Pi LCD Setup
I used an I2C lcd1602 which uses SCL and SDA. The wiring for this model is quite simple as the individual lines are interfaced already. I used the SunFounder kit mentioned in the "Assets" section above, so I referenced a lot of their documentation. By adding the LCD the all the wiring (arguably the most confusing part) is done.
So Homer's "Everything is okay alarm" goes off every three seconds when everything is okay, and when things aren't okay the alarm stops. Here is the clip. So the code is pretty simple. If I could make one suggestion it'd be that if you are troubleshooting and you know your buzzer works, to unplug it to save yourself some frustration.
#*****SimpsonsAlarm.py*****# from twilio.rest import Client import PCF8591 as ADC import LCD1602 as LCD import RPi.GPIO as GPIO import time import math # Board Setup GPIO.setmode(GPIO.BCM) # Pin Setup gas = 17 buzzer = 18 fire = 27 #*****Initialize Pins*****# def setup(): ADC.setup(0x48) LCD.init(0x27, 1) GPIO.setup(fire, GPIO.IN) GPIO.setup(gas, GPIO.IN) GPIO.setup(buzzer, GPIO.OUT) global Buzz # Assign a global variable to replace GPIO.PWM Buzz = GPIO.PWM(buzzer, 440) #*****Core Functionality*****# def loop(): while True: firetmp = ADC.read(0) # get analog value from flame sensor gaslvl = ADC.read(1) # get analog value from gas sensor lvls = "Gas: " +str(gaslvl)+", Fire: "+str(firetmp) digFire = GPIO.input(fire) # Flame sensor digital output digGas = GPIO.input(gas) # Gas sensor digital output if digFire == 0 or digGas == 0: # fire or gas levels are too high warning = 'DOH! Something is not okay!!\nLevels: '+lvls #Simpson's Reference print(warning) time_end = time.time() + 30 # setup for 30 second while loop while time.time() < time_end: # flash warnings for 30 seconds clearLCD() LCD.write(0, 0, 'DOH! Something ') # write warning on LCD LCD.write(0, 1, 'is not Okay! ') time.sleep(3) clearLCD() LCD.write(0, 0, '*****Danger*****') LCD.write(0, 1, 'Fire:'+str(firetmp)+' Gas:'+str(gaslvl)) time.sleep(3) else: # otherwise print (lvls) # display fire and gas levels in command line LCD.write(0, 0, 'Gas: '+str(gaslvl)) # write levels to lcd LCD.write(0, 1, 'Fire: '+str(firetmp)) time.sleep(3) LCD.write(0, 0, 'Everything Is Ok') LCD.write(0, 1, '****************') Buzz.start(50) # make the buzzer make noise time.sleep(2) Buzz.stop() # stop the buzzer time.sleep(1) clearLCD() #*****Clear Contents of LCD*****# def clearLCD(): LCD.write(0, 0, ' ') LCD.write(0, 1, ' ') time.sleep(.1) #*****Main*****# if __name__ == '__main__': try: setup() loop() except KeyboardInterrupt: pass