Pages

Saturday, January 25, 2014

Cheap CO2 meter using the MQ135 sensor with AVR ATmega

MQ135 is an Air Quality Sensor suitable for detecting of NH3, Alcohol, Benzene and other gases.


The description below, is what i derive from the poor datasheet of this sensor, it may be uncorrect, so if you have suggestions please leave me a feedback.

update - Here you can find a way to estimate the correlation function of MQ sensors, and extrapolate coefficients from the datasheet curves:
http://davidegironi.blogspot.com/2017/05/mq-gas-sensor-correlation-function.html
update - Here a temperature and humidity correlation method for the MQ sensors;
http://davidegironi.blogspot.com/2017/07/mq-gas-sensor-correlation-function.html

The "sensitivity characteristics of the MQ-135" figure of the datasheet, you can see it below, it is used to convert the output of the sensor to the related ppm physical physical charateristic for the gas under test.


The graphic above seems a power function
y = a*x^b
so
ppm = a*(Rs/Ro)^b
using power regression, we can obtain scaling factor (a), and exponent (b), for the gas we would like to measure

Then
Ro = Rs * sqrt(a/ppm, b) = Rs * exp( ln(a/ppm) / b )

So, if we want to calibrate a sensor, we "just" need a know amount of a certain gas, then we can read the resistance output value from the sensor (Rs), and we can compute the calibrated Ro value.
We know the current amount of CO2 gas in atmosphere http://co2now.org/, we can use this as a reference for calibration.

Datasheet tell us even detecting concentration scope for a certain gas, so we can compute the limit for Rs/Ro.

Rs/Ro_limit = (ppm/a) ^ (1/b)

Now, because i want to build a CO2 meter, let's try to calibrate this sensor for measure CO2.

For CO2, if we measure points graph and do power regression we can obtain the function
ppm = 116.6020682 (Rs/Ro)^-2.769034857
we also know that the current amount of CO2 gas in atmosphere is (unfortunately) 392ppm, so, heating the sensor for 24 hours, and leaving it in open air, if we measure 26954ohm as the resistance output we can Ro should be 41763.
Datasheet does not tell us much for detecting concentration scope for this gas, the figure is from 10 to 200ppm, but we can suppose a limit from 10 to 2000ppm.

The above derivation seems to works quite well, but for estimating the scaling factor, and exponent in a more precise way, i've logged raw adc data from MQ135 and correlated to a MHZ14 NDIR infrared sensor.

Data was logged on a xively datastream.


You can see the correlation results above.
Correlation is done by a matlab script using power regression and polynomial curve fitting.
We have to convert raw value to calculated resistance.
And then, because we will later use a Ro, we "impose" a arbitrary Ro, such that scaling factor and exponent can be used in the math of our microcontroller.
Imposing a Ro of 41000, obtained scaling factor in my experiment is: 56.0820, and exponent is -5.9603.
Using this parameters now we should obtain a better response for our sensor.
The Ro calibration is again important because one sensor can differ from other.
The previous reading of 26954ohm return, using those new correlated scaling factor and exponet values,  us a value of 683ppm, without any calibration of the Ro.

I've checked the xively datastream with the new correlated function, and it seams to repond a little better than the datasheet one, even if reading does not always respond as is expected.

I've checked this method against 5 sensors, 4 of them, after the Ro has been calibrated, seems to fit the same response curve, 1 not.
Because MQ135 are electro-chemical sensor, i do not expect all the sensors has the same curve.

With the code you can find the matlab script, and the speadsheet helper for computations.


ChangeLog
  • 01b: fixed inconsistent value of the R3 resistor between schematics and code (thanks to Emmanuel Pierre for reporting this bug) 
  • 01: first version.

Code

Notes
  • read risk disclaimer
  • excuse my bad english

