Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. MQ2 Sensor w/AirQuality Sketch

MQ2 Sensor w/AirQuality Sketch

Scheduled Pinned Locked Moved Hardware
15 Posts 5 Posters 12.4k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gigaguy
    wrote on last edited by
    #1

    I bought a gas sensor MQ2 from ebay. trying to figure out how to connect it, and use the Air Quality Sketch.

    http://www.ebay.com/itm/371194045196?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

    Ive got the analog connection on A0, not sure if i need to connect the digital pin too or not.

    I am getting a reading of 0ppm for all the measurements in the sketch.

    Any ideas? TIA.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      robosensor
      wrote on last edited by
      #2

      I'm using similar sensors, this test sketch works fine for me, but I'm using raw readings from this sensors.
      8 sensors connected to A0-A7 pins on arduino nano directly to analog output pin on each sensor board.

      #include <SPI.h>
      #include <MySensor.h>  
      
      unsigned long SLEEP_TIME = 10000; // Sleep time between reports (in milliseconds)
      
      #define NODE_ID										4
      
      MySensor gw;
      MyMessage msg(0, V_VAR1);
      
      int lastLightLevel[8] = {0};
      unsigned long lastRefreshTime = 0;
      
      void setup()
      {
      	pinMode(13, OUTPUT);
      	// warm up sensors (3 minutes)
      	for (int i = 0; i < 180; i++)
      	{
      		digitalWrite(13, HIGH);
      		delay(250);
      		digitalWrite(13, LOW);
      		delay(250);
      		digitalWrite(13, HIGH);
      		delay(250);
      		digitalWrite(13, LOW);
      		delay(250);
      	}
      	gw.begin(NULL, NODE_ID);
      
      	gw.sendSketchInfo("GasSensors", "1.5");
      
      	gw.present(1, S_AIR_QUALITY, false, "MQ-2");
      	gw.present(2, S_AIR_QUALITY, false, "MQ-3");
      	gw.present(3, S_AIR_QUALITY, false, "MQ-4");
      	gw.present(4, S_AIR_QUALITY, false, "MQ-6");
      	gw.present(5, S_AIR_QUALITY, false, "MQ-7");
      	gw.present(6, S_AIR_QUALITY, false, "MQ-8");
      	gw.present(7, S_AIR_QUALITY, false, "MQ-9");
      	gw.present(8, S_AIR_QUALITY, false, "MQ-135");
      }
      
      void loop()
      {
      	gw.wait(100);
        
      	boolean bNeedRefresh = (millis() - lastRefreshTime) > SLEEP_TIME;
      	if (bNeedRefresh)
      	{
      		lastRefreshTime = millis();
      	}
      	for (int i = 0; i < 8; i++)
      	{
      		int lightLevel = 0;
      		for (int j = 0; j < 32; j++)
      			lightLevel += analogRead(i);
      		lightLevel = (lightLevel / 32);
      
      		int nDifference = lastLightLevel[i] - lightLevel;
      		if ((abs(nDifference) > 5) || bNeedRefresh)
      		{
      			gw.send(msg.setSensor(i + 1).set(lightLevel));
      			lastLightLevel[i] = lightLevel;
      		}
      	}
      }
      

      Raw ADC readings are 20-80 (differs from sensor to sensor) for fresh outdoor air.

      G 1 Reply Last reply
      1
      • E Offline
        E Offline
        epierre
        Hero Member
        wrote on last edited by
        #3

        you can find the curves for the MQ2 here: https://github.com/empierre/arduino/blob/master/AirQuality-Multiple_Gas_Sensor1_4.ino

        Check the thread about gas sensors: http://forum.mysensors.org/topic/147/air-quality-sensor/83

        Regarding having 6 electolithic sensors on a nano, check ths out about powering them :

        Arduino Mega is given at : DC Current per I/O Pin 40 mA, so at 5V I have P=5*0,04=0,2w

        When I list all the gases sensors I have this:

        MQ2          5V     0,8 w
        MQ6          5V     0,75w
        MQ131      6V     1,1 w
        MQ135      5V     0,8 w
        MQ138      5V     0,85w
        MS2610    2.35V  0,09w
        TGS2600  5V     0,21w
        TGS2602  5V     0,28w
        HCHO       5V     NA
        

        z-wave - Vera -&gt; Domoticz
        rfx - Domoticz &lt;- MyDomoAtHome &lt;- Imperihome
        mysensors -&gt; mysensors-gw -&gt; Domoticz

        1 Reply Last reply
        0
        • R robosensor

          I'm using similar sensors, this test sketch works fine for me, but I'm using raw readings from this sensors.
          8 sensors connected to A0-A7 pins on arduino nano directly to analog output pin on each sensor board.

          #include <SPI.h>
          #include <MySensor.h>  
          
          unsigned long SLEEP_TIME = 10000; // Sleep time between reports (in milliseconds)
          
          #define NODE_ID										4
          
          MySensor gw;
          MyMessage msg(0, V_VAR1);
          
          int lastLightLevel[8] = {0};
          unsigned long lastRefreshTime = 0;
          
          void setup()
          {
          	pinMode(13, OUTPUT);
          	// warm up sensors (3 minutes)
          	for (int i = 0; i < 180; i++)
          	{
          		digitalWrite(13, HIGH);
          		delay(250);
          		digitalWrite(13, LOW);
          		delay(250);
          		digitalWrite(13, HIGH);
          		delay(250);
          		digitalWrite(13, LOW);
          		delay(250);
          	}
          	gw.begin(NULL, NODE_ID);
          
          	gw.sendSketchInfo("GasSensors", "1.5");
          
          	gw.present(1, S_AIR_QUALITY, false, "MQ-2");
          	gw.present(2, S_AIR_QUALITY, false, "MQ-3");
          	gw.present(3, S_AIR_QUALITY, false, "MQ-4");
          	gw.present(4, S_AIR_QUALITY, false, "MQ-6");
          	gw.present(5, S_AIR_QUALITY, false, "MQ-7");
          	gw.present(6, S_AIR_QUALITY, false, "MQ-8");
          	gw.present(7, S_AIR_QUALITY, false, "MQ-9");
          	gw.present(8, S_AIR_QUALITY, false, "MQ-135");
          }
          
          void loop()
          {
          	gw.wait(100);
            
          	boolean bNeedRefresh = (millis() - lastRefreshTime) > SLEEP_TIME;
          	if (bNeedRefresh)
          	{
          		lastRefreshTime = millis();
          	}
          	for (int i = 0; i < 8; i++)
          	{
          		int lightLevel = 0;
          		for (int j = 0; j < 32; j++)
          			lightLevel += analogRead(i);
          		lightLevel = (lightLevel / 32);
          
          		int nDifference = lastLightLevel[i] - lightLevel;
          		if ((abs(nDifference) > 5) || bNeedRefresh)
          		{
          			gw.send(msg.setSensor(i + 1).set(lightLevel));
          			lastLightLevel[i] = lightLevel;
          		}
          	}
          }
          

          Raw ADC readings are 20-80 (differs from sensor to sensor) for fresh outdoor air.

          G Offline
          G Offline
          gigaguy
          wrote on last edited by gigaguy
          #4

          @robosensor What is on pin 13? In the Setup() warmup part?
          btw, I am getting a value of 1 or 2, after warmup.

          R 2 Replies Last reply
          0
          • G gigaguy

            @robosensor What is on pin 13? In the Setup() warmup part?
            btw, I am getting a value of 1 or 2, after warmup.

            R Offline
            R Offline
            robosensor
            wrote on last edited by
            #5

            @gigaguy pin 13 connected to board's led :)
            Warm up is just 3-minute pause (with led blinking) between power up and start of main loop measurements transmission. Cold sensors shows very big measurements, so I heating them up for 3 minutes.

            1 Reply Last reply
            0
            • G gigaguy

              @robosensor What is on pin 13? In the Setup() warmup part?
              btw, I am getting a value of 1 or 2, after warmup.

              R Offline
              R Offline
              robosensor
              wrote on last edited by robosensor
              #6

              @gigaguy
              example response of sensors for natural gas (left) and alcohol (right):

              gas.png

              1 Reply Last reply
              0
              • E Offline
                E Offline
                epierre
                Hero Member
                wrote on last edited by
                #7

                @robosensor this graph shows well the limit ofg the MQ gas sensors, they somehow all react to the same gases, and thus are not one gas specific.

                z-wave - Vera -&gt; Domoticz
                rfx - Domoticz &lt;- MyDomoAtHome &lt;- Imperihome
                mysensors -&gt; mysensors-gw -&gt; Domoticz

                R 1 Reply Last reply
                0
                • E epierre

                  @robosensor this graph shows well the limit ofg the MQ gas sensors, they somehow all react to the same gases, and thus are not one gas specific.

                  R Offline
                  R Offline
                  robosensor
                  wrote on last edited by
                  #8

                  @epierre in general, you are right, but there are exceptions.

                  This measurements, but one sensor per graph:
                  mq2.png
                  mq3.png
                  mq4.png
                  mq6.png
                  mq7.png
                  mq8.png
                  mq9.png
                  mq135.png

                  And 6-day graph of all sensors:
                  mq-all-6days.png

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    epierre
                    Hero Member
                    wrote on last edited by
                    #9

                    @robosensor sure, but people need to know not to rely for their life on these classes of sensors.

                    z-wave - Vera -&gt; Domoticz
                    rfx - Domoticz &lt;- MyDomoAtHome &lt;- Imperihome
                    mysensors -&gt; mysensors-gw -&gt; Domoticz

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Viper_Scull
                      wrote on last edited by
                      #10

                      is it normal that the reads of my MQ-2 sensor are 0ppm in fresh air?
                      If I use a lighter to throw some gas, the reads skyrocket and then without gas they go back to zero.
                      Should I adjust the sensitivity?

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        epierre
                        Hero Member
                        wrote on last edited by
                        #11

                        @Viper_Scull If you follow my method, it considers clean air has no toxic gas so it should be 0.

                        If you are able to do a calibration, you could change that, but I would recommend to keep the straight lean air way if you cannot do that.

                        z-wave - Vera -&gt; Domoticz
                        rfx - Domoticz &lt;- MyDomoAtHome &lt;- Imperihome
                        mysensors -&gt; mysensors-gw -&gt; Domoticz

                        V 1 Reply Last reply
                        0
                        • E epierre

                          @Viper_Scull If you follow my method, it considers clean air has no toxic gas so it should be 0.

                          If you are able to do a calibration, you could change that, but I would recommend to keep the straight lean air way if you cannot do that.

                          V Offline
                          V Offline
                          Viper_Scull
                          wrote on last edited by
                          #12

                          @epierre said:

                          @Viper_Scull If you follow my method, it considers clean air has no toxic gas so it should be 0.

                          If you are able to do a calibration, you could change that, but I would recommend to keep the straight lean air way if you cannot do that.

                          @epierre I use the AirQualitySensor example in the MySensors library that I believe is your work indeed. Happy now to know that it's ok 0ppm for clean air.

                          One thing I noticed on the sketch is that last_mq inital value is 0. If we are in clean air, no value is send to the gateway when connected because val_mq = last_mq, so the controller would have no data to display unless it sets it to zero by default or gas is present.

                          By the way, I can't get domoticz to recognize this sensor. It receives the name of the sketch and the version, but although I can see that the sensor sends the value, there's no sign of it in the log of domotic and no device is recognized.

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            epierre
                            Hero Member
                            wrote on last edited by
                            #13

                            @Viper_Scull I have someone else reporting this, you should ask @GizMoCuz for him to support it

                            z-wave - Vera -&gt; Domoticz
                            rfx - Domoticz &lt;- MyDomoAtHome &lt;- Imperihome
                            mysensors -&gt; mysensors-gw -&gt; Domoticz

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              Viper_Scull
                              wrote on last edited by
                              #14

                              @epierre, already asked. Turns out, domoticz doesn't support V_VARs. Changing V_VAR1 in the sketch for V_DUST_LEVEL works.

                              Moshe LivneM 1 Reply Last reply
                              0
                              • V Viper_Scull

                                @epierre, already asked. Turns out, domoticz doesn't support V_VARs. Changing V_VAR1 in the sketch for V_DUST_LEVEL works.

                                Moshe LivneM Offline
                                Moshe LivneM Offline
                                Moshe Livne
                                Hero Member
                                wrote on last edited by
                                #15

                                @Viper_Scull Domoticz supports V_VARs, just not as a viewable or modifiable value on the controller. It will store and return the V_VARs values for the sketch, nothing more.

                                1 Reply Last reply
                                0

                                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                With your input, this post could be even better 💗

                                Register Login
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                23

                                Online

                                12.0k

                                Users

                                11.2k

                                Topics

                                113.4k

                                Posts


                                Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • MySensors
                                • OpenHardware.io
                                • Categories
                                • Recent
                                • Tags
                                • Popular