Electrical – How to calculate Gas sensor PPM from analog readings

analogarduinogas-sensor

I am new to Arduino and electronics in general.I am working on MQ 7 Gas sensor to test Carbon monoxide level. But the issue is i still can't grasp the concept of converting analog readings to PPM. I tried to follow this ,but some parts, are very clear especially R0 = RS_air/(26+(1/3))

So far with my code below the ratio I get is between 20 and 30. Can someone help me complete the PPM calculation or point me in the right direction.Here is the Data sheet

Thank you in advance

int gasAnalogPin = A0;

void setup() {
  Serial.begin(9600);

  pinMode(gasAnalogPin, INPUT);

}

void loop() {

 float sensor_volt;
 float RS_air;
 float R0; 
 float sensorValue = 0;


  // A) preparation
    // turn the heater fully on
    analogWrite(gasAnalogPin, HIGH); 
    // heat for 1 min
    delay(60000);

    // now reducing the heating power: turn the heater to approx 1,4V
    analogWrite(gasAnalogPin, 286.72); // 255x1400/5000
     // heat for 90 sec
    delay(90000);


  // B) reading    
    // CO2 via MQ7
    analogWrite(gasAnalogPin, HIGH); 
    delay(50); // Getting an analog read apparently takes 100uSec

  for(int i = 0; i <= 100; i++){
    sensorValue = sensorValue + analogRead(gasAnalogPin);

   }  
 sensorValue = sensorValue/100.0; //get the avarage value
 sensor_volt = sensorValue/1024*5.0;
 RS_air = (5.0-sensor_volt)/sensor_volt;
 R0 = RS_air/(26+(1/3)); // Not sure how they came up with this ?


 float RS_gas = 0;
 float ratio = 0;

  sensor_volt = 0;
  sensorValue = 0;
  sensor_volt = 0;

  sensorValue = analogRead(gasAnalogPin);
  sensor_volt = sensorValue/1024*5.0;
  RS_gas = (5.0-sensor_volt)/sensor_volt;
  ratio = RS_gas/R0; //Replace R0 with the value found using the sketch above

  Serial.print("PPM:"); // How to calculate PPM?


}

Best Answer

You misunderstood the referenced article. The two code snippets are not supposed to work together in a loop.

First code is control. It reads sensor 100 times, calculates average and prints it out. This should be done in normal air conditions, i.e. with detectable gas concentrations at their safe levels. You take the printed value and place it into second code.

The second code is what actually supposed to run continuously in a loop. It reads sensor and compares it to previously obtained control value. This gives you current gas concentration relative to normal conditions.

Note, that better way to do this is to replace analogRead in the second code with inner loop, similar to the first, to get an average of 10-50 consecutive readings instead of a single read.

UPDATE

In order to calculate current PPM you need to find sensor resistance RS first. Normally it is done by simple formula for voltage divider: RS/RL = (V-Vs)/Vs, where RL is load resistor. When measured in clean air the calculated RS becomes R0. The first code uses this equation, however I have no idea why they divide this by some constant. IMHO the formula for ratio should be simple RS/R0 = RS_gas/RS_air. I suspect it is supposed to represent the gain of OP Amp used in their circuit somehow, but it would be pointless since the constant should be present in second code too, where they would cancel each other.

Regarding PPM calculation, first thing you should notice is that the relationship between RS/R0 and PPM is given on log-log scale. The second thing is that the graph in this scale is approximately linear. There are several methods to deal with this.

You can take the graph from datasheet and plot it on linear scale. The result would be exponential curve and you can use various tools to find exponential fit for it. This article has examples of exponential approximation and a code for some sensors.

You can also treat the graph as straight line y = ax + b but substitute x and y for their logarithms: log(y) = a * log(x) + b. Then you can use points on the graph in datasheet to find out slope (a) and offset (b). This seems to be exactly what the author of the referenced article is doing (although I am not sure the resulting formula is correct).

Finally, it seems the article is missing very important part of the functionality - the variable voltage supply. The circuit is supposed to switch between 1.5V and 5V for sensor to operate properly, according to datasheet. The measurements are done at low voltage, then it is heated up to clean adsorbed gases.