365 comments:

  1. The chance for getting this to work is possibly quite high then? Compared to other CO2 sensors, this makes a really cheap variant so it would be awesome for DIY :) I've ordered some MQ-135 to see if I can get it to work as well!

    ReplyDelete
    Replies
    1. yes, but keep in mind that 1 of the 5 sensor i've got has a different behaviour of the other.
      so the calibration curve is different, and you can not use the one i've proposed here.

      Delete
    2. Hello Davide,
      i am not able to follow your calculation. On my MQ135 i get a value of 27037Ohm. Please can you explain step by step how to calculate the value of Ro ?

      Delete
    3. Hello, if you assume that
      ppm = a*(Rs/Ro)^b
      You can make some math around that, and get that
      Ro = Rs * sqrt(a/ppm, b) = Rs * exp( ln(a/ppm) / b )
      To understaind how to get here, take a look at the properties of exponential function (google around for that), and properties of logarithmic function.

      Delete
    4. Hello,

      in my opinion

      y = Rs/Ro and x = ppm value

      isn´t it?

      Delete
    5. Hello, take a look at "Ashutosh Bhatt" comments, May 28, 2015 and June 1, and my reply. Let me know it sounds right to you too.

      Delete
    6. In your formula ppm = a*(Rs/Ro)^b,we want to get ppm but how to get Ro?Does Ro is 30KΩ-200KΩ based on the datasheet?

      Delete
    7. If ppm = a*(Rs/Ro)^b a then Ro = Rs * sqrt(a/ppm, b) = Rs * exp( ln(a/ppm) / b ). Said that you know how many ppm there are, you can measure Rs of the sensor, and then you can get Ro. The datasheet states that Rs should be between 30Kohm and 200Kohm.

      Delete
    8. yes,but the sensor give output voltage then i want change it to ppm?can i know how?I need to get ppm i should know how to find Ro

      Delete
    9. The MQ135 is a resistive chemical sensor. To read the sensor resistance, we measure the voltage drop of the sensor by the ADC using a pull-up (or pull-down) resistor for the reason explained here: http://electronics.stackexchange.com/questions/70009/why-use-a-pull-down-resistor-with-a-ldr-and-microcontroller
      Said this, take at first the sensor as is (do not connect to any micro). Apply 5V voltage (at least from a 300mA source) to the H pins of the sensor. Then use a multimeter to read the resistance between A and B. You should get something near 20k. Blow on the sensor and this resistance should decrease.
      Once you have prove that your sensor works, read the sensor resistance using your ATmega (and the pull-down sensor), you should get the same resistance as the multimeter. Then you could apply the math of my library to convert Rs to ppm, and to estimate the Ro of the sensor.

      Delete
  2. By the way, you dont happen to have any video with the testing of this project? Blowing on it and showing some values from the UART? As I can't test this myself right now, it would be awesome to see it working on video :)

    ReplyDelete
  3. Unfortunately my multimeter detects the connection between H1 and H2 to be short circuit ( shows a resistance of 30 Ohm which is very low ) , hence when i connect this to my arduino voltage across USB drops to 3.6 V across the terminals and I am unable to run a 2*16 LCD display.The current drawn by the sensor is about 150 mA.What should be done ? Should I connect a 100 ohm resistor in series with the coil to limit current.Will that affect readings too much ? or should I use an external voltage regulator IC to drive the sensor ?

    ReplyDelete
    Replies
    1. hello,
      H1-H2 resistance is correct, this a chemical sensor with an heating element, H1-2 does the heating, that's the reason why it sucks 150mA.
      Connect the sensor as the circuit you find above. Use and external power supply if you pc can not supply enough current to run all your devices. Double check your wiring, keep in mind that with an external supply you can eventually damage your USB connection port.
      You can also use an external powered USB hub, this is what i usually use for experiments.

      Delete
  4. Replies
    1. you can find code in the link above, in the post.

      Delete
  5. Hallo,
    can you help me.
    if the MQ135 use for Alcohol meter, how about calculate conversion formula?
    i'm applying with C code.

    ReplyDelete
    Replies
    1. Apply power regression to "sensitivity characteristics of the MQ-135" figure of the datasheet, searching for alchool points.

      Delete
  6. hy mr. I use this code as an example of my project, what do you think?
    whether the Rs output is listed ppm?

    int adcPin = 0;
    int adcValue = 0;
    float v;
    float rs, ppm;
    void setup ()
    {
    Serial.begin (9600);
    delay (2000);
    }
    void loop ()
    {
    adcValue = analogRead (adcPin);
    v = adcValue * (5.00/1024);
    rs = (100 - (20.00 * v)) / v;
    Serial.print ("Volt out put");
    Serial.println (v);
    Serial.print ("RS =");
    Serial.println (rs);
    delay (1000);
    }

    ReplyDelete
    Replies
    1. To me the sensitivity characteristic curve to convert the reading from the sensor to ppm, found in the datasheet, seems a power function, and your Rs conversion is not power.
      Also i do not find any voltage to ppm reference in datasheet, this is the reason why i convert ADC to resistence value. You can also use voltage for conversion of course, it depends on your conversion function.

      Delete
    2. so, the output of the code does not display the data to ppm?
      because there are 2 outputs. The first output of the voltage, and a second output Rs, so now I'm confused. how to display data in the form of ppm, I've read its datasheet, but I do not understand to implement to the Arduino.

      whether you could help me?

      Delete
    3. yes, the first is the voltage.
      you have to relate the value read from the sensor to the ppm value of the gas.
      if you read my post above, you can find the description and methods to convert it to ppm.
      in this post you can also find code that compute this conversion.
      in a few word.. convert ADC value to resistence, then use the "sensitivity characteristics of the MQ-135" figure of the datasheet, to correlate it to ppm, finding the power regression curve that fits this conversion.

      Delete
    4. ok I'll try, thanks for the explanation

      Delete
    5. helow this is my poject http://www.youtube.com/watch?v=GA2juXog7NY i"m useng Sensor Mq123 for CO2 ditection, but valibration sensor inaccurate.

      I do not understand how to translate the formula in the datasheet to get the output ppm, bisakh you give me an example of code themselves?

      I built using micro controller manufacturer ATmega 328 Arduino uno.

      I was progreming basic visual studio. I hope you will help me because it is very hard so that I can complete my studies.

      Delete
    6. You can download sample code from this post, you will find it right above here, it's avr_lib_mq135_01.zip.
      There you can find my way to convert MQ-135 resistance value read from ADC to ppm.

      Delete
  7. Replies
    1. Hello, you have to use power regression on the datasheet curve.

      Delete
  8. Thank you,
    What is the Rs and R0 ?
    How to find Rs for CO2 gas ?

    ReplyDelete
    Replies
    1. You can reat this in the post above. Anyway, Rs is the resistance output value from the sensor, Ro is the calibration value used in to correlate read Rs to gas ppm. To find Rs, just read the resistance of the sensor when it is heated.

      Delete
  9. Thank for help,
    I am calibrate R0 for specific ppm of CO2 gas after set RL=R0,which can give me voltage at adc pin this voltage converted inform of percentage and display on LCD.
    When ppm change,R0 is change(according you calculated),again i am set RL=R0, and process repeated.i am used RL as a potentiometer(variable resistor).

    ReplyDelete

  10. How did you get 108251?

    #define MQ135_DEFAULTRO 108251 //default Ro for MQ135_DEFAULTPPM ppm of NH3

    ReplyDelete
    Replies
    1. Hello, using power regression on the datasheet NH3 curve.

      Delete
  11. I understand how you got all these numbers:

    #define MQ135_DEFAULTPPM 392 //default ppm of CO2 for calibration
    #define MQ135_DEFAULTRO 41763 //default Ro for MQ135_DEFAULTPPM ppm of CO2
    #define MQ135_SCALINGFACTOR 116.6020682 //CO2 gas value
    #define MQ135_EXPONENT -2.769034857 //CO2 gas value
    #define MQ135_MAXRSRO 2.428 //for CO2
    #define MQ135_MINRSRO 0.358 //for CO2

    You left the sensor on for 24 hours then measured the resistance between H1 and H2. This gave you a value of 26954ohms which is Rs. We know that the atmospheric level of co2 is 392ppm. So,

    Rs=26954ohms
    ppm =392

    y = a*x^b
    ppm = a*(Rs/Ro)^b
    Ro = Rs * exp( ln(MQ135_SCALINGFACTOR/ppm) / MQ135_EXPONENT )
    Ro = (26954) * exp( ln( 116.6020682/392) / -2.769034857 )
    Ro = 41763

    Now, I don't understand how you found the default Ro=108251ohms at 100 ppm for NH3.

    //define sensor resistance at 100ppm of NH3 in the clean air
    //calibrate your sensor to obtain precise value
    //Ro = Rs * sqrt(MQ135_SCALINGFACTOR/ppm, MQ135_EXPONENT)
    // = Rs * exp( ln(MQ135_SCALINGFACTOR/ppm) / MQ135_EXPONENT )
    //#define MQ135_DEFAULTPPM 100 //default ppm of NH3 for calibration
    //#define MQ135_DEFAULTRO 108251 //default Ro for MQ135_DEFAULTPPM ppm of NH3

    //the graphic at fig.2 of datasheet (sensitivity characteristics of the MQ-135)
    //seems a power function y = a*x^b
    //so ppm = a*(Rs/Ro)^b
    //using power regression, you can obtain scaling factor (a), and exponent (b) !for a specific GAS!
    //points: (1.5 , 10) (0.75 , 100) (0.59 , 200)
    //#define MQ135_SCALINGFACTOR 37.58805473 //for NH3
    //#define MQ135_EXPONENT -3.235365807 //for NH3

    //define the Rs/Ro valid interval, MQ135 detect from 10ppm to 300ppm
    //look at the datasheet and use the helper to define those values
    //#define MQ135_MAXRSRO 1.505 //for NH3
    //#define MQ135_MINRSRO 0.56 //for NH3

    Ro = Rs * sqrt(MQ135_SCALINGFACTOR/ppm, MQ135_EXPONENT)
    = Rs * exp( ln(MQ135_SCALINGFACTOR/ppm) / MQ135_EXPONENT )

    Q135_SCALINGFACTOR=37.58805473
    ppm=100
    MQ135_EXPONENT=-3.235365807

    Ro = Rs * exp( ln(37.58805473/100) /-3.235365807 )

    But what about Rs? How did you find the Rs value for NH3 at 100ppm so that you could find Ro?

    ReplyDelete
    Replies
    1. Hello,
      If you look at the datasheet curve, and record the points on the curve on it, then you can do power regression. You can build power regression in different ways, there are a bounch of tool online, or you can use matlab powerfit func.
      One you have done this, you have found Ro. Then you know that Ro = Rs * exp( ln(MQ135_SCALINGFACTOR/ppm) / MQ135_EXPONENT ), so you can find Rs.
      Note, please do not flow the comment with long text if not necessary.

      Delete
  12. How can we recognize which pins of the MQ135 sensor are to be left disconnected and which pins are to be connected to ADC for gas sensing projects using microcontroller ?

    ReplyDelete
    Replies
    1. Hello, look at the datasheet Fig.2
      You can even ook at the schematic in this page, use gas sensor opposite pins (A and B). This image may even be helpful http://www.staceyk.org/airSensors/MQ135.png

      Delete
  13. sir, please do let me know how to determine the value of other gases from the reading obtained from mq135 sensor. as i have come to understand it is a combination of all the values of air contaminants detected by the air.i am very grateful for the formula for co2 gas. please do help me with other gases as well

    ReplyDelete
    Replies
    1. You have to interpolate the points on the datasheet curve of the sensor for the gas you need. And apply power regression to that values.

      Delete
  14. hello there. thanks for the video. i am doing a project involving arduino and gas sensor. however, i am using a CO2 gas sensor, my whole idea is to test the sensor in one of the car and see how much of a CO2 does it emit in a certain time. my question is should i use MQ-135 sensor or MG-811 sensor for this? also is the coding and wiring same for MQ-135 and MG-811?
    thanks!
    Best Regards

    ReplyDelete
    Replies
    1. sorry not the video, i meant the instructions. was watching too many videos before i was directed here :). However my question is the same.

      Delete
    2. Hello, wiring is pretty the same, though it is better to wire the MG-811 through an opamp, to gain the signal, and have better readings. You can find an opamp circuit to drive the MG811 googling around.
      The MG811 it's a little better then an uncalibrated MQ135. Said this, my suggestion is, if possible, to spend a little more of the MQ-811 and use a NDIR sensor, i know they are not cheap as chemical sensor, but they have more accuracy.

      Delete
  15. thanks for that explanation!
    i am using a MQ7 sensor for my project, i calculated values of a and b for some gases,can you tell me if those values are correct?
    co2 a=99.09 b=-1.460
    co a=23.168 b= -0.6835
    h2 a=22.75 b= -0.7422

    ReplyDelete
    Replies
    1. Hello, I've calculated just the NH3 and CO2 gas value, if you look above in the post you can find my a and b value for CO2, 116.6020682 b = -2.769034857. You can find NH3 values in the header file for the library.
      If you used the power regression method, values are correct. Your values may be a little different from mine due to accuracy in extracting the points from the graph. May be that you have better eye and select the points in a better way than me.

      Delete
  16. Hi
    I use the mq135 sensor and a cubloc cb210 for my project. My project is to measure the CO2 levels in the air. I have no idea how to do . Can you explain more clearly because I am an amateur.
    Thank

    ReplyDelete
    Replies
    1. Hello, what i can tell you about the CO2 mesurement using the chimical MQ135 sensor is written in this post. If you are interested in more information i suggest you to make a request on the mysensors.org forum, which has some posts about the air sensors.

      Delete
  17. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  18. Hi Davide Gironi,
    I'm actualy working on the MQ135 sensor with an arduino uno card. My problem : I don't understand how I can be sure i measure CO2 value and not other gas.

    Thanks

    ReplyDelete
    Replies
    1. Hello,
      This is a chemical general sensor, so it measure many gases. You can approximate the resistance to ppm value of a gas by using correlation or directly using the datasheet curve. But you will not measure a single gas. Anyway, if just a gas change in your air mixture, you are measuring the change of that gas. To measure a single gas where many gas are involved and many are changing, you have to buy a specific gas sensor, better if NDIR.

      Delete
  19. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello. You can find the code for this project in the blog post above.

      Delete
  20. salve,
    può per favore aiutarmi a costruire la sua stessa applicazione però per Arduino ?
    grazie mille !

    ReplyDelete
    Replies
    1. Ciao.
      Passo uno, collega il sensore secondo le specifiche che trovi su questo post, o in vari altri siti: http://playground.arduino.cc/Main/MQGasSensors
      Passo due, leggi la resistenza del sensore utilizzando la funzione "AnalogInput" del framework Arduino.
      Passo tre, applica la formula che indico nel post per convertire la resistenza letta in ppm.
      Nota generica riguardo a questo sensore: vedi mio commento qui sopra del "April 9, 2015 at 8:14 PM"

      Delete
    2. Può specificare qual è la formula per convertire la resistenza letta in ppm ?

      Delete
    3. ppm = 116.6020682 (Rs/Ro)^-2.769034857
      ossia in c:
      ret = (double)MQ135_SCALINGFACTOR * pow( ((double)resvalue/ro), MQ135_EXPONENT);
      con resvalue, valore di resistenza letto in ohm, MQ135_SCALINGFACTOR, MQ135_EXPONENT e ro, impostati ai valori prescelti a seconda del Gas, utilizzando il datasheet, oppure correlazione e convoluzione. Trovi questi valori nella descrizione del post.

      Delete
    4. Scusa la mia ignoranza, ma per me è arabo. Non è che avresti qualche guida o un tutorial pronto per arduino ?
      Da quello che ho capito ppm=a*(Rs/Ro)^b
      a=116.602 b=-2.769
      quindi quello che mi manca è il rapporto Rs/Ro che non ho capito bene cosa sono.

      Delete
    5. TLTR; Ro = 41763
      Per la versione non TLTR; /Too Long Too Read/, ti consiglio di leggere bene il post sopra. Trovi anche dei post molto organizzati sul forum di Arduino.

      Delete
    6. Forse ho capito. Quindi Ro=41763 ed Rs= al valore della resistenza che trovo con la funzione Analoginput. Giusto ?

      Delete
    7. Buongiorno,
      ho provato con questo sketch, ma mi da come risultato il valore Infinito. Dove sta l'errore ?

      void setup(){
      Serial.begin(9600); //inizializza a 9600 bits a secondo
      }

      void loop(){
      int Valore= analogRead(A0); //leggi dal pin A0
      Serial.println(pow((Valore/41763),-2.769));
      delay(1000); // prendi temperatura ogni 2 secondi
      }

      Delete
    8. Prima di tutto dovresti misurare la resistenza, non il valore di uscita dall'ADC. Se cerchi su google o sul forum Arduino troverai come misurare una resistenza con Arduino.

      Delete
  21. Buongiorno e scusi ancora,
    Purtroppo come avrà capito non sono pratico ne con la programmazione ne tanto meno con l'elettronica. Può aiutarmi Lei ? le mostro sotto il link del modulo.

    http://www.plexishop.it/it/arduino/sensori-per-arduino/fumo-gas-agenti/modulo-sensore-qualita-aria-mq-135.html

    La ringrazio

    ReplyDelete
    Replies
    1. Così può andare bene ?

      void setup(){
      Serial.begin(9600); //inizializza a 9600 bits a secondo
      }

      void loop(){
      int Valore= analogRead(A0); //leggi dal pin A0
      Serial.println(Valore);
      delay(100); // prendi temperatura ogni 1 secondi
      }

      Delete
    2. Hello, looking at your module i've notice that it also have a digital output, you have to use the Analog output of the module for this purpose. Also, what you read with the analorRead function is the ADC raw value, that must be converted to resistence. Please write next comment in English, that way they could be usefull even for not Italian people.

      Delete
  22. Hi Davide,
    I am making use of MQ-135 sensor for detetcing the gas concentrations and I have understood it for CO2.
    The formula:
    Ro = Rs * sqrt(a/ppm, b) = Rs * exp( ln(a/ppm) / b )

    Will it help me in determining what value of resistance I need to add to detect NH3?
    Assuming I am changing the constant values for NH3 in the above equation.
    I don't have much knowldege regarding getting values from Data Graphs.So need your help in this.
    Thanks.

    ReplyDelete
    Replies
    1. Hello, to abtain your gas values for this sensor, you have to compute power regression. Anyway, you are "lucky", the NH3 values can be found in the mq135.h file of this project.

      Delete
  23. Hello

    How have adcres 22000,00?

    thanks

    ReplyDelete
    Replies
    1. In order to read the resistance value.You have to read the ADC raw value, and then, because we know the pulldown resistore, and the ADC accurancy, we can calcuate the resistance attached to the ADC input, which is our sensore resistance, that we can use to compute the gas value we need.

      Delete
  24. Ciao Davide,
    the datasheet give a further scale factor based on the operating temperature and humidity.
    Have you logged these parameters too and checked for a possible better fit?
    Co2 is above 400ppm now.
    http://co2now.org/

    Ciao, Leonardo

    ReplyDelete
    Replies
    1. Hello, yes I've looked at that graph. Consider that this is a chemical sensor, it warm up, and warming up it also changes temperature and humidity next to the sensor. So, for indoor use Rs/Ro, T is almost always over 30 degrees, and next to 1. Also consider that's this is not a CO2 sensor, and for that reason using the datasheet values without any NDIR sensor corralation could bring to inconsistend data. Said that, It could be interesting to log that data and correlate it if you are planning to use this sensor for outdoor use, or when ambient temperature is less than 10 degrees.

      Delete
  25. hello Davide,

    You are considering "ppm = a*(Rs/Ro)^b" but by analysing graph i found it as "(Rs/Ro)= a*ppm^b" as in graph ppm is given on X axis . Explain how you select this equation please . .

    ReplyDelete
    Replies
    1. To me the figure seems a semi-log graph, with a logarithmic scale on the x axis. So the function is something logarithmic, not exponential.

      Delete
    2. Revision: i mean log-log graph.

      Delete
  26. Yes function is logarithmic but I am not getting why you put "ppm" LHS(left hand side)? For me it should be on RHS in this equation

    "y = a*x^b
    so
    ppm = a*(Rs/Ro)^b". I also want to know how you do preheating. In datasheet it is mentioned to do preheating for two days.. So each time when i give supply to circuit, to get reading do we need to preheat sensor with 1.4 V and 5 V? Please mention also, for different gas sensor different preheat time and heater voltage is required. After doing preheating once for 48 hr, if I mount sensor on a PCB, do i need to maintain preheat condition each time before take readings??

    ReplyDelete
    Replies
    1. For sake of simplicity, I flip the graph (I forgot to mention it). If you flip it, the function is something like y = a*x^b, with positive a and negative b value so ppm = a*(Rs/Ro)^b.
      I've notice that 6 hours preheting is enough to get consistent values. But as usual, taking the "over 24 hours" preheat time of the datasheet as reference is better.
      I just preheat, and heat with 5V. Can not find the 1.4V and two days specs in the datasheet I've here.
      To me preheat means to power on the internal heating resistance of this sensor, while it is mounted on the PCB. Then after the preheat time, you can get "consistent" values, leaving the sensor "heated". Note that consistent is "double quoted" because you can not expect to get accurate values from a chemical sensor like this.
      Thank you for your interest, please give me a feedback, if you think I've made something wrong that way i can correct it.

      Delete
    2. Thaks for your helpful reply.. Actually I have come across many MQ series sensors and figaro sensors. Preheating with 5V and 1.4V is given in MQ-7 sensor,which I forgot to mention.

      Suppose I have preheat sensor today for 6 hours, getting values.. If after 3-4 days ,again I want read values,do I need to preheat for 6 hours again for "consistent" reading for "few minutes" will work??

      Can we detect more than one gas' concentration at a time in ppm using MQ-135? If yes how? If no why??

      Delete
    3. If you power off the heater letting the temperature goes down, you have to preheat it again, every time.
      We can detect more than one gas, but, keep in mind that it's not a true single gas detection. The chemical resistance inside this sensor react (if heated), to more than one gas, it can not isolate a single gas. So, what you get by conversion, is a resistance value to a ppm value, of many gas. However, if just one gas changed, or the other gases remains almost constant, you can get that changes. If you are able to mantain constant values for all the gas, and modify just one gas, then you can obtain the gas response over the sensor. The builder of this sensor does this job, making the figure i use for gas convertion. You can not be shure you are measuring just one gas.
      Also consider that, the chimical resistance degradate in a few years, I've read somewhere (can't remember well), 2 years.

      Delete
  27. Thanks for your description.

    I use three MQ135 to each system.
    I have question.

    1. I have been connect MO135's two A pin to 5V, and two B pin to Sensing signal, but according to "http://www.staceyk.org/airSensors/MQ135.png", it use just one A pin, and one B pin. I was wrong?

    2. I use three sensors. two sensors have Ro max that is 26kOhm, but one sensor's Ro max is 42kOhm. Do you know about these phenomenon?
    I use three sensors while over 4 days go on.
    "Rs/Ro" result different between 42kOhm and 26kOhm.
    I cording adaptive Ro algorithm, but it is running on empty on me.

    ReplyDelete
    Replies
    1. Hello and thank you for feedback.
      1. If you look at the datasheet MQ135 Fig 2. both A seems connected, the same is for B. Nothing wrong in connecting one pair or both.
      2. The 42K one is far from the standard accuracy of this sensor. But it can happens, those are really cheap sensors. It happens to me to have sensor that have wrong accurancy.

      Delete
  28. hello sir...
    I found the best support for the sensor MQ135..
    the best post ever seen on google.
    I am using this sensor for the following gases
    NH3
    Nox
    C2H5OH (alcohol)
    Co2
    Benzene
    smoke

    So I got these things for Co2
    #define MQ135_DEFAULTPPM 392 //default ppm of CO2 for calibration
    #define MQ135_DEFAULTRO 41763 //default Ro for MQ135_DEFAULTPPM ppm of CO2
    #define MQ135_SCALINGFACTOR 116.6020682 //CO2 gas value
    #define MQ135_EXPONENT -2.769034857 //CO2 gas value
    #define MQ135_MAXRSRO 2.428 //for CO2
    #define MQ135_MINRSRO 0.358 //for CO2


    I want those things for remaining gases also.
    Awaiting for a reply...
    you can contact on this email also
    ankit.pitroda2609@gmail.com

    ReplyDelete
    Replies
    1. Hello, thank you for feedback.
      For other gasses, you have to compute power regression starting by point you can read from datasheet. As alternative you can correlate as i do.

      Delete
  29. Hi Davide,

    Thanks for the highly informative post and equally detailed discussions.

    Can you please elaborate if the sensor can be used for higher concentrations of CO2 (in the range - 10000 - 40000 ppm) of course some error of order of 1000 ppm is acceptable.

    ReplyDelete
    Replies
    1. Hello,
      the datasheet does not even test this sensor for anything above 200ppm. Unluckly I do not have any testing evironment to run your test. Let me know if you can find a test cap and how is your test going.

      Delete
  30. Hi Davide,
    Wonderful write-up!
    Do you have the data that generates the sensitivity characteristics curves? Also the data generates the humidity/temperature impedance curve is what I am looking for too. By using our own ways to powerfit those curves, the conversion is not that accurate WRT the original data, isn't it? Thanks for your help.

    ReplyDelete
  31. Hi Davide,
    Wonderful write-up!
    Do you have the data that generates the sensitivity characteristics curves? Also the data generates the humidity/temperature impedance curve is what I am looking for too. By using our own ways to powerfit those curves, the conversion is not that accurate WRT the original data, isn't it? Thanks for your help.

    ReplyDelete
    Replies
    1. Hello and thank you.
      I do not have original data, I get a few points. I've also used an helper matlab script. Here you can find the script taos_re_builder.m, which is almost the same i've used there (unluckly i can not find the version i've used for the point grabbed) : http://davidegironi.blogspot.it/2013/06/irradianceilluminance-meter-using.html the original reference can be found here: https://jethomson.wordpress.com/tsl230r-articles/how-to-convert-a-graph-saved-as-an-image-to-point-series-plot-data/

      Delete
    2. This comment has been removed by the author.

      Delete
    3. l'immagine è fuori scala! Ho riscalato l'ascissa e l'ordinata (separatamente) in cad per ricavarmi i punti in modo preciso...

      Delete
    4. Ciao. Come dicevo all'utente prima non sono riuscito a trovare nessuna immagine ben definita, ho provato anche a contattare il produttore del sensore, che però non mi è stato di aiuto. In che senso è fuori scala? Potrei sbagliarmi ma mi sembra in scala, logaritmica. Se puoi e vuoi scrivimi pure la funzione di regressione che risulta dai tuoi punti. Grazie.

      Delete
    5. This comment has been removed by the author.

      Delete
  32. Your have done a very good job. Is there a way to get your source code in Python? Somebody can help please?

    ReplyDelete
    Replies
    1. Hello. Thank you for you comment. You can find the code for this project in the download link above in the description. But this avrgcc C code, i does not have any Python version of this.

      Delete
  33. Hello,

    How can I measure the Rs value of the sensor?
    Which pins should I measure?

    Thanks!

    ReplyDelete
    Replies
    1. Hello, you can measure the resitence of the sensor using a multimeter, between pin A and B.

      Delete
  34. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi, Thanks for your great post!

      I'm a little stuck with the calibration. How do you measure RL?
      Or do you get that through measuring Rs as mentioned above ? (looking at fig. 2 https://www.olimex.com/Products/Components/Sensors/SNS-MQ135/resources/SNS-MQ135.pdf there doesn't seem to be a resistor between AB?) I cant seem to find the part on the board to measure the resistance. I get a reading of 1kohm between the left bottom of the sensor and Analog out of the board.

      I tried to infer the resistance from adding a small 40 ohm resistor to it but the Rs value out of it and the response seems a bit unstable and inaccurate as it falls to 140ppm. I get values that shoot up to few thousand at times (For CO2). Possibly I do have a different curve, but I tried with a second board and its a little similar.

      I got myself these http://www.ebay.ch/itm/400490343695?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

      My Vout ranges from around 0.25 to 0.5 V, Analog read value of approx 50.

      I was calculating Rs from these values and the RL value.. which I assumed its same on every board (but cant figure out how to confirm this).

      / Also what exactly do you mean by imposing an Ro for calibration (stage2)? From the correlation you want to refine the exact Rs/Ro for that sensor, that may differ from the above chart?

      Thanks

      Delete
    2. Hello,
      to measure the resistence of the sensor i've used a pull-down resistor (R3), take a look here, this could help: http://electronics.stackexchange.com/questions/70009/why-use-a-pull-down-resistor-with-a-ldr-and-microcontroller
      You have to trace out what's that board is it doing to your sensor signal in order to use it. You have to be shure that the board does not gain your signal.
      If you are planning to use correlation as a method of calibration, imposing the Ro value means to fix it at a specific value, in order to get a calibrated scaling factors. Take a look to the matlab helper here mq135_correlation_helper.m.

      Delete
  35. Hi, thanks for this great set of instructions. I read all of the comments (".. not measuring one gas but if only one gas increases you can measure this increase."). I have 2 questions and it would be great if you could help me:

    1) Imagine you have a conference room and it is empty, windows are open for 4 minutes. When you then close the window you can assume around 350 to 400 ppm co2 in the room. Now lets say you have 10 people entering the room and breathing for an hour. I guess then the only gas (for the most part) beeing added is co2. Hence in this specific case the cheaper sensor can do the job of checking how much the level is increasing, right?

    2) Is there an even simpler multi-gas sensor that is an NDIR so that it uses less power? Then the above scheme could be done in a wearable device.

    Thanks for your advice, Marcus from Heidelberg

    ReplyDelete
    Replies
    1. Hello, 1) yes it can, but because it's a chemical sensor accuracy is not the best. 2) a cheap and pretty accurate one is the MH-Z19.

      Delete
  36. some of the regression curves are quite close and even overlapping. So how do I know I am measuring CO@ and not NH4 for instance

    ReplyDelete
    Replies
    1. Hello, as i said in comments above, you can't. That's a chemical sensor that reacts to many gasses. You may just guess which gas only if other gases doesn't changes.

      Delete
  37. This comment has been removed by the author.

    ReplyDelete
  38. Dear Davide

    Could you please answer to me why i do used your lib and PPM is very low.
    I got only 0.2-30 PPM only

    I used lib from this guy
    https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library

    ReplyDelete
    Replies
    1. Hello, At first just power up your sensor, do not use any micro and measure the resistance with a multimeter. Check your resistance to ppm conversion function by hand.

      Delete
    2. Dear Davide

      Can i just do by used 9VDC from VCC pin from arduino ? i just can't convert by hand because i don't know PPM in the air room.

      Reason is i'm not expecting it accurate PPM i just need it to sense of CO2. I will used it in the seal room environment and i have to provide CO2 gas from Co2 take to equal with CO2 from open air.


      Thanks for your help Davide
      Regards,

      Delete
    3. Hello, you have to supply this sensor with 5V, at least 800mW, as stated by the datasheet. Estimate it with open air CO2 if you haven't got any other sensor.

      Delete
  39. can we use mq135 sensor for detecting number of humans in a room using co2 concentrations

    ReplyDelete
    Replies
    1. Hello, to me a NDIR CO2 sensor would be better for that purpose. The MA135 it's a chemical sensor and it's not able to sense CO2 only, it react to various gases.

      Delete
    2. Thank you for your reply... can you give some suggestions for detecting the no. of humans present in a room with the help of other gas sensors(for different gases)(as per me human detection through gas sensor can be done only through concentrations of CO2 might be VOC and dust sensor)

      Delete
    3. Hello, I'm sorry but I've not idea about that, it should take some research to assert if human can be detected and with what accurancy with a gas sensor. I just can say you that a good and cheap NDIR CO2 is the MH-Z14.

      Delete
    4. Hi sir, when i use mq 135 sensor my sensors value drops to 0(zero) after some time . i am using my sensor in a small air conditioned lab (9*3*3)meters with 3 or 4 persons ,so what could be expected CO2 concentration in ppm.(note: sensor is not detecting below 10 ppm, when value is less than 10 ppm it gives zero value)

      Delete
    5. Hello, What's the sensor resistance? Use a meter to check it. Also check the resitance in open air, and various ppm concentrations.

      Delete
  40. This comment has been removed by the author.

    ReplyDelete
  41. hi, I really don't understand how u get the Rs for NH3?? ..i know thw power regression can help me get a and b..but the Rs is using multimeter measure the Rs between the 2 pin's of MQ135 sensor.. When we measure Rs, did the gas sensor need to expose in the 100ppm NH3 or just clean air??

    ReplyDelete
    Replies
    1. Hello, if you are try to calibrate it against NH3, not using the datasheet power regression data, you need to puth the sensor in a close chamber with a well know amount of NH3, then the calibration function works like the CO2. Remember to set the MQ135_DEFAULTPPM as the amount of the NH3 of your closed chamber.

      Delete
  42. Hi, if I burned air with a candle in a glass so all de O2 transforms to CO2. Can we use these air to calibrate the sensor? Is possible that the concentration of CO2 must be known and costant in these situation?

    ReplyDelete
    Replies
    1. To me is difficult to know exactly the concentration of CO2 in that situation, indeed you have to know the initial O2 concentration, the concentration of other gasses, and also candles doesn't stop burning because all the oxygen is gone (take a look here: http://misconceptions.science-book.net/wp-content/uploads/2011/09/Chap2-1.pdf). If you do not live in a big city, CO2 in open air should be something next to 400ppm. I'm not a chemical expert, so it's better ask them.

      Delete
  43. What if I'm measuring at a different Temp and Humidity condition then the above equations won't work (since they are for 20C and 65% RH). So, Sir can you help in achieving the measurement for different temp and humidity values??

    ReplyDelete
    Replies
    1. Hello. Good question, you are right, I did not take into account humidity and temperature drift. That's because they almost gives a .5 deviation on Rs/Ro, which translate in less than 50ppm of accurancy. Indeed the datasheet Fig4 Rs/Ro over degree to me looks like a +.1 drift or less on humidity, and almost a -.08 to +0.8 on temperature below 10 degree. A +-1.0 drift cause less than 100ppm (datasheet Fig3). Because this is not an accurate sensor, and because I use it in indoor environment I don't take in consideration humidity and temperature. Anyway I did not prove the Fig4 drift by correlation with other sensors, so my conjecture about temperature and humidity should be wrong. If you are planning to use it in really cold environment you can add the temperature drift to the conversion formula. If you have further information or make an analysis of this correlation please tell me there. Thank you.

      Delete
    2. Sir, actually I came across this site which is for LPG sensor MQ6, whose link i'll share with you. There, they have used interpolation of the data obtained at their measured values of temperature and humidity with the two equations, one with eqn for 35%RH and other one for 85% RH with varying temperature, to obtain a correction factor which they multiply with their result obtained for 20C and 65%RH. So, I'm a bit confused whether their ppm values obtained are for 20C/ 65%RH or for their current temperature and humidity values which they are using for interpolation. The link is as follows:
      http://www.savvymicrocontrollersolutions.com/arduino.php?topic=arduino-mq6-lpg-gas-sensor

      Just see the function
      " float RsRoAtAmbientTo20C65RH(float RsRo_atAmb, float ambTemp, float ambRH) " in their code where my doubt lies.

      So, sir can you just check and tell whether for which temp/humidity the ppm values calculated actually resemble.

      Delete
    3. They correct the Rs/Ro value read at the actual ambient humidity and temperature, to obtain a Rs/Ro as it was read at 20C 65RH, cause that's the reference of the MQ-6 Fig3 Rs/Ro to ppm correlation datasheet graph.

      Delete
  44. Very interesting and informative blog post! Have you seen the site of Martin Liddament:
    http://www.veetech.org.uk/Prototype_CO2_Monitor.htm
    He describes a method for calibrating the upper end of the CO2 level (for an MG-811 sensor) by blowing into a plastic bag containing the sensor.
    My hesitation with the MQ-135 is that the slopes of the various gasses are practically identical except for the starting and ending points, and because these sensors are not super accurate, I worry that you might be measuring several other pollutants instead of the target (CO2).
    --sam

    ReplyDelete
    Replies
    1. Hello, never read about the 40000ppm calibration method using the average human breath 4% concentration. That's interesting.
      About you hesitations, you can read my "April 9, 2015 at 8:14 PM" blog post response. To me, a good and cheap NDIR CO2 is the MH-Z14, it costs a little more than the MG-811, but it has better accurancy than the chemical resistive MG-811 one. Hope this helps.

      Delete
  45. hi, i want to calculate ppm value for MQ7 CO sensor.
    This is link for product: http://www.amazon.in/gp/product/B00NR2KXSC?psc=1&redirect=true&ref_=oh_aui_detailpage_o00_s00#productDetails
    This is the MQ7 datasheet: https://www.pololu.com/file/download/MQ7.pdf?file_id=0J313

    From this sensor , how to find Rs, Rl value and ppm
    any idea

    ReplyDelete
    Replies
    1. Hello, just like I've done for the CO2 value of this sensor, take point from the "Fig.3 sensitivity characteristics of the MQ-7" graph. Using power regression you will get scaling factor (a), and exponent (b), then you can get the Ro.

      Delete
  46. Hello Davide,
    In your post: "For CO2, if we measure points graph and do power regression we can obtain the function ppm=116.6020682(Rs/Ro)^-2.769034857"
    How did you calculate a=116.6020682, and b=-2.769034857.
    Please explain me clearly. Thanks you so much !!!

    ReplyDelete
    Replies
    1. See post above. Take point from the datasheet Rs/Ro versus ppm figure, and do power regression. You can find a few online tools that compute this for you, just inserting point that you get from graph. A more accurate way to get point is to use a matlab script to interpolate the graph, like I've done here: http://davidegironi.blogspot.it/2013/06/irradianceilluminance-meter-using.html#.VsTUE7ThCUk (you can find the script as an helper package).

      Delete
    2. Thanks you guys !!!
      And how do I turn on/off a sensor in a time? Ex: turn off in 15 minutes, then turn on.
      Thanks you so much.

      Delete
    3. You can use a transistor as a switch on the power line of the sensor, the only think you have to keep in mind is that the transistor should tolerate be at least 500mA of DC current. Also that this sensor should be on for at least 24 hours as datasheet states in order to have consistent readings.

      Delete
    4. My mean is use code to turn on/off. Can I change pinMode to OUTPUT to turn off, ex:

      void TurnoffInTime(int analogpin, float time){
      pinMode(analogpin, OUTPUT);
      delay(time);
      pinMode(analogpin, INPUT);
      }
      Thanks you so much !!!

      Delete
    5. The simple solutions is leave the pin as input, just do not read and compute it to ppm.

      Delete
    6. Thanks brother. Have a nice day =))

      Delete
  47. can i have some test code for mq 135 with arduino uno r3
    nh3, benzen,smoke iwould like to detect.please help

    ReplyDelete
    Replies
    1. Hello, I've not an arduino library, indeed you can find the code for atmega above. It will be simple to convert it to Arduino. You have to do power regression or correlation to obtain factor for other gas than CO2 and NH3, the value for those gas can be found in the library in this post.

      Delete
  48. This comment has been removed by the author.

    ReplyDelete
  49. Didn't find in any sources, what gases sensitivity characteristics are shown on diagram from datasheet. Only NH4, CO, CO2 are recognisable, 3 other are scrambled.

    Is there any correct sensor sensitivity diagram?

    ReplyDelete
    Replies
    1. I've asked this question times ago, but i can get reply from any sellers. I've found a chinese datasheet and tryied to translate those values, take a look here: http://uploadpie.com/lSumf (link will expire in 1 week). If you find further information on scrambled gas please write it down here.

      Delete
  50. Hello,
    I have read the sensitivity characteristics curve in datasheet of MQ135 and I don't know AL R, 3/4, 1/4* and +- mean? Can you help me, please !!!
    NH4 represent for NH3, doesn't it ?
    Thank you so much !!!

    ReplyDelete
    Replies
    1. Hello. That are simply scrambled characters. Take a look to my reply comment above your here on comment February 28, 2016 at 2:11 PM. It may be interesting. I suppose the NH3 is the reference gas but for same reason, they write NH4 on datasheet Fig.2.

      Delete
    2. Thank you so much !!! Have a nice day !!!

      Delete
  51. sir,how to convert the raw value obtained from ADC into a resistance value(Rs).kindly help me

    ReplyDelete
    Replies
    1. Hello, TLRT; resval = (1024*balanceresistor)/adc-balanceresistor);
      You can take a look to eq4 here for a fully explanention: https://www.maximintegrated.com/en/app-notes/index.mvp/id/1753
      ADC in ATmega usually has 10bit resolution, the balance resistor in this page sample is 22k (22000).

      Delete
  52. Hi Davide,
    I have an CO2 MG811 sensor. I don't know where tcm pin connect with ?

    See it: http://forum.mysensors.org/uploads/files/1457101160426-f08406-co2-carbon-dioxide-sensor-module-font-b-mg811-b-font-voltage-0-2v-voltage-output.jpg

    Please help me. Thank you so much !!!!

    ReplyDelete
    Replies
    1. Hello, if you use the MG811 over a preamp board like this, it depends on the board circuit, usually you have to connect Vcc to your power supply VCC (+), ask the board seller which voltage and current does the board request, i suppose it will be 6V 300mA, Gnd to ground (-), and Aout straight in you micro ADC input. But i repeat it depends on the board schematics.

      Delete
    2. Thank you. But I found the circuit map, it is very complex. Do you understand this circuit map?
      I think that Vcc->5V, GND->GND, Aout->Analog pin, Dout->Digital pin. But I worry that it'll fail when set up like I think.
      Datasheet:http://www.mouser.com/ds/2/321/CO2SensorDatasheetMG811-17599.pdf
      I also ask the seller but no response.
      Thank you so much !!!

      Delete
    3. Sr. The circuit map: http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2568&stc=1&d=1210174233

      Delete
    4. It's a non inverting amplifier with gain given by G=1+(R3+R2)/R4. But i doubt that's something like you board, is not usual to have boards for that sensors with + and - voltage power requirements.

      Delete
    5. Thank you so much !!! Good job, Good boy =))

      Delete
  53. Thank you so much,sir.I also using MQ135 with Atmega16,an 8 bit controller to sense CO2.I got the ADC raw value.I have to convert it into resistance value(Rs).what is the balance resistor value for this 8 bit controller?

    ReplyDelete
    Replies
    1. Hello, for this specific case is 22k, but it depends on sensors, and readings you have to get. You can take a look here for an in deep explanation: http://electronics.stackexchange.com/questions/70009/why-use-a-pull-down-resistor-with-a-ldr-and-microcontroller

      Delete
  54. As u mentioned above,Referring to eqn 4,the resistance eqn will be like
    Resistance=(((2^N)*balance resistance)-(ADC*balance resistance))/ADC.
    But its not matching with eqn u have posted and I'm getting resistance value in negative, if I use the eqn u have posted.Also Wat is TLRT?awaiting for ur reply,sir.

    ReplyDelete
    Replies
    1. Eq4 is Res = ((2^N/adc) - 1)*balanceresistor = (2^N/adc)*balanceresistor - balanceresistor = (1024/adc)*balanceresistor - balanceresistor = 1024*balanceresistor/adc - balanceresistor. Sorry, typo error, it was TLTR (too long too read)

      Delete
  55. Since I am not very good in math, I would like to ask this.
    My module had a 10k resistor onboard, I could replace it with 22k but re-soldering might damage the board. What would I need to change in the code if I use 10k instead of 22k?

    ReplyDelete
    Replies
    1. Hello. Using my sample, just the #define MQ135_PULLDOWNRES 22000 line to 10000. But if you have a board, it might be that it gain up the signal. Check if there's an opamp, if so, you might do some change to my code. Cause the signal is gained by the board.

      Delete
    2. Hi Davide, Thank you for that hint!
      I am using this board: http://www.ebay.com/itm/201499458847
      As far as I understand, there is no opamp.
      The little chip should be for the (unused) D-out pin and from the resistors I have replaced the "102"(1k) resistor with a 10k resistor. Do you think I only need to change that one #define then?

      Delete
    3. Yes, I do it without problems. The little chip you mention is the opamp and comparator for the d-out. You can adjust the level you want for a "1" output in the D-out. For the A-out the is only the pull up resistor 1k, I also repalace it with another 15k smd resistor for better response.

      Delete
    4. Thank you for sharing this information!

      Delete
    5. This comment has been removed by the author.

      Delete
  56. (ignore my previous reaction, I have deleted it)
    I am using your code with Arduino. For that I had to change uint16_t to unsigned int (which is 16 bit).

    I let the module pre-heat for a couple of hours and then put it outside for a while, then I take the ro value of 47587 to the MQ135_DEFAULTRO. For MQ135_DEFAULTPPM I then use 400.

    When I then place it inside my home, It says:
    ADC: 525
    RES: 9504
    ro: 14833
    ppm: 10089.58
    Which hopefully cannot be true! (ppm > 10.000!?)

    ReplyDelete
    Replies
    1. That resistence value seems a little odd to me, i mean it's too loo. In open air, so next to 400ppm, You should get something like 25000ohm. Can with a multimeter the resistance value output of your sensor.

      Delete
    2. I modiefied the board, so I can provide 5V to the heater without the 5V on the A/B pins. Now I can measure the reistance with a simple multimeter.

      It is a bit windy outide (and only 10 degrees) so it fluctuates, outside is 8-10k. Inside our house it is 6.5-7k.

      Delete
    3. Oh boy I found the problem...

      Our Chinese friends wanted to make the module more fun, by adding a chip that makes a LED turn on when CO2 goes above a certain limit.

      But now I discovered that the addition has a very (bad) influence on the measured resistance! (simply because the input of the chip has a lower resistance)

      After partially removing the chip, I now measure more than 20k outside. Back to calibrating the code...

      Delete
    4. Newsflash: :)
      The module with your code on an Arduino now comes with 1200ppm inside. I will need to do better calibration later (because it actually is too cold outside now), but I am already very happy.
      Thanks Davide!

      Delete
  57. Hi Davide:

    Thank you for providing help on sensor projects. I"m using the MQ 131, for ozone. If you have used this sensor, what would be a good resistance to use for the 48 hour burn in period?

    ReplyDelete
    Replies
    1. Hello, I'm sorry but I've never used the MQ131. From the datasheet it seems that it works almost like the MQ135, so you just have to do correlation or power regression to obtain conversion values.

      Delete
    2. Honestly, I'm lost as to where the resistor goes on the MQ sensors. Some illustrations showing it going parallel across the Analog output and ground. Others show the resistor going in series from the analog output to your datalogger. I've tried it both ways, but the output runs on an arduino serial from 22 or so to the max of 1023, swinging back from low to high.

      Delete
    3. The first step I will do is to heat the sensor and measure the resistance between A and/or B. Datasheet states that it should be something between 100K and 200K for 50ppb O3. You have to heat the circuit with 6V, as sheet said. So, at first try to heat and measure it with a multimeter. Does it works? If you could connect the sensor to a micro. Connect one H pin to GND, the other H pin to 6V. Then connect one B pin to 5V, the opposite A pin to the and ADC pin of your micro, and to a 22k resistor in pull down (i.e. to GND). Then you should be able to read the same resistance you get from multimeter in your micro. Hope this helps.

      Delete
  58. Actually I am finding MQ135 in Proteus 8 software to interface it with AT89C51 but I am not getting it can u plz help me

    ReplyDelete
    Replies
    1. Hello, I'm sorry but I do not use Proteus nor have any AT89C51 to test this library against. Also note that I've delete you 4 comments asking the same thing, please do not ask same thing many times.

      Delete
  59. when I do power regression using excel, I don't get even close to your values, could you elaborate a little more on how you ended up with those values after your power regression? thanks

    ReplyDelete
    Replies
    1. Hello, I'm sorry but I've never use Excel for power regression. I've used matlab. Also there are a lot of powerfull online tools that can compute it.

      Delete
  60. dear sir,
    when i read analog pin of mq-135 sensor module it gives reading to me like only 150-200 etc. this is the resistance value or count like(0-255).my module has 4 pin like analog,digital,vcc and ground

    ReplyDelete
    Replies
    1. Hello. Read the output of the sensor with a multimeter? Then check your adc to resistance formula with some know values resistor from 10K to 100K. Then try to replace the know value resistor with the sensor. Please do not make multiple comments with the same request, I've deleted your duplicate comment.

      Delete
    2. sir please say about adc to resistance formula.

      Delete
    3. Hello, you can find it in the adc library. It's a fairly basic formula. Here you can find a good explanation of that: http://electronics.stackexchange.com/questions/70009/why-use-a-pull-down-resistor-with-a-ldr-and-microcontroller

      Delete
  61. in adc conversison what is balance resistor and how we get this? thanks

    ReplyDelete
  62. Hi Davide,

    Thanks for sharing the knowledge of MQ135, it helps me a lot in my project. However, I still a little bit confuse with the Ro and Rs. May I know that whether the Ro and Rs is the same as the link below? Thanks! (https://drive.google.com/file/d/0B4_2jFMdHQBjSUJodmtBOTN6ck0/view?usp=sharing)

    ReplyDelete
    Replies
    1. Hello. No it's not the same. Ro is a constant. The one between "A or B" and "A or B" is Rs, Rl is the pull-down resistor.

      Delete
    2. Oh I see, I will try it soon. Thanks!

      Delete
  63. Hi Davide,
    Thanks for your sharing. After I read your programs and your founding, I final understand how to get the ppm value for each gas.
    However,do you know what is the meaning of the reading from Analog Output of the senser?
    And from the fig. 2 above (the graph with 7 curves), I can read the words of 4th, 6th and 7th curve, would you mind telling me which kind of gases are they?

    Thank you :D

    ReplyDelete
    Replies
    1. Hello. I do not understand what's the "reading from Analog Output of the senser" about, sorry, where did you read this? About the other gasses, take a look at my comment here on "February 29, 2016 at 11:35 PM".

      Delete
    2. The sensor has two output, right? one is AOUT and the other one is DOUT, I would like to know what is the value (the raw data) that I get the sensor stands for?

      Delete
    3. And for the rest 3 gases in the fig.2, I found that the 6th gas is Toluene and the 7th gas is Beneze. Even the chinese version, it's still to blur to read the 4th gas, so I can't get what it is.
      Hope this information can help you :)
      Btw, I am the "Unknown April 26, 2016 at 6:12 AM"

      Delete
    4. This sensor has one output, it's a resistance output sensor. We measure the resistance using a pulldown resistor. The sensor board can have an analogic (AOUT) and a digital (DOUT) output. Usually the digital output is on over a certain threshold. To measure this sensor by using a board, you have to know what's the board gain (if it's a simple opamp based board), or more in general which is the board schematics.
      You have to sorry me, but i forgot that I've already try a translation. To me the gasses sems the one in this figure: http://pho.to/A7w8U - But I'm not chinese, so i can be wronk. How do you get that translations, are you chinese? Thank you for helping us in finding the "unkwnow gases".

      Delete
    5. This comment has been removed by the author.

      Delete
    6. Thanks for your reply, I am able to obtain the CO2 using this sensor on Arduino (but cannot be sure that the reading is correct, since the readings keep "jumping"), and trying to get the values of other pollutants.
      Yes, I am a chinese. As I said in the above comment, the last two gases are Toluene and Benzene.(From the same figure on other website) And now I finally recognized the words that the 3rd gas (below CO) should be Alcohol.

      Delete
    7. Great! And thank you! I had try the translation using a list of gases in chinese, something difficult for one like me that does not understand chinese :) So, if you can confirm me that gases are like this figure: http://pho.to/A80hO - I will edit the post, and if you give me permission i will write your name just below the picture, as text, not inside the picture.
      About the jumping. How much is that jumping in terms of resistance leaving the sensor in open air? Have you tryied using other sensor?

      Delete
  64. Hi Davide!
    I´m using the MQ135 sensor for measuring CO2 gas in an arduino board and while using the code above we have some questions.
    1.-The return value for GetRZero function is 270 kOhm for us, and you got 41 KOhm, is it possible? or is it just wrong? With this obtained value our PPM is around 20, should we change the parameters?
    2.- Once we heated the sensor , shall we heat it every time that we re gonna use it?
    Thank you very much!

    ReplyDelete
    Replies
    1. Hello,
      1) Check the sensor resistance with a multimeter, does it fit what you get in your micro?
      2) The datasheet does not tell much about this. I pre-heat it every time i disconnect the sensor heater for a while.

      Delete
  65. hello!
    Should we check the resistance once heated? or before preheating?
    Gracie mille

    ReplyDelete
  66. Hi bro I really admire your research about this, and your friendly and spontaneous answer, but I would like you to see my blog with my respective research about the MQ135 and all the equations for all gases http://angeloloza.blogspot.mx/, and my question is about the potentiometer included in the mq135 board, I think it is for sensivity, but I dont know how it works, do you know something about? thanks for all bro!

    ReplyDelete
    Replies
    1. Good post Programmer.
      About your question the potentiometer is only to select the limit for the digital output be on. For example you can configure the potentiometer for 1000 ppm CO2 or for 5000 ppm CO2. Is like an alarm. All the sensor of this type has the same functionality, light, temperatures, etc.. The potentiometer fix the limit for the digital output.

      Delete
    2. Oh I thought in that , because when I put it to max starts lighting, thank you!

      Delete
    3. Hello, thank you for your comment.
      Oscar tells you about the potentiomenter, just an addition, if you use a board with an opamp that boost your analog sensor signal, consider that gain.
      You've made a great job. You can find my tought about your method on your blog post.
      As an addition, I had a post on my blogger draft about finding out values for the MQ sensors. I hope I will be able to end it up, and post it.
      About the gasses translation, according to "King AoIeong", which is chinese, the last gasses is "Benzene". Look at post "Unknown April 26, 2016 at 6:12 AM" and replyes, the last one is the translated figure.
      Let me know what do you think about the translation, so we will find out which is that "mysterious" gas :)

      Delete
  67. This comment has been removed by the author.

    ReplyDelete
  68. Sir,could you please tell us the default R0 for gases present in MQ135 sensor i.e;
    1.Benzene(C6H6),
    2.Ammonia(NH3),
    3.Oxides of nitrogen(NOx)and
    4.Alcohol(C2H2OH).
    Looking forward for your reply,this could help us a lot sir!!

    ReplyDelete