Interfacing Gravity Infrared CO2 Sensor with Arduino to Measure Carbon Dioxide in PPM

Published  November 2, 2020   1
Interfacing Gravity Infrared CO2 Sensor with Arduino

The increasing concentration of carbon dioxide in the air has become a serious problem now. According to the NOAA report, the ozone CO2 concentration has reached 0.0385 percent (385 ppm) and it is the highest amount in 2.1 million years. This means that in one million particles of air, there are 385 particles of carbon dioxide. This rising level of CO2 has affected the environment badly and led us to face the situation like climate change and global warming. There are many air quality measuring devices installed on roads to tell the CO2 level, but we can also build a DIY CO2 measuring device and can install it in our area.

In this tutorial, we are going to interface the Gravity Infrared CO2 Sensor with Arduino to measure the CO2 concentration in PPM. Gravity Infrared CO2 Sensor is a high precision analog CO2 sensor. It measures the CO2 content in the range of 0 to 5000 ppm. You can also check our previous projects where we used the MQ135 Gas sensor, Sharp GP2Y1014AU0F Sensor, and Nova PM Sensor SDS011 to build an air quality monitor.

Components Required

  • Arduino Nano
  • Gravity Infrared CO2 Sensor V1.1
  • Jumper Wires
  • 0.96’ SPI OLED Display Module
  • Breadboard

Gravity Infrared CO2 Sensor

Gravity Infrared CO2 Sensor

Gravity Infrared CO2 Sensor V1.1 is the latest high-precision analog infrared CO2 sensor released by DFRobot. This sensor is based on non-dispersive infrared (NDIR) technology and has good selectivity and oxygen-free dependency. It integrates temperature compensation and supports DAC output. The effective measuring range of this sensor is from 0 to 5000ppm with an accuracy of ± 50ppm + 3%. This Infrared CO2 Sensor can be used in HVAC, indoor air quality monitoring, industrial process, and security protection monitoring, agriculture, and animal husbandry production process monitoring.

 

Infrared CO2 Sensor Pinout:

As mentioned earlier, the Infrared CO2 Sensor comes with a 3-pin connector. The below figure and table shows the pin assignments for the Infrared CO2 Sensor:

Infrared CO2 Sensor Pinout

Pin No.

Pin Name

Description

1

Signal

Analog Output (0.4~2V)

2

VCC

VCC (4.5~5.5V)

3

GND

GND

Infrared CO2 Sensor Specifications & Features:

  • Gas Detection: Carbon Dioxide (CO2)
  • Operating Voltage: 4.5 ~ 5.5V DC
  • Preheating Time: 3min
  • Response Time: 120s
  • Operating Temperature: 0 ~ 50 ℃
  • Operating Humidity: 0 ~ 95% RH (no condensation)
  • Waterproof and anti-corrosion
  • High cycle life
  • Anti-water vapor interference

0.96’ OLED Display Module

OLED (Organic Light-Emitting Diodes) is a self light-emitting technology, constructed by placing a series of organic thin films between two conductors. A bright light is produced when an electric current is applied to these films. OLEDs are using the same technology as televisions, but have fewer pixels than in most of our TVs.

OLED Display Module

For this project, we are using a Monochrome 7-pin SSD1306 0.96” OLED display. It can work on three different communications Protocols: SPI 3 Wire mode, SPI four-wire mode, and I2C mode. The pins and its functions are explained in the table below:

We have already covered OLED and its types in detail in the previous article.

Pin Name

Other Names

Description

Gnd

Ground

Ground pin of the module

Vdd

Vcc, 5V

Power pin (3-5V tolerable)

SCK

D0,SCL,CLK

Acts as the clock pin. Used for both I2C and SPI

SDA

D1,MOSI

Data pin of the module. Used for both IIC and SPI

RES

RST, RESET

Resets the module (useful during SPI)

DC

A0

Data Command pin. Used for SPI protocol

CS

Chip Select

Useful when more than one module is used under SPI protocol

OLED Specifications:

  • OLED Driver IC: SSD1306
  • Resolution: 128 x 64
  • Visual Angle: >160°
  • Input Voltage: 3.3V ~ 6V
  • Pixel Colour: Blue
  • Working temperature: -30°C ~ 70°C

Learn more about OLED and its interfacing with different microcontrollers by following the link.

Circuit Diagram

Circuit Diagram for interfacing Gravity Analog Infrared CO2 Sensor with Arduino is given below:

Interfacing Gravity Infrared CO2 Sensor with Arduino

The circuit is very simple as we are only connecting Gravity Infrared CO2 Sensor and OLED Display module with Arduino Nano. Infrared CO2 Sensor and OLED Display module both are powered with +5V and GND. The Signal (Analog Out) pin of the CO2 sensor is connected to the A0 pin of Arduino Nano. Since the OLED Display module uses SPI communication, we have established an SPI communication between the OLED module and Arduino Nano. The connections are shown in the below table:

S.No

OLED Module Pin

Arduino Pin

1

GND

Ground

2

VCC

5V

3

D0

10

4

D1

9

5

RES

13

6

DC

11

7

CS

12

After connecting the hardware according to the circuit diagram, it should look something like below:

Arduino Interfacing with Gravity Infrared CO2 Sensor and OLED

Arduino Code to Measure CO2 Concentration

The complete code for this Gravity Infrared CO2 Sensor with Arduino project is given at the end of the document. Here we are explaining some important parts of the code.

