Example Code for Arduino-CO2 Sensing
Last revision 2025/12/16
Explore how to measure CO2 levels using Arduino and DFRobot's CO2 sensor with detailed hardware and software setup, calibration, and sample code for precise readings.
Hardware Preparation
- Arduino board (e.g., Arduino Uno) x 1
- DFRobot CO2 Sensor Module (SEN0159) x 1
- External power supply (7~12V) x 1
- Buzzer (optional) x 1
- Jumper wires x several
Software Preparation
- Arduino IDE (download from Arduino Official Website)
- No additional libraries are required for the sample code.
Wiring Diagram

Calibration
This module is an electrochemistry sensor, you should calibrate it before actual measurement.
You should provide stable power to this module, and the sensor will heatup while working. Please put this module into an area where the air is clean. After continuous working for about 48 hours, you can measure the output voltage of this module. Then modify the defination in the code with the voltage value(unit:V) divide by 8.5.
#define ZERO_POINT_VOLTAGE (voltage/8.5)
For example, the voltage you measured from the module is 2.4V, then 2.4/8.5=0.282. So modify the defination as below:
#define ZERO_POINT_VOLTAGE (0.282)
After the modification, upload the sample code to your Arduino board.
Sample Code
You need to set potentiometer onboard to the threshold value. Just make the red led turn off. With the CO2 concentration is enough high to make the sensor output voltage higher than threshold value,the led will be turned on. If you connect a buzzer to the module(right side), you will hear the alarm.
/*******************Demo for MG-811 Gas Sensor Module V1.1*****************************
Author: Tiequan Shao: [email protected]
Peng Wei: [email protected]
Lisence: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
Note: This piece of source code is supposed to be used as a demostration ONLY. More
sophisticated calibration is required for industrial field application.
Sandbox Electronics 2012-05-31
************************************************************************************/
/************************Hardware Related Macros************************************/
#define MG_PIN (A0) //define which analog input channel you are going to use
#define BOOL_PIN (2)
#define DC_GAIN (8.5) //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.220) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define REACTION_VOLTGAE (0.030) //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); //UART setup, baudrate = 9600bps
pinMode(BOOL_PIN, INPUT); //set pin to input
digitalWrite(BOOL_PIN, HIGH); //turn on pullup resistors
Serial.print("MG-811 Demostration\n");
}
void loop()
{
int percentage;
float volts;
volts = MGRead(MG_PIN);
Serial.print( "SEN0159:" );
Serial.print(volts);
Serial.print( "V " );
percentage = MGGetPercentage(volts,CO2Curve);
Serial.print("CO2:");
if (percentage == -1) {
Serial.print( "<400" );
} else {
Serial.print(percentage);
}
Serial.print( "ppm" );
Serial.print("\n");
if (digitalRead(BOOL_PIN) ){
Serial.print( "=====BOOL is HIGH======" );
} else {
Serial.print( "=====BOOL is LOW======" );
}
Serial.print("\n");
delay(500);
}
/***************************** 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]);
}
}
Result
When the code is uploaded to the Arduino board, open the serial monitor (baud rate 9600) to view the output. The expected results include:
- Voltage Reading: "SEN0159: X.XX V" (where X.XX is the measured voltage from the sensor).
- CO2 Concentration: "CO2: <400 ppm" (if below 400 ppm) or "CO2: XXX ppm" (if above 400 ppm).
- Digital Pin Status: "=====BOOL is HIGH======" (when CO2 is below threshold) or "=====BOOL is LOW======" (when CO2 exceeds threshold).
External power supply (7~12V) is necessary to supply the microcontroller board when using this CO2 sensor module.
Sensor Type: This module is an electrochemical sensor, which requires calibration to ensure accurate measurements.
Potentiometer Adjustment: Set the potentiometer onboard to the threshold value by turning it until the red LED turns off. The LED will turn on when CO2 concentration is high enough to make the sensor output voltage exceed the threshold.
Was this article helpful?
