Pages

Monday, May 1, 2017

MQ gas sensor correlation function estimation by datasheet

The MQ series are cheap gas sensors.
They are resistive electro-chemical sensors.
These sensors have a heating element and a resistance that react to gases.
Below a MQ135 sensor opened. Inside it looks like that.

To read the sensor's resistance, we measure the voltage drop of the sensor.
The method we use to measure resistance on the micro is by using the ADC, and a pull-up (or pull-down) resistor. Here http://electronics.stackexchange.com/a/70012 you can find a good explanation on the pull-down/up method on reading resistance.

As a reference, let's consider the MQ135 sensor (again).
We will look at the "sensitivity characteristics of the MQ-135" figure of the datasheet.

Notice that it's a log-log plot, this means that is has logarithmic scales on both axes.
Another thing to notice, is that the y-axis is for Rs/Ro, it will be much clearer if we impose ppm on the y-axis.
So as a first step, we will flip the axis.
As a reference, above we will see a flip axis example

Now, let's take a look at a power function with different exponents.

Let's try to plot those function on a log-log scale.

You may notice that an exponential function flipped is again an exponential function, with different coefficient of course.
As you may see, the power function with a negative exponent looks like the one we are searching for.
Summarizing, let's take the power function y=x^-2 and plot this on the log-log scale, with a 10 multiplier

We are searching for something like:

y=a*x^b

with b<0

Now, our purpose is to find out the value of the a and b constants, starting from the datasheet figure.
We can do this by performin a nonlinear regression. Specifically we need a power regression.
First, we need to collect values from the datasheet figure.WebPlotDigitizer is the tool we are going to use here: https://github.com/ankitrohatgi/WebPlotDigitizer. We can find few online tutorials that teach us how to use WePlotDigitalizer. I will try here to sum it up here:
1) extract the figure from the datasheet
2) load the figure on WebPlotDigitalizer
3) set axis, remember to se axis both as log
4) check out points from the curve of the gas you need to found
5) click on the view data button, and you get the points you need.

Now that we have found out the points of the curve from the datasheet, we need something to load with those points and perform a power regression.
You can use matlab or you favorite math software tools. There are also other online tools you can use to perform power regressions. Here I will use an R (ref. https://en.wikipedia.org/wiki/R_(programming_language)) script I've write.

You just have to load the points values you have found out, and launch this R script. You can also launch it on http://www.r-fiddle.org/.
The script will find out the function coefficients (a and b), and print out the function found next to the graph points, just to check the function's accuracy.

Below the Rs/Ro vs ppm function, as seen on the datasheet.

Then the flipped function we are searching for

Then, the plot of both functions on linear scale axes:

Now we have to estimate the Ro coefficient.
Ro estimation is quite simple
We just need to solve this equation for Ro
ppm=a*(Rs/Ro)^b
Ro=Rs* sqrt(a/ppm,b)=Rs*(a/ppm)^(1/b)
note: in my library here: http://davidegironi.blogspot.it/2014/01/cheap-co2-meter-using-mq135-sensor-with.html i simply Ro as Rs*sqrt(a/ppm,b) = Rs*exp(ln(a/ppm)/b), which is the same that Rs*(a/ppm)^b.

So given the function coefficients, we have to measure the resistance of the sensor at a know amount of ppm for the gas we are investigating.

Now, just substitute the found function coefficients (a and b), the resisteance we have read (Rs in ohm) at the amount of gas (ppm) to the estimation Ro function
Ro=Rs*(a/ppm)^b

One last step is to provide the Rs/Ro limits we are going to use to validate the input for our solving function ppm=a*(Rs/Ro)^b
We know that Rs/Ro=(ppm/a)^(1/b)
So
min[Rs/Ro]=(max[ppm]/a)^(1/b)
max[Rs/Ro]=(min[ppm]/a)^(1/b)
By the datasheet figure we have to select the max and min Rs/Ro and ppm points.
Notice that we get the max Rs/Ro value for the min ppm point value.

55 comments:

Sir please correct me if I am wrongfirst we have to calculate the value of scaling factor and exponential by using the R script you gave then we need to find the value of Ro for our sensor so we need to write the equation you gave for Ro and Rs/Ro and upload it to arduino and wait until 24 hours. After that we have to see the value of Ro and Rs/Ro by defining it as constant and then we can calculate ppm value.Sir please correct me if I am wrong

Hello. It something like this. You can use the R script to get the "a" and "b" factor, and also the Ro value by reading the resistance of the sensor (heated up for a few hours) at a know value of ppm. Then you can upload all to Arduno. By datasheet the sensor did consisten readings after 24 hours although it may takes just an hours to get good reading to me.

I was wondering how you came to a known value of ppm for a gas (CO2/NH4 etc.). Did you leave your sensor outside/inside and find the relative/general values of the ppm from references like google or the internet. I understand there is some percentage in the air that stays relatively constant. Otherwise I would like to know how you knew exactly what amount of ppm you put into the sensor without using another sensor to measure it! :)

Hello, it really depends on which gas are you trying to measure. For CO2 it was quite simple https://www.co2.earth/, for other gases, you must have a calibrated sensor or a tricky way to get a know amount of gas.

Thank you for the fast reply. I think for the scale of my project the ship for the other gases has sailed. after a little bit of googling I also come to the same website. Thanks for sharing the website. Have a great evening :)

Hi Davide, How did you determine the RL value of the sensor? In the datasheet it says its adjustable and the sensor I have does not have the 6 pins but is attached to a blue sort of board and has 4 pins. I have no clue on how to determine the RL or is this an exterior resister I can add myself to the A0 to the Ground?

