Based on robtillaart's approach:
// GAS SENSOR MQ-2
// This sensor can detect smoke, methane, carbon dioxide and other gases
//VARIABLES
float Ro = 10000.0; // this has to be tuned 10K Ohm
int sensorPin = 0; // select the input pin for the sensor
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
float Vrl = 0.0;
float Rs = 0.0;
float ratio = 0.0;
// SETUP
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // initialize serial communication with computer
// analogReference(EXTERNAL);
}
// get CO ppm
float get_CO (float ratio){
float ppm = 0.0;
ppm = 37143 * pow (ratio, -3.178);
return ppm;
}
// LOOP
void loop() {
val = analogRead(sensorPin); // read the value from the analog sensor
Serial.println(val); // send it to the computer (as ASCII digits)
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(100); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(100); // stop the program for some time
Vrl = val * ( 5.00 / 1024.0 ); // V
Rs = 20000 * ( 5.00 - Vrl) / Vrl ; // Ohm
ratio = Rs/Ro;
Serial.print ( "Vrl / Rs / ratio:");
Serial.print (Vrl);
Serial.print(" ");
Serial.print (Rs);
Serial.print(" ");
Serial.println(ratio);
Serial.print ( "CO ppm :");
Serial.println(get_CO(ratio));
delay(10000);
}
With the following to mention and ask:
- Ro has somehow to be figured out and I can't imagine how to make the tuning
- Rl suggested as 20000ohms but has to be tuned as well
- I preferred to make a correlation here instead of LUT. I made a table with x's and y's out of the graph and correlated the results
Rs/Ro ppm
5 200
4 500
3,5 800
3 1000
2,5 2000
2,2 3000
1,9 5000
1,5 10000
then when you correlate you get y = 37143x-3,178 with a R² = 0,9948
What do you all think?