{"id":1894,"date":"2024-06-28T08:29:41","date_gmt":"2024-06-28T08:29:41","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1894"},"modified":"2024-08-11T17:19:22","modified_gmt":"2024-08-11T17:19:22","slug":"smoke-sensor-mq3-sensor","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1894","title":{"rendered":"Smoke Sensor MQ3 Sensor"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">Flammable Gas &amp; Smoke Sensor- It is suitable for detecting the H<sub>2<\/sub>, LPG, CH<sub>4<\/sub>, CO, Alcohol or Propane. MQ3 sensor can sense LPG, CO, Smoke etc. For MQ3 sensor if the concentration is high the resistance decrease, if the concentration is low the resistance increase. &nbsp;&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">D<sub>0 <\/sub>: Indicates the presence of combined gases D<sub>0 <\/sub>becomes high, when the gas concentration exceeds the threshold value (as set the potentiometer) and low otherwise.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">A<sub>0<\/sub> : Produces an analog output proportional to gas concentration so a higher concentrations results in higher voltage and a lower concentration result in lower voltage<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"454\" height=\"219\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor.jpg\" alt=\"\" class=\"wp-image-1897\" style=\"width:371px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor.jpg 454w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-300x145.jpg 300w\" sizes=\"(max-width: 454px) 100vw, 454px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">For pure air the resistance factor is 9.83. First step is to calculate the resistance in air, suppose it is R<sub>0<\/sub>. We use ATtiny85 for full program. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"839\" height=\"46\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-2.jpg\" alt=\"\" class=\"wp-image-1902\" style=\"width:456px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-2.jpg 839w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-2-300x16.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-4-2-768x42.jpg 768w\" sizes=\"(max-width: 839px) 100vw, 839px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define RO_CLEAN_AIR_FACTOR   9.83\nvolatile uint16_t adc_value;\nfloat R0,R0_OLD,Rs;\nvoid R0_resistancs(void)\n{\n\tadc_value=read_adc(2);\n\tR0_OLD=MQResistanceCalculation(adc_value);\n\tR0=R0_OLD\/RO_CLEAN_AIR_FACTOR;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">What is the resistance? Here for pure air R = 1, but for normal air the resistance is R<sub>0<\/sub> = (1024\/ADC Value)-1. So the function C:&gt; float MQResistanceCalculation(uint16_t raw_adc)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define RL_VALUE              1 \/\/define the load resistance on the board, in kilo ohms\n#define ADC_MAX 1023\nfloat MQResistanceCalculation(uint16_t raw_adc)\n{\n\treturn (((float)RL_VALUE*(ADC_MAX-raw_adc)\/raw_adc));\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">Since the sensor need some time to initialize, so wait in a loop for calculating air resistance. Now look at the datasheet of the MQ3 sensor-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"628\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-datasheet-1024x628.jpg\" alt=\"\" class=\"wp-image-1907\" style=\"width:800px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-datasheet-1024x628.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-datasheet-300x184.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-datasheet-768x471.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/MQ3-sensor-datasheet.jpg 1140w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">From curve the ration R<sub>s<\/sub>\/R<sub>0<\/sub> indicates the presence of LPG, CO or Smoke. If we put the R<sub>s<\/sub>\/R<sub>0 <\/sub>value in the graph then we get the value of LPG, CO or Smoke in PPM. From the curve the value of slop, transient and constant value as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nfloat LPGCurve&#x5B;3]  =  {1.413,0.21,-0.473}; \/\/{2.3,0.21,-0.47};\nfloat COCurve&#x5B;3]  =   {1.413,0.72,-0.34};  \/\/{2.3,0.72,-0.34};\nfloat SmokeCurve&#x5B;3] = {2.3,0.53,-0.44};  \/\/{2.3,0.53,-0.44};\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The desire PPM is &#8211; <img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"20\" class=\"wp-image-1911\" style=\"width: 280px;\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-1.jpg\" alt=\"\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-1.jpg 543w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-5-1-300x22.jpg 300w\" sizes=\"(max-width: 280px) 100vw, 280px\" \/><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nint MQGetPercentage(float rs_ro_ratio, float *pcurve)\n{\n\tpercentage=(pow(10,(((log10(rs_ro_ratio)-pcurve&#x5B;1])\/pcurve&#x5B;2]) + pcurve&#x5B;0])));\n\treturn percentage;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">In order to read LPG, CO or Smoke <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nfloat MQGetGasPercentage(float rs_ro_ratio, int gas_id)\n{\n\tif ( gas_id == GAS_LPG ) {\n\t\treturn MQGetPercentage(rs_ro_ratio,LPGCurve);\n\t\t} else if ( gas_id == GAS_CO ) {\n\t\treturn MQGetPercentage(rs_ro_ratio,COCurve);\n\t\t} else if ( gas_id == GAS_SMOKE ) {\n\t\treturn MQGetPercentage(rs_ro_ratio,SmokeCurve);\n\t}\n\treturn 0;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#252525\">So the full function for calculating the PPM of LPG, CO or Smoke in air is<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nextern volatile float lpg,co,smoke; \/\/ extern variable\nvoid air_ppm(void)\n{\n\tadc_value=read_adc(2);\n\tRs=MQResistanceCalculation(adc_value);\n\tRs=Rs\/R0;\n\tlpg = MQGetGasPercentage(Rs,GAS_LPG);\n\tco = MQGetGasPercentage(Rs,GAS_CO);\n\tsmoke = MQGetGasPercentage(Rs,GAS_SMOKE);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-10 wp-block-paragraph\" style=\"color:#5c5c5c\">We have the function ready, now let make a program that buzz when the smoke or lpg or co is present. According to datasheet of ATtiny85 to generate 20KHz f<sub>PWM<\/sub> at OC1A pin is-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid Buzzer_init(void)\n{\n\tDDRB|=(1&lt;&lt;DDB1);\n\tTCCR1|=(1&lt;&lt;COM1A1)|(1&lt;&lt;PWM1A); \/\/only OC1B is connected\n\tTCCR1|=(1&lt;&lt;CS12)|(1&lt;&lt;CS10); \/\/FP_PWM=F_CPU\/16\n\tOCR1C=199; \/\/Top value with F_PWM=20KHz\n\tBuzzer_off();\n}\nvoid Buzzer_on(void)\n{\n\tTCCR1|=(1&lt;&lt;PWM1A);\n\tOCR1A=100; \/\/50% duty cycle;\n}\nvoid Buzzer_off(void)\n{\n\tTCCR1&amp;=~(1&lt;&lt;PWM1A); \/\/PWM Mode disable;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">We use extern volatile float lpg,co,smoke; -&gt; global variable to display data any file. You can display the data in LCD module, but in this tutorial we chose ATtiny85 which doesn\u2019t have enough pin for controlling LCD. We use OLED for display the data. Connect all the elements according to the connection diagram.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-12 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s look at the main program that will help you in building your own logic-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/********************************************************************\n******      D0- Digital Value 1(High Gas Concentration)      *********\n******                        0(Low gas Concentration)       *********\n******      A0- Analog Value of gas presence                 *********\n******      D0 --- N\/C      &amp;       A0-ADC Channel 0         *********\n*********************************************************************\/\n#include &lt;avr\/io.h&gt;\n#include &lt;util\/delay.h&gt;\n#include &lt;stdio.h&gt;\n#include &quot;smoke.h&quot;\nvolatile char display_value&#x5B;8]=&quot;text&quot;;\nint main(void)\n{\n    \/* Initialize OLED Display *\/\n    adc_init();\n    init_OLED();\n    reset_display();\n    clear_display();\n    Buzzer_init();\n    \/*  Main program with logo and design *\/\n    OLCD_write_string(0,0,&quot;MQ3 Sensor Init&quot;);\n\tfor(uint16_t i=6500;i&gt;=1;i--)\n\t{\n\t\tR0_resistancs();\n\t\tsprintf(display_value,&quot;Time:%d   &quot;,i);\n\t\tOLCD_write_string(1,0,display_value);\n\t\t_delay_ms(1000);\n\t}\n\tclear_display();\n\t_delay_ms(10);\n\tOLCD_write_string(0,0,&quot;Air Materials  &quot;);\n    while(1)\n    {\n\t\tair_ppm();\n\t    sprintf(display_value,&quot;LPG:%d PPM    &quot;,(int)lpg);\n\t    OLCD_write_string(1,0,display_value);\n\t\tsprintf(display_value,&quot;CO:%d PPM     &quot;,(int)co);\n\t\tOLCD_write_string(2,0,display_value);\n\t\tsprintf(display_value,&quot;Smoke:%d PPM   &quot;,(int)smoke);\n\t\tOLCD_write_string(3,0,display_value);\n\t    (((int)lpg&gt;20)|((int)co&gt;20)|((int)smoke&gt;20))? Buzzer_on():Buzzer_off();\n\t    _delay_ms(1000);\n    }\n    return 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/MQ3-Sensor.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-13 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/MQ3-Sensor.rar\">MQ3 Sensor.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Flammable Gas &amp; Smoke Sensor- It is suitable for detecting the H2, LPG, CH4, CO, Alcohol or Propane. MQ3 sensor can sense LPG, CO, Smoke etc. For MQ3 sensor if the concentration is high the resistance decrease, if the concentration is low the resistance increase. &nbsp;&nbsp; D0 : Indicates the presence of combined gases D0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1894","post","type-post","status-publish","format-standard","hentry","category-adc"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1894"}],"version-history":[{"count":18,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions"}],"predecessor-version":[{"id":2627,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions\/2627"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}