Hello, Rl is the load resistor. As example in the MQ135 datasheet it is stated to chose a value between 10k to 47k). It's a pull down resistor, take a look at the schematics here http://davidegironi.blogspot.it/2014/01/cheap-co2-meter-using-mq135-sensor-with.html

Hello, yes. The MQ-7 follow the same principles of this sensor. You have just to built up the conversion points, and the compute factors for the sensor you are using and the gas you would like to trace.

Before I apologize if my language is still wrong, I want to introduce myself first, I am chan heldo, I come from Indonesia. I have the final task to make a CO gas detector using MQ-7 sensor. But I am still confused in making arduino program code for the MQ-7 sensor. I hope you can help me in making the program code. thank you

Hello, at first you have to check it out the factor you have found by simply reading the resistence of the sensor with a multimeter and applying the conversion formula with the factor values found. For CO and MQ-7 of course you can not use the a,b Ro value that I provide as sample, but you have to find your how using the method of this blog post.

I just worked through the MQ-7 and I got the values for MQ-7 CO (not the heater/RL values, I used a module that already does this part). I didn't do a power regression since there were two points that were exactly marked on the data sheet. Here is the octave/matlab code and the final equation:

Thanks for this post, Davide, it has been very helpful for my bachelor thesis :). I also used this post as a helper, and I guess I corrected the value he found there a little bit ;): http://forum.arduino.cc/index.php?topic=294085.0

Hi pedro, thanks for your response Have you ever designed a tool using the MQ-7 sensor? Can you help me to complete my task? and Can you give me your email? Or you can chat with me at sinagabonor2320@gmail.com. Sorry if my language is still wrong

Hello, power up the sensor header (one Hpin to GND, the other to VCC), then read with a multimeter the value between A and B pin. Start with a multimeter and a spreadsheet, then you can go through a microcontroller.

Hi Davide, I am working with ozone sensor and i could not understand how "long rS = ((1024.0 * mqR) / adcRaw) - mqR;" came in the formula. I am using the same formula as in your blog, just changing the values and I am not sure if it is correct because it does not match with the photometric ozone analyser that we have in our university. I was wondering if you can explain "long rS = ((1024.0 * mqR) / adcRaw) - mqR;" line in the formula.

Hello, you have to measure the resistance of the sensor between A and B pin when it's in a know amount of gas. In my example it was in 392ppm of CO2, and i measure 26954ohm. You have to put your sensor in a know amount of methane and measure the resistance of it, please be carefull with CH4.

I was trying to reproduce your project for measuring CO2. I followed the same process you explain on the video, i've got the following values:rO = 42842; a = 111.336; b = -2.716215;which are sufficiently close to those you're reporting.The problem is that i am obtaining Rs/Ro values around 7 and they continue incresing (now they are around 20) thus going further from the maxRsRo = 2.428. Of course the result is an "out of range" message.

However, i also performed the same process using the values of r0, a and b that you are using.

Hello, at first connect you sensor and measure the resistance of the sensor using a multimeter. Then check that your resistance value measured on your micro is almost the same. You should get a resistance value on range, if not it could be your sensor. A valid resistance should be something around 30K.

Hello Mr. Gironi,My question is regarding the why's and how's of the adc and pull up resistor wiring configuration to get a reading for mrcs and mppm. I read through the link you've included and am still a little lost. Second, the configuration I'm using is a resistor that goes from the power rail to the 5V input on the mq sensor. this resistor is in series with another resistor that is connected to the A0 pin on the sensor. Am I close to correct configuration or completely off?

Hello, the pull up it is used to read a resistance, here is the MQ resistance. We need the sensor resistance cause we use it to calculate the ppm. You can experiment heating the sensor up with a power supply between H pins, and then read the resistance between A and B pin using a multimeter, the resistance you read should be the same that you observe on the microcontroller side. Here you can find a good explanation on the pullup resistor usage and reason for use them https://electronics.stackexchange.com/questions/70009/why-use-a-pull-down-resistor-with-a-ldr-and-microcontroller . I really do not understain to much of your circuit, there should be no resistor in series to to me, you circuit should look like this one https://www.elprocus.com/wp-content/uploads/2016/05/MQ-%E2%80%93-135-AIR-QUALITY-SENSOR.jpg.

Thank you for your reply, I figured out the confusion! My sensor is attached to a small carbon board that has a pull down resistor inside of it already. The board consists of 4 pins A0, G, D0, and V. The configuration is straight forward, and the measured resistance of the sensor will be taken from the A0 pin using R2 = (Vin/Vout-1)R1, when R1 is 10K potentiometer on the back of my sensor's board and R2 is the resistance of the sensor.

Hello and thank you. The sketch could be the same of the CO2, what's change are the a, b factor and Ro value. You can find the a and b factor using the method proposed here in this post, you can even use the Ro proposed here for CO2 but it would be better if you find the NH3 appropriate one using a know amount of NH3 and the method proposed.

I just successfully managed to reproduce your setup using a pre mounted board named FC-22 with the MQ135 on it. The board comes with a pull-down resistor of 1k and some other features for digital reading.

I would like some help regarding my readings. My Raw measurements tend to fall between 36 and 40 on a closed environment. For these values I get between 374ppm and 504ppm. It seems the ppm values vary too much for very small changes on the raw readings and since raw data can range from 0-1023 it's an indicative the precision is not very high. Are my readings close to yours? Is there any way I could try to increase precision with this sensor?

Hello and thank you. Yes, the correlation formulae it's pretty sensitive to slow raw changes, due to the conversion factors. You can make it a little better correlating it to a NDIR sensor, but to me this is not usefull, I mean, this sensor is a eltectro/chemical one, you can use it to get a ~pretty~ accurate values, for more you need to get to NDIR.