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 11.3k 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.
  • 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
        • epierreE Offline
          epierreE 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
          • epierreE 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
            • epierreE Offline
              epierreE 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
                • epierreE Offline
                  epierreE 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
                  • epierreE 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
                    • epierreE Offline
                      epierreE 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
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          29

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          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