https://www.aliexpress.com/item/MG811-CO2-Carbon-Dioxide-Gas-Sensor-Module-Detector-with-Analog-Signal-Output/32662785980.html
Hello,
I'm trying to measure CO2 concentrations with this sensor (link above). I have a 6V battery connected to a breadboard. I connected VDD pin of the module with the 5V pin on arduino, the GND to GND on arduino and the AOUT on A0 pin on arduino. Also, I connected the Vin pin of arduino to the 6V on breadboard with the respective grounds. I used the sample code given in (MG-811 CO2 Sensor Module | Sandbox Electronics) and I'm getting very high or negative values of ppm. I tried to change the ZERO_POINT_VOLTAGE and the REACTION_VOLTAGE in the code. I did get some reasonable values but when I moved the sensor into clean air, the values didn't drop to approximately 400 ppm. I would like to ask what I've been doing wrong cause it's my first time I'm dealing with arduino projects.
/************************Hardware Related Macros************************************/
#define MG_PIN (A0) //define which analog input channel you are going to use
#define DC_GAIN (6) //define the DC gain of amplifier
/***********************Software Related Macros************************************/
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interval(in milisecond) between each samples in
//normal operation
/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should derermine this value.
#define ZERO_POINT_VOLTAGE (0.324) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define REACTION_VOLTGAE (0.20) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
/*****************************Globals***********************************************/
float CO2Curve[3] = {2.602, ZERO_POINT_VOLTAGE, (REACTION_VOLTGAE / (2.602 - 3))};
//two points are taken from the curve.
//with these two points, a line is formed which is
//"approximately equivalent" to the original curve.
//data format:{ x, y, slope}; point1: (lg400, 0.324), point2: (lg4000, 0.280)
//slope = ( reaction voltage ) / (log400 –log1000)
void setup()
{
Serial.begin(9600);
}
void loop()
{
int percentage;
float volts;
volts = MGRead(MG_PIN);
percentage = MGGetPercentage(volts, CO2Curve);
Serial.print("CO2=");
Serial.println(percentage);
Serial.println(volts);
delay(1000);
}
/***************************** MGRead *********************************************
Input: mg_pin - analog channel
Output: output of SEN-000007
Remarks: This function reads the output of SEN-000007
************************************************************************************/
float MGRead(int mg_pin)
{
int i;
float v = 0;
for (i = 0; i < READ_SAMPLE_TIMES; i++) {
v += analogRead(mg_pin);
delay(READ_SAMPLE_INTERVAL);
}
v = (v / READ_SAMPLE_TIMES) * 5 / 1024 ;
return v;
}
/***************************** MQGetPercentage **********************************
Input: volts - SEN-000007 output measured in volts
pcurve - pointer to the curve of the target gas
Output: ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
of the line could be derived if y(MG-811 output) is provided. As it is a
logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
value.
************************************************************************************/
int MGGetPercentage(float volts, float *pcurve)
{
if ((volts / DC_GAIN ) >= ZERO_POINT_VOLTAGE) {
return -1;
} else {
return pow(10, ((volts / DC_GAIN) - pcurve[1]) / pcurve[2] + pcurve[0]);
}
}