{"id":96,"date":"2016-02-06T18:19:43","date_gmt":"2016-02-06T18:19:43","guid":{"rendered":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=96"},"modified":"2018-12-01T15:43:25","modified_gmt":"2018-12-01T15:43:25","slug":"detecting-lpg-propane-butane","status":"publish","type":"post","link":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=96","title":{"rendered":"Detecting LPG, Propane &#038; Butane"},"content":{"rendered":"<p>As I previously mentioned, I got a number of gas sensors. The intention is to use one to build a gas leak detector for a neighbour who has no sense of smell and runs a burger van. After reading the datasheets for a number of possible sensors\u00a0including\u00a0the <a href=\"https:\/\/www.seeedstudio.com\/depot\/datasheet\/MQ-2.pdf\" target=\"_blank\" rel=\"noopener\">MQ-2<\/a>, <a href=\"https:\/\/www.seeedstudio.com\/depot\/datasheet\/MQ-5.pdf\" target=\"_blank\" rel=\"noopener\">MQ-5<\/a> and <a href=\"https:\/\/www.sparkfun.com\/datasheets\/Sensors\/Biometric\/MQ-6.pdf\" target=\"_blank\" rel=\"noopener\">MQ-6<\/a>, I decided to use the MQ-6 as it seemed best suited. The MQ-2 is sensitive to smoke, which rules it out and the MQ-6 appears to be more sensitive to LPG than the MQ-5, judging by the datasheets. The sensors I have come on breakout boards. My MQ-6 board is powered from 5v and\u00a0has a digital output with the threshold set by a potentiometer on the board in addition to the analogue output. The on board comparator (I&#8217;ve not looked into what form it takes) lights an LED when the gas concentration exceeds the level set by the potentiometer, but be aware that a digital LOW corresponds to a positive identification of gas, and a digital HIGH corresponds to the concentration being lower than the set threshold. The datasheet states that the sensor consumes &lt;750mW, mine seems to be using something\u00a0between\u00a0400 and 500mW.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-109 size-medium\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_164330-300x225.jpg\" alt=\"MQ-6 Gas Sensor\" width=\"300\" height=\"225\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_164330-300x225.jpg 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_164330-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>I have been using an Arduino Pro Micro\u00a0board because they are convenient for prototyping in breadboard as they have a built in USB port and a DIL layout. I may switch to a ATTiny85 or a ATMega328 in the future. The Pro Micro\u00a0is built around the ATMega32u4 chip and can be bought cheaply from alibaba, or almost as cheaply using paypal from banggood.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-104 aligncenter\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_101439-300x225.jpg\" alt=\"Arduino Pro Micro\" width=\"300\" height=\"225\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_101439-300x225.jpg 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_101439-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>After writing some comments in an empty sketch to give me the structure of what I wanted to do, I wired up the sensor, along with an LED (unfortunately the Pro Micro only has LEDs for power, TX and RX) and a push-to-make switch to act as an &#8220;action&#8221; button. I later added a 2N7000 MOSFET, which I am using to switch power to the sensor &#8211; this is being used to reduce the power consumption after 10 minutes and reduce battery usage should someone forget to turn the device off!<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-108 aligncenter\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_144747-300x225.jpg\" alt=\"20160206_144747\" width=\"300\" height=\"225\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_144747-300x225.jpg 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_144747-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Quick diagram (ignore the colours in the photo, they&#8217;re misleading) :<\/p>\n<p><img loading=\"lazy\" class=\"size-medium wp-image-114 aligncenter\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-06-181443-e1454782644753-300x280.png\" alt=\"Screenshot from 2016-02-06 18:14:43\" width=\"300\" height=\"280\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-06-181443-e1454782644753-300x280.png 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-06-181443-e1454782644753.png 657w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The sensor seems to need bedding in and so the values I&#8217;ve used for alarm thresholds are only temporary. Once it stabilises (it is referred to as a &#8220;burning in&#8221; period elsewhere), I&#8217;ll re-visit the values.<\/p>\n<p>The current code is as follows :<\/p>\n<pre class=\"lang:arduino decode:true\" title=\"LPG Gas Detector\">\/\/ LPG Gas Detector\r\n\/\/ \r\n\/\/ Using the MQ-6, although with changes to detection values,\r\n\/\/ you could easily use the MQ-5, or perhaps the MQ-2.\r\n\/\/ No responsibility is taken for failure to detect gas.\r\n\/\/ Samplerates are approximate for simplicity.\r\n\/\/ 6th Feb 2016 version 0.1\r\n\r\n\r\n\r\nint heaterPin = 9; \/\/ connect a MOSFET or transistor to this pin to\r\n                      \/\/ drive the Heater\r\nint DIn = 10; \/\/ connect the digital trigger from the sensor\r\n\r\nint AIn = A0; \/\/ connect the analogue input from the sensor\r\n\r\nint LED1 = 2; \/\/ LED\r\n\r\nint action = 7; \/\/ action button\r\n\r\nunsigned long stopTime;\r\nbyte heaterOffMinutes = 10;\/\/ time before auto power down heater\r\nbyte heaterDelayMinutes = 3;\/\/ time to wait for heater to start\r\n\/\/byte heaterDelayMinutes = 1;\/\/ time to wait for heater to start\r\nbyte samplesPerMinute = 30;\/\/ number of samples in a minute\r\nbyte reheatDelayMinutes = 1;\/\/ Time to reheat after low power mode. Should really be 3 minutes.\r\n\r\nboolean debug = true;\r\n\r\nint lowAlarm = 250;\r\nint medAlarm = 375;\r\nint highAlarm = 500;\r\n\r\nvoid setup() {\r\n  unsigned long heaterOff = heaterOffMinutes*60L*1000L;\/\/ min*seconds*ms\r\n  int heaterDelay = heaterDelayMinutes*60L;\/\/ min*seconds\r\n\r\n  if(debug){\r\n    Serial.begin(115200);\r\n  }\r\n  \r\n  delay(3000);\r\n\r\n  if(debug){\r\n    Serial.println(\"Setting up pins...\");\r\n  }\r\n  \r\n  pinMode(DIn,INPUT);\r\n  pinMode(AIn, INPUT);\r\n  pinMode(LED1, OUTPUT);\r\n  pinMode(heaterPin, OUTPUT);\r\n  pinMode(action, INPUT);\r\n\r\n  digitalWrite(LED1, LOW);\r\n\r\n  if(debug){\r\n    Serial.println(\"Turning on heater...\");\r\n  }\r\n  \r\n  \/\/ Turn on heater\r\n  digitalWrite(heaterPin, HIGH);\r\n\r\n  if(debug){\r\n    Serial.println(\"Waiting for heater to warm up...\");\r\n  }\r\n  \r\n  \/\/ Wait Three minutes before giving readings\r\n  heat(heaterDelay);\/\/heaterDelay in seconds\r\n\r\n  \/\/ Record target stop time for auto heater off (save the battery!)\r\n  stopTime = millis()+heaterOff;\r\n  if(debug){\r\n    Serial.println(\"Heater will stop at : \" + String(stopTime,DEC));\r\n  }\r\n  \r\n}\r\n\r\nvoid loop() {\r\n  \/\/ Go to sleep after 10 minutes? At least turn off heater.\r\n  if(millis() &lt; stopTime){\r\n      if(debug){\r\n        Serial.println(String((stopTime-millis())\/1000, DEC) + \" seconds until sleep. Taking Reading...\");\r\n      }\r\n      int AVal = analogRead(AIn);\r\n      boolean DVal = !digitalRead(DIn);\/\/ 0 is high\r\n      if(debug){\r\n        Serial.println(\"Digital is : \" + String(DVal, DEC));\r\n      }\r\n      if(debug){\r\n        Serial.println(\"Analogue is : \" + String(AVal, DEC));\r\n      }\r\n      if(AVal &gt; lowAlarm){\r\n        if(AVal &gt; medAlarm){\r\n          if(AVal &gt; highAlarm){\r\n            \/\/HighAlarm\r\n            digitalWrite(LED1, HIGH);\r\n            if(debug){\r\n              Serial.println(\"Alarm! High\");\r\n            }\r\n          }else{\r\n            \/\/MedAlarm\r\n            if(debug){\r\n              Serial.println(\"Alarm! Medium\");\r\n            }\r\n            digitalWrite(LED1, LOW);\r\n            delay(10);\r\n            digitalWrite(LED1, HIGH);\r\n            delay(50);\r\n            digitalWrite(LED1, LOW);\r\n            delay(200);\r\n            digitalWrite(LED1, HIGH);\r\n            delay(50);\r\n            digitalWrite(LED1, LOW);\r\n            delay(200);\r\n            digitalWrite(LED1, HIGH);\r\n            delay(50);\r\n            digitalWrite(LED1, LOW);\r\n          }\r\n        }else{\r\n          \/\/LowAlarm\r\n          if(debug){\r\n              Serial.println(\"Alarm! Low\");\r\n            }\r\n          digitalWrite(LED1, LOW);\r\n          delay(10);\r\n          digitalWrite(LED1, HIGH);\r\n          delay(50);\r\n          digitalWrite(LED1, LOW);\r\n          delay(200);\r\n          digitalWrite(LED1, HIGH);\r\n          delay(50);\r\n          digitalWrite(LED1, LOW);\r\n        }\r\n      }else{\r\n        \/\/NoAlarm\r\n        digitalWrite(LED1, LOW);\r\n      }\r\n\r\n  \r\n  \r\n  }\/\/ end of stopTime if\r\n  else{\r\n    if(debug){\r\n      Serial.println(\"Low Power mode\");\r\n    }\r\n    \/\/sleep mode instead of delay?\r\n    digitalWrite(heaterPin, LOW);\r\n    int y = 0;\r\n    for(int x=0; x&lt;30; x++){\r\n      if(digitalRead(action)){\r\n        y++;\r\n      }else{\r\n        y=0;\r\n      }\r\n      if(y&gt;2){\r\n        \/\/ Record target stop time for auto heater off (save the battery!)\r\n        digitalWrite(heaterPin, HIGH);\r\n        while(digitalRead(action)){\r\n          \/\/do nothing\r\n          delay(100);\r\n        }\r\n        heat(reheatDelayMinutes*60L);\r\n        stopTime = millis()+(heaterOffMinutes*60L*1000L);\r\n        x = 30;\r\n        if(debug){\r\n          Serial.println(\"Heater will stop at : \" + String(stopTime,DEC));\r\n        }\r\n      }\r\n      delay(2000);\r\n    }\r\n    \r\n  }\r\n  delay(60000L\/samplesPerMinute);\r\n\r\n  digitalWrite(LED1, LOW);\r\n  delay(10);\r\n  digitalWrite(LED1, HIGH);\r\n  delay(10);\r\n  digitalWrite(LED1, LOW);\r\n}\r\n\r\nvoid heat(int heaterDelay){ \/\/ heaterDelay in seconds\r\n  for(int x = 0; x&lt; heaterDelay; x++){\r\n    boolean btn = false;\r\n    if(debug){\r\n      Serial.println(\"Readings start in... \" + String(heaterDelay-x,DEC)+ \" seconds\");\r\n    }\r\n    \/\/Serial.println(String(x, DEC) + \" of \" + String(heaterDelay, DEC));\r\n    digitalWrite(LED1, !digitalRead(LED1));\r\n    if(digitalRead(action)){\r\n      btn = true;\r\n    }\r\n    delay(1000);\r\n    if(digitalRead(action) &amp;&amp; btn){\r\n      x = heaterDelay;\r\n      if(debug){\r\n        Serial.println(\"Warm up skipped...\");\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>If you spot any errors, please drop me a message!<\/p>\n<p>As always, the most difficult part of this project seems to be identifying a suitable power source and box. I&#8217;ve been using my USB meter to gauge how much energy the circuit uses &#8211; I estimate that I \u00a0can run it with the heater on for 10 hours off four AA batteries. I&#8217;m struggling to identify a box that would easily allow the end user to change the battery without being forced to use a PP6 battery, which would be a waste of 4v. I&#8217;m not sure how accurate my little USB meter is, but it is very useful as I power a lot of my projects off computer USB ports or my USB battery packs!<\/p>\n<div id=\"attachment_107\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-107\" loading=\"lazy\" class=\"wp-image-107 size-medium\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_141459-300x225.jpg\" alt=\"USB Meter\" width=\"300\" height=\"225\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_141459-300x225.jpg 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2016\/02\/20160206_141459-1024x768.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-107\" class=\"wp-caption-text\">My USB Voltage and Current Meter.<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As I previously mentioned, I got a number of gas sensors. The intention is to use one to build a gas leak detector for a neighbour who has no sense of smell and runs a burger van. After reading the datasheets for a number of possible sensors\u00a0including\u00a0the MQ-2, MQ-5 and MQ-6, I decided to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[24,13,12],"tags":[28,27,25,26],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7g9vY-1y","_links":{"self":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/96"}],"collection":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":13,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":358,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions\/358"}],"wp:attachment":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}