The code uses the Adafruit_GFX, and Adafruit_SSD1306 libraries. These libraries can be downloaded from the Library Manager in the Arduino IDE and install it from there. For that, open the Arduino IDE and go to Sketch > Include Library > Manage Libraries. Now search for Adafruit GFX and install the Adafruit GFX library by Adafruit.

Adafruit GFX Library

Similarly, install the Adafruit SSD1306 libraries by Adafruit. The Infrared CO2 sensor doesn’t require any library as we are reading the voltage values directly from the analog pin of Arduino.

After installing the libraries to Arduino IDE, start the code by including the needed library files. Dust sensor doesn’t require any library as reading is taken directly from the analog pin of Arduino.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Then, define the OLED width and height. In this project, we’re using a 128×64 SPI OLED display. You can change the SCREEN_WIDTH, and SCREEN_HEIGHT variables according to your display.

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Then define the SPI communication pins where OLED Display is connected.

#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13

Then, create an Adafruit display instance with the width and height defined earlier with the SPI communication protocol.

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

After that, define the Arduino pin where the CO2 sensor is connected.

int sensorIn = A0;

Now inside the setup() function, initialize the Serial Monitor at a baud rate of 9600 for debugging purposes. Also, Initialize the OLED display with the begin() function.

Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC);
analogReference(DEFAULT);

Inside the loop() function, first read the signal values at the Analog pin of Arduino by calling the analogRead() function. After that, convert these analog signal values to voltage values.

void loop(){
  int sensorValue = analogRead(sensorIn);
  float voltage = sensorValue*(5000/1024.0);

After that, compare the voltage values. If the voltage is 0 V, it means that some problem has occurred with the sensor. If voltage is greater than 0 V but lesser than 400 V, then it means that the sensor is still in the pre-heating process.

if(voltage == 0)
  {
    Serial.println("Fault");
  }
  else if(voltage < 400)
  {
    Serial.println("preheating");
  }

If the voltage is equal or greater than 400 V, then convert it to CO2 concentration values. 

else
  {
    int voltage_diference=voltage-400;
    float concentration=voltage_diference*50.0/16.0;

After that, set the text size and text colour using the setTextSize() and setTextColor().

display.setTextSize(1);
display.setTextColor(WHITE);

Then in the next line, define the position where the text starts using the setCursor(x,y) method. And print the CO2 Values on OLED Display using the display.println() function.

    display.println("CO2");
    display.setCursor(63,43);
    display.println("(PPM)");
    display.setTextSize(2);
    display.setCursor(28,5);
    display.println(concentration);

And in the last, call the display() method to display the text on OLED Display.

 display.display();
 display.clearDisplay();

Testing the Interfacing of Gravity Infrared CO2 Sensor with Arduino

Once the hardware and code are ready, it is time to test the sensor. For that, connect the Arduino to the laptop, select the Board and Port, and hit the upload button. Then open your serial monitor and wait for some time (preheat process), then you'll see the final data.

Arduino Serial Monitor

The Values will be displayed on OLED display as shown below:

Gravity Infrared CO2 Sensor with Arduino

Note: Before using the sensor, let the sensor heat-up for approx 24 hours to get correct PPM values. When I powered the sensor for the first time, the output CO2 concentration was 1500 PPM to 1700PPM and after a 24 hour heat-up process, the output CO2 concentration decreased to 450 PPM to 500 PPM that are the correct PPM values. So it is necessary to calibrate the sensor before using it to measure the CO2 concentration.

This is how an Infrared CO2 sensor can be used to measure the accurate CO2 concentration in air. The complete code and working video are given below. If you have any doubts, leave them in the comment section or use our forums for technical help.

Code
int sensorIn = A4;
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup(){
  Serial.begin(9600);
  // Set the default voltage of the reference voltage
  analogReference(DEFAULT);
  display.begin(SSD1306_SWITCHCAPVCC);
  display.clearDisplay();
  display.display();
}
void loop(){
  //Read voltage
  int sensorValue = analogRead(sensorIn);
  // The analog signal is converted to a voltage
  float voltage = sensorValue*(5000/1024.0);
  if(voltage == 0)
  {
    Serial.println("Fault");
  }
  else if(voltage < 400)
  {
    Serial.println("preheating");
  }
  else
  {
    int voltage_diference=voltage-400;
    float concentration=voltage_diference*50.0/16.0;
    // Print Voltage
    Serial.print("voltage: ");
    Serial.print(voltage);
    Serial.println("mv");
    //Print CO2 concentration
    Serial.print("CO2 Concentration: ");
    Serial.print(concentration);
    Serial.println("ppm");
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(18,43);
    display.println("CO2");
    display.setCursor(63,43);
    display.println("(PPM)");
    display.setTextSize(2);
    display.setCursor(28,5);
    display.println(concentration);
    display.display();
    display.clearDisplay();
  }
  delay(2000);
}
Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Could you please explain a couple of points?

 

1.

float voltage = sensorValue*(5000/1024.0);

Where do 5000 and 1024 come from in this calculation? Am I right in thinking this is taking a Sine Wave up to a max amplitude of 5V and scaling it into 1024 units?

2.  int voltage_diference=voltage-400

Where does 400 come from here? What does it represent?

3. float concentration=voltage_diference*50.0/16.0;

What do 50.0 and 16.0 represent here? Where do they come from?