{"id":20126,"date":"2016-05-05T09:44:36","date_gmt":"2016-05-05T09:44:36","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=20126"},"modified":"2019-04-02T10:23:32","modified_gmt":"2019-04-02T10:23:32","slug":"guide-for-mq-2-gas-smoke-sensor-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-for-mq-2-gas-smoke-sensor-with-arduino\/","title":{"rendered":"Guide for MQ-2 Gas\/Smoke Sensor with Arduino"},"content":{"rendered":"<p>This guide\u00a0shows how to build a smoke detector that beeps when it detects flammable gas or smoke.<!--more--><\/p>\n<h2>The MQ-2 Gas Sensor<\/h2>\n<p>The MQ-2 smoke sensor is the one in the following figure:<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-20796\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=400%2C308&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"smoke sensor r\" width=\"400\" height=\"308\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?w=400&amp;quality=100&amp;strip=all&amp;ssl=1 400w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=150%2C116&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=300%2C231&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=80%2C62&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=220%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=130%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 130w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=195%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 195w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-r.jpg?resize=309%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 309w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The MQ-2 smoke sensor is sensitive to smoke and to the following flammable gases:<\/p>\n<ul>\n<li>LPG<\/li>\n<li>Butane<\/li>\n<li>Propane<\/li>\n<li>Methane<\/li>\n<li>Alcohol<\/li>\n<li>Hydrogen<\/li>\n<\/ul>\n<p>The resistance of the sensor is different depending on the type of the gas.<\/p>\n<p>The smoke sensor has a built-in potentiometer that allows you to adjust the sensor digital output (D0) threshold. This threshold sets the value above which the digital pin will output a HIGH signal.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-20536\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=648%2C397&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"smoke sensor label\" width=\"648\" height=\"397\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?w=766&amp;quality=100&amp;strip=all&amp;ssl=1 766w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=150%2C92&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=80%2C49&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=220%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=163%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 163w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=245%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 245w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=389%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 389w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?resize=678%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 678w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/p>\n<h2>How does it work?<\/h2>\n<p>The voltage that the sensor outputs changes accordingly to the smoke\/gas level that exists in the atmosphere. The sensor outputs a voltage that is proportional to the concentration of smoke\/gas.<\/p>\n<p>In other words, the relationship between voltage and gas concentration is the following:<\/p>\n<ul>\n<li><strong>The greater<\/strong> the gas concentration, <strong>the greater<\/strong> the output voltage<\/li>\n<li><strong>The lower<\/strong> the gas concentration, <strong>the lower<\/strong> the output voltage<img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-20127\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smokepicture.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"smokepicture\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smokepicture.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smokepicture.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smokepicture.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smokepicture.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/li>\n<\/ul>\n<p>The output can be an analog signal (A0) that can be read with an analog input of the Arduino or a digital output (D0) that can be read with a digital input of the Arduino.<\/p>\n<h2>Pin Wiring<\/h2>\n<p>The MQ-2 sensor has 4 pins.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"283\"><strong>Pin<\/strong><\/td>\n<td width=\"283\"><strong>Wiring to Arduino Uno<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"283\">A0<\/td>\n<td width=\"283\">Analog pins<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">D0<\/td>\n<td width=\"283\">Digital pins<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">GND<\/td>\n<td width=\"283\">GND<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">VCC<\/td>\n<td width=\"283\">5V<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Example: Gas Sensor with\u00a0Arduino<\/h2>\n<p>In this example, you will read the sensor analog output voltage and when the smoke reaches a certain level, it will make sound a buzzer and a red LED will turn on.<\/p>\n<p>When the output voltage is below \u00a0that level, a green LED will be on.<\/p>\n<h3>Parts needed:<\/h3>\n<p>So, for this example, you\u2019ll need:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20261\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08397.jpg?resize=731%2C479&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DSC08397\" width=\"731\" height=\"479\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08397.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/DSC08397.jpg?resize=300%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mq-2-smoke-gas-sensor\/\" target=\"_blank\" rel=\"noopener noreferrer\">1 x MQ-2 gas\u00a0sensor\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Breadboard\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1 x red LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1 x green LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3-24v-piezo-buzzer\/\" target=\"_blank\" rel=\"noopener noreferrer\">1 x buzzer<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">3 x 220\u03a9 resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Schematics<\/h2>\n<p>Follow these schematics to complete the project:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20654\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=447%2C503&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"circuit\" width=\"447\" height=\"503\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?w=447&amp;quality=100&amp;strip=all&amp;ssl=1 447w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=133%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 133w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=267%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 267w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=71%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 71w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=196%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 196w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=89%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 89w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=212%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 212w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=369%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 369w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/circuit-1.png?resize=433%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 433w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/a><\/p>\n<h2>Code<\/h2>\n<p>Upload the following sketch to your Arduino board (feel free to adjust the variable <em>sensorThres<\/em>\u00a0with a different threshold value):<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*******\n \n All the resources for this project:\n http:\/\/randomnerdtutorials.com\/\n\n*******\/\n\nint redLed = 12;\nint greenLed = 11;\nint buzzer = 10;\nint smokeA0 = A5;\n\/\/ Your threshold value\nint sensorThres = 400;\n\nvoid setup() {\n  pinMode(redLed, OUTPUT);\n  pinMode(greenLed, OUTPUT);\n  pinMode(buzzer, OUTPUT);\n  pinMode(smokeA0, INPUT);\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  int analogSensor = analogRead(smokeA0);\n\n  Serial.print(&quot;Pin A0: &quot;);\n  Serial.println(analogSensor);\n  \/\/ Checks if it has reached the threshold value\n  if (analogSensor &gt; sensorThres)\n  {\n    digitalWrite(redLed, HIGH);\n    digitalWrite(greenLed, LOW);\n    tone(buzzer, 1000, 200);\n  }\n  else\n  {\n    digitalWrite(redLed, LOW);\n    digitalWrite(greenLed, HIGH);\n    noTone(buzzer);\n  }\n  delay(100);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/smoke_detector.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Video demonstration<\/h2>\n<p>Watch this quick video demonstration to see this project in action:<\/p>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/_mEUszHqWDg?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>I hope you found this guide\u00a0useful.<\/p>\n<p>Share this post with a friend that also likes electronics!<\/p>\n<p>You can contact me by leaving a comment. If you like this post probably you might like my next ones (<a href=\"https:\/\/randomnerdtutorials.com\/download\" target=\"_blank\" rel=\"noopener noreferrer\">click here to subscribe my blog<\/a>).<\/p>\n<p>Thanks for reading,<\/p>\n<p>-Rui Santos<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide\u00a0shows how to build a smoke detector that beeps when it detects flammable gas or smoke.<\/p>\n","protected":false},"author":1,"featured_media":20536,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,302,245,269,223,264,7,10],"tags":[],"class_list":["post-20126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-0-guide","category-arduino-ide","category-guide-project","category-modules","category-project","category-a-theory-and-blog","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/04\/smoke-sensor-label.png?fit=766%2C469&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/20126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=20126"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/20126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/20536"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=20126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=20126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=20126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}