Arduino LPG Gas Alarm
Background
This project evolved one weekend after "experimenting" with an alcohol sensor and my Arduino UNO. I was surprised at how easy it was to set up. After showing a friend the different types of sensors available we agreed that a gas leak detector would be a worthy project. In Costa Rica we don't have that many accidents due to gas leaks but they do happen. EDIT: I guess they happen more than I thought. Once my housekeeper was cleaning the stove and accidentally left a gas burner open. When I arrived home at night all the gas in the cylinder was gone. Luckily our houses in Costa Rica are well ventilated and there was no explosion. The idea of this alarm is to beep and flash a LED when butane/propane (LPG) is detected.
The sensor
The LPG Gas Sensor MQ-6 is very easy to connect to your Arduino. Here are instructions how to wire it up and test with some quick code. Basically, there are two parts to the sensor. The first circuit is a heating coil that connects to 5VDC. This uses about 150mA of current and keeps the sensor element warm. The second circuit is a variable resistor that varies according to the detection of butane/propane. So all we have to do it read the voltage using one of the analog pins on the Arduino and alarm when the voltage gets to a specific set point.
The schematic
For this circuit I wanted to keep the costs down. Using an Arduino UNO in any project adds $30 so I decided to try my luck using the ATmega328p microcontroller directly. Here is the schematic for the circuit. Note that I am not using a crystal for the microcontroller but instead am using the internal clock set to 8MHz. The schematic says ATMEGA168 but it really is the 328. The Fritzing part says 168.
Circuit Description
Up in the upper left corner we have a basic DC regulator circuit using an LM7805 purchased at RadioShack. The input should be between 7 and 12 VDC, however I recommend not going over 9VDC as the regulator heats up quite a bit and will need a heatsink. The output of the regulator gives us 5VDC.
In the upper right hand side of the schematic we see the stylized representation of the sensor circuit. The heater element connects directly to 5VDC and GND.
The sensing element connects to 5VDC and to a 10K resistor to GND. We sniff the voltage between these two resistors. The more gas is detected, the more current flows through the sensor and this creates more voltage across the 10K resistor. We read this voltage in on the Analog0 pin (physical pin 23 on the microcontroller).
The LED is wired via a pull-up resistor to 5VDC on Digital pin13. The piezoelectric buzzer is on Digital pin8.
A 6 pin header connects to the RX and TX pins on the microcontroller. I used this USB to serial converter to program the microcontroller although one could use the Arduino UNO to do the same thing.
The Code
The Arduino IDE code attached below does the following:
Turns on the power and waits for 30 seconds for the coil to heat up. The readings can vary quite a bit when the sensor is cold. The LED flashes during this "boot up" period.
After 30 seconds the LED goes steady and we start to read in the voltage. 3 samples of the voltage are taken and averaged out. This process cycles as fast as it can.
If the sensor reading goes above the setpoint (500) the alarm is triggered. The LED starts to flash and the piezoelectric buzzer starts to beep.
Using the ATMega328p w/ internal clock
With a slight modification, I used these instructions to reburn the bootloader in my ATMega328p using my Arduino UNO and set it up so that it runs at 8MHz using the internal clock. At the bottom of the linked page you will find instructions for connecting the Arduino UNO to the microcontroller. Because my ATMega328p already had a bootloader installed and the fuses were set for 16MHz with an external crystal I couldn't burn in the bootloader. Amazingly enough I temporarily connected a 20MHz crystal that I had lying around to pins 10 and 11 (with no other circuitry!!) and everything worked. The bootloader and fuses burned in and my ATMega328p fired up without the need for any external crystal!
Fabrication
The circuit was built on standard proto board and installed in a PVC plastic electrical box. Instead of worrying about standoffs I just used double sided foam tape to secure the board to the box. For a more professional finish I printed out a front panel (below is the LibreOffice Draw document) and laminated it on both sides with plastic adhesive used to cover kids notebooks in Costa Rica. I then created a black mask version with a hole so that the the LED shines only through the white paper indicator and doesn't bleed out around the edges. It turned out quite nice.
Things to Do
Currently the sensor only alarms in the presence of gas. If the gas dissipates then the alarm turns off. Perhaps it would make more sense to have the alarm turn on and then require a reset button to turn it off. Then one would know if a leak had taken place during the day.
Also, an 8-pin ATTiny85 microcontroller would be perfect for this project instead of the ATMega328p due to how few pins we are using.