MQ2 Sensor w/AirQuality Sketch

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

    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.

  • 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);
    		digitalWrite(13, LOW);
    		digitalWrite(13, HIGH);
    		digitalWrite(13, LOW);
    	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()
    	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.

  • Hero Member

    you can find the curves for the MQ2 here:

    Check the thread about gas sensors:

    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

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

  • @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.

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


  • Hero Member

    @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.

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

    This measurements, but one sensor per graph:

    And 6-day graph of all sensors:

  • Hero Member

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

  • 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?

  • Hero Member

    @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 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.

  • Hero Member

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

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

  • Hero Member

    @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.

Log in to reply

Suggested Topics

  • 41
  • 2
  • 1
  • 7
  • 3
  • 45
  • 18