Sunday, July 2, 2017

MQ gas sensor correlation function against temperature and humidity

We have taken a look at the MQ sersor in this post here:
As I said those sensor are electro-chemical. Accuracy of those sensor is not the best. Also they will react to many gases. It means that if you are trying to measure the ppm of a certain gas with this sensor, you will have false measurement values if any of the other gas that the sensor react to, changes.
Here I will "overengeneer" on this type of sensor, trying to correlate the MQ sensor readings to temperature and humidity too, even if this correlation to me is not prominent. The correlation formula I've found may be wrong, so let me know if there is something to fix here.

As the previous post, let's consider the MQ-135 sensor as reference, find below the "sensitivity characteristics of the MQ-135" figure of the datasheet we used to obtain ppm from resistance redings.

The MQ sensor datasheet also comes with a "typical dependence curve of the sensor on temperature and humidity" let's call it dependance curve.

It shows us how the Rs/Ro change against temperature and humidity.

What we are searching here, it's a way to relate the sensitivity characteristics curve with the temperature and humidity dependance curve.
It means, a way to find out ppm, reading the sensor resistance at any given temperature and humidity. 

Before going ahead.
Notice that the sensitivity characteristics curve, from which we have found the correlation function ppm=a*(Rs/Ro)^b coefficients a and b, it's acquired under certain circumstances, as example for the MQ-135 sensor 20C temperature, 65% humidity, O2 21%, Rl 20k, Ro at 100ppm of NH3 in the clean air.

The dependance curve of the sensor also it's acquired under certain circumstances, as example for the MQ-135 sensor Rs at 100ppm of NH3, Ro at 100ppm of NH3 in air at 33%RH and 20C.

At first we have to ask if we can relate sensitivity characteristics curve to dependance curve of the sensor?
Each curve it's taken under different circumstances, the first has both Rs and Ro acquired at 20C temperature, 65% humidity, the second one has Ro acquired at 20C temperature, 33% humidity. Both ratio related to a Ro at 100ppm of NH3.
The Ro of the sensitivity characteristics curve it's taken at Ro at 100ppm of NH3 in clean air, just as the Ro of the dependance curve, only temperature and humidity differs. Obviously it means that the sensor resistance at 100ppm of NH3 Rs it's just Ro, at any temperature and humidity,
This does not means that the sensitivity characteristics curve it's valid under any temperature and humidity, notice that the dependance curve may differs under different gas concentration, or different gas. We just know it's acquired for 100ppm of NH3, but we do not know what's the dependance curve at other gas concentration. Also the sensitivity characteristics curve may differs under different circumnstances of temperature and humidity.
If we suppose that the dependance curve it's linear over different gases and concentration, we may relate the ratio of the sensitivity characteristics curve to dependance curve.

We get the a and b coefficients for the correlation function ppm=a*(Rs/Ro)^b from the sensitivity characteristics curve.
To estimate the Ro, we read the sensor resistance at temperature T1 and humidity H1 (Rs_T1CH1%) given a know amount of gas to estimate. We are trying to estimate Ro at 100ppm of NH3, at temperature 20C and humidity 65% (Ro_20C65%).
Because the sensitivity characteristics curve it's acquired 20C and 65%, we need to correlate Rs_T1CH1% to Rs_20C65%. We can do this, because we have assumed that the dependance curve it's linear over different gas amount.
Defined Ro at 100ppm of NH3 20C and 33% as Ro_20C33%, by the dependance curve
Rs_T1CH1% / Ro_20C33% = m
Rs_20C65% / Ro_20C33% = n
Rs_20C65% = (n / m)*Rs_T1CH1%
Ro_20C65%=(n / m)*Rs_T1CH1%*(a/ppm)^(1/b)

Now conditions changed, we are at temperature T2 C and humidity H2 % (Rs_T2CH2%).
So hour function is not yet valid
for this function to be validated, we need the to correlate Rs at temperature T2 C and humidity H2 % (Rs_T2CH2%) to temperature 20C and humidity 65% (Rs_20C65%).
As we does for the previos correlation
Rs_T2CH2% / Ro_20C33% = t
Rs_20C65% / Ro_20C33% = q
Rs_20C65% = (q / t)*Rs_T1CH1%
ppm=a*((q*Rs_T1CH1%)/(t * Rs_20C65%))^b

To find out the correlation factor between this ratio, the simpler thing here, is to build up two lookup tables, one for each of the curves showed.
On the MQ-135 we will get a lookup table for 85%RH and one for 33%TH curve.
We can do this by using a tool like WebPlotDigitalizer.
Now, we have two lookup table, for two different humidity values. Each of the points of that lookup tables represent the Rs/Ro against a certain temperature.

