A simple Arduino gas detector example.


So you may be wondering why I tried this. Well, I wrote some code to check for dangerous gases released when charging lead acid batteries as a safety concern at work. This came about because we charge lots of sealed lead acid batteries which usually charge without any problems but on one occasion one battery had failed causing the room to fill with a rotten egg stink. This is not just a bad smell; in fact it was Hydrogen Sulphide we were smelling which is very toxic in large concentrations. This definitely was bad; the entire building stunk on both floors and it took all day to get rid of the stink with windows open. In the middle of winter I might add. Faulty batteries can also release hydrogen which is explosive if in a sealed room and with a high enough concentration although modern SLA batteries do not give off nowhere near as much as flooded types. However H2S gas is bad for your health in any concentration and is produced by all lead acid batteries when misused or overcharged. Such gases are also found in rubbish dumps and sewers and other areas with decomposing organic matter in enclosed spaces.

So I wrote code for a sensor that would detect these gases and display the levels on a LCD display triggering visual and audible alarms if thresholds were met. The software also triggered a relay which could turn off the chargers and turn on an extractor fan. Often batteries were left on charge overnight and automatic gas detection and prevention was a must. I also planned to add a remote monitoring facility as well.

Anyway for reasons I cannot explain the project was cancelled so I simplified my code and added a methane sensor to the mix as well. You guessed it; there is another use for my code…

The silly bit

An Arduino fart detector of course!

So, I needed to find out what gases are present in a fart. Google revealed that human farts typically contain mostly hydrogen followed by methane and finally trace elements of hydrogen sulphide. This of course depends on what the said farter has been eating and these gas levels can vary. Want to know if breathing in farts really is bad for you? I’d imagine it is and this bit of code will prove that point or not. Bear in mind both hydrogen and methane burn and hydrogen sulphide is toxic!

Want to know if breathing in other people’s rancid turd fumes in communal toilets such as work places is also bad for you? Do you want to prove that holding your breath when following that co-worker who always stinks up the place into the toilet is a wise decision? Well you can find out by using an Arduino and a handful of MQ series sensors!

I planned to use the MQ-4 (Methane), MQ-8 (Hydrogen) and a MQ-136 (Hydrogen Sulphide) series of sensors. By far the most expensive is the MQ-136 due to the chemicals used for detection; the others are only a few pounds. All these sensors run off 5V and have an analog output which varies in proportion to the gas level. They have an internal heater hence draw quite a bit of current so you will need to use a decent 5V supply. Do not use the built in regulator on the Arduino as it cannot supply the current required. The sensors should be housed in an enclosure with a slow speed fan to draw in air but not too much as the fan could blow fresh air onto the sensors. Some experimentation with sensor positioning and / or fan speeds would be required here as some gases are lighter than / heavier than air. A semi-permanent installation in or near a bathroom to detect those turd fumes may be a good idea. The sensors require 2-3 mins to warm up and really only become accurate after being on for an initial burn in period of 24 hours.

The code

Anyway onto the code:-

I used the .ino file to avoid HTML messing up the code so here is the file for download.

Further improvements and other serious use examples

You could modify it to display the actual levels on a 20×4 LCD rather than LED’s indicating if a threshold has been met or not. It’s simple code which could be vastly improved. If my gas detector project at work is resurrected I would look at using this code and add facilities to turn off chargers and turn on fans etc.

Not to be used for any serious monitoring, use this code for entertainment (fart sensor) or as an example to make your own gas sensing equipment. The code is released without any warranty and can be modified for your own use as you see fit. Just remember to credit me if you wish to re-distribute it.

Note this project is incomplete and hasn’t been fully assembled. The code has been tested to work OK; you will need sensors and suitable load resistors or pre-made boards typically available on ebay to use this. You can prove the code works by varying the voltage on the analog input pins. All data is dumped to the serial port every 10 seconds.

Hope this is of use to someone for detecting hazardous gases when charging batteries or as a fart sensor. Other examples for use are monitoring the typical hazardous gases (Methane, Hydrogen & Hydrogen Sulphide) produced in rubbish dumps, in sewers and slurry pits amongst others. It really needs an LCD display to display the levels rather than threshold indicators but this depends on how simple you want it.

As I mentioned earlier if the project is resurrected I will post any improved code here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.