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:
update - Here a temperature and humidity correlation method for the MQ sensors;

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

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

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


  • read risk disclaimer
  • excuse my bad english

Monday, January 6, 2014

CO2 meter using NDIR infrared MH-Z14 sensor library for AVR ATmega

MH-Z14 NDIR Infrared gas module is a common type, small size sensor, using non-dispersive infrared (NDIR) principle to detect the existence of CO2 in the air, with good selectivity, non-oxygen dependant and long life.

MH-Z14 has a PWM output, with a sensitivity range of 0ppm to 2000ppm CO2, an accurancy of ±200ppm.
The cycle is 1004ms±5%, given the duty cicle Th (pulse high), Tl is 1004-Th, we can convert it to CO2 value using the formula:
CO2ppm = 2000 * (Th - 2ms) /(Th + Tl - 4ms)

This library read PWM output, and convert it to CO2ppm.

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 8MHz


  • read risk disclaimer
  • excuse my bad english