lookup_33 = (t_1C33%, RsRo_1C33%),  (t_2C33%, RsRo_2C33%) ... (t_nC33%, RsRo_nC33%) 
lookup_85 = (t_1C85%, RsRo_1C85%),  (t_2C85%, RsRo_2C85%) ... (t_nC85%, RsRo_nC85%)

We can use linear interpolation ( to relate Rs/Ro to any temperature and humidity.

As example, suppose we have read Rs at 22C, 50%
Rs_22C50%/Ro_20C33% will be the interpolation between Rs_22C33%/Ro_20C33% and Rs_22C85%/Ro_20C33%.
Rs_22C33%/Ro_20C33% = RsRo_20C33% + (22C - 20C)*(RsRo_30C33% - RsRo_20C33%)/(30C-20C)
Rs_22C85%/Ro_20C33% = RsRo_20C85% + (22C - 20C)*(RsRo_30C85% - RsRo_20C85%)/(30C-20C)
Rs_22C50%/Ro_20C33% = RsRo_22C33% + (50% - 33%)*(RsRo_22C85% - RsRo_22C33%)/(85%-33%)

At the end it's just a matter of linear interpolation, to find out m and n values to estimate Ro at 100ppm NH2 temperature 20C and 65% humidity, and t and q values to estimate Rs at temperature 20C and 65% humidity.

All have been implemented and tested using an ATmega8.
The underlying chart shows the CO2 ppm correlation.
Time is reported on the x-axis, CO2 ppm values on the left y-axis, humidiy and temperature values on the right y-axis.
The MQ135 resistance reading are then translated to ppm with and without the temperature / humidity correlation method (green lines).
A reference NDIR sensor shows us an accurate CO2 reading.
As you can see the CO2 temperature/humidity correlation function works slightly better than the uncorrelated one.
Tests are repeated 3 times per sensor, using 3 MQ135 sensors.

The code below implements the resistance to ppm corrleation functions, and also the temperature and humidity correlation functions, as the main correlation function mq_getppmtemphumd.


  • read risk disclaimer
  • excuse my bad english


  1. Dear Mr Gironi,

    I am working on a room climate sensor board for a university project. I'm using the MQ 135 sensor and at the moment this library:
    As far as I understand, the library relies on your earlier blog entry from 2014.
    I'm still trying to calibrate the sensor correctly. I now found your very recent blog entry concerning the correlation functions. Do I understand correctly, that I'd need a way to get sensor readings under known temp./ppm conditions to make use of the code posted above?

    Lotte Heckmann

    1. Hello, first of all good work for your univ. project. Yes, this project it is based on the 2014 article, and on the last month MQ article. You need to know the correct amount of ppm for the gas you are trying to measure, and the sensor resistance for the basic correlation formula. About the temperature and humidity you just need to interpolate the dependance curve (fig. 4). To be hones one could also correlate it to temperature and humidity reading and obtain the fig 4 curve, but the dependance curve it's enough to me for this type of sensor. Hope this helps.

    2. Hello, thanks a lot for your fast reply. As far as I know, we don' have the utilities to measure the ppm correctly, but I'll have to talk to my supervisors again to be sure. Thanks a lot anyways!

  2. This comment has been removed by the author.

  3. Hello Davide thanks for the great tutorial, I was wondering if you've ever used the mq-137 for ammonia? If so, what are your thoughts on it compared with the mq135.

    1. Hello Gustavo, thank you for your comment. No, I've never use the mq137 so unluckly I can't compare it to the mq135.

  4. This comment has been removed by the author.

  5. Hello Sir,In my project if I enter mqR=3000 as pull down resister,only then I get a decent ppm readings...can you explain me why it is happening?
    Note:I have been heating the sensor for 24 hours

    1. Hello, before using the stright code, it's better to check it using a multimeter and doing the math on a spreadsheet.

  6. This comment has been removed by a blog administrator.

  7. This comment has been removed by a blog administrator.

  8. This comment has been removed by a blog administrator.

  9. hello sir thanks for sharing priceless information.
    sir am using TGS2610 for my university project am not am to find co relation function please help me.

    1. Hello, I've take a look to the TGS 2610 datasheet. It seems to me you can use the same method I'm using here on the Sensitivity Characteristic table. It wouldn't be that difficult.

  10. Thanks a lot Davide for such enlightening article. I'll be using these MQ sensors for my graduation project related to IoT and Data analytics. Will cite this article accordingly. Thank you again for all the help

    1. Hello molinajimenez. It will be a pleasure to have been cited. If you mind please mail me your graduation doc. Thanks!

    2. I will sure do so. Do you happen to have twitter or something so we can stay in touch? I'd like to keep improving my skills at sensors in order to become a better IoT developer! :)

    3. Sure, you can find me here:

    4. Alright, already followed you. Thanks a lot!