Using an Atlas Scientific CO2 Sensor with an Arduino

co2-sensor-arduino

Share This Post

In this tutorial we will show you how to connect an Atlas Scientific EZO-CO2™ Embedded NDIR Carbon Dioxide Sensor to an Arduino Uno. There are multiple ways to connect Atlas Scientific sensors to an Arduino, but for ease of use; We will be using a very basic, easy to follow setup that will get our CO2 sensor running in UART mode in no time!

With all the talk of CO2 these days, you would think it would be easy to detect. The Atlas Scientific EZO-CO2 sensor is a compact Non-Dispersive Infrared (NDIR) sensor that gets right to the point, giving you the CO2 readings in ppm. With internal temperature and humidity compensation.

Side Note

The Atlas Scientific EZO-CO2 Sensor detects gaseous CO2, it does not read dissolved CO2.

Please do not submerge this sensor in water as will cause severe internal damage.

Be sure to check out the Datasheet for the EZO-CO2 sensor, by clicking HERE.

Necessary items

Before we begin have the following items readily available:

1x Atlas Scientific EZO-CO2™ Sensor
1x 5 pin male header (comes with the EZO-CO2™ Sensor)
1x Arduino Uno (most commonly used Arduino)
1x USB cable type A – B male/male
4x Different colored male/female jumper cables

Step 1. Wiring the Arduino

When connecting a sensor to an Arduino or even a Raspberry Pi, try to remember that organization is key, which is why we are using 4 different colored jumper cables. Sure, we all know that Red is VCC and Black is ground. However, over the years we have seen many users use the same-colored jump cables throughout their entire project. I know all blue looks cool… but it’s very easy to get lost in your own mess. For this tutorial, we are going to use Red, Black, White, and Green.

The colors of the jumper cables chosen for this tutorial match the wires within the EZO-CO2 cable housing. Organization is key!

White = RX
Green = TX
Black = GND
Red = VCC

OK let’s get right to it!

Let’s start by placing the white jumper cable into pin 3 on the Arduino Uno board.

Then, place the green jumper cable into pin 2 on the Arduino Uno board.

These two jumper cables are important as they will allow both the EZO-CO2 Sensor and the Arduino Uno board to communicate with each other.

Next, place the red jumper cable into the 5V pin. And finally place the black jumper cable into any of the 2 GND or ground pins near the 5V pin.

These two jumper cables will now supply power and a ground line from the Arduino Uno to the EZO-CO2 Sensor.

Now that all four of the jumper cables have been placed, your Arduino board should look a lot like some sort of spider monster.

We are almost done!

Now that the Arduino Uno board is all set up, lets move onto the EZO-CO2 Sensor itself.

Step 2. Wiring the EZO-CO2 Sensor

The Atlas Scientific EZO-CO2 Sensor comes with a 5 pin male header inserted into the cable housing. If your 5 pin male header has not been inserted into the cable housing, please do so now.

Next, following the above image, connect the female ends of the jumper cables onto the 5 pin male header. Remember… Organization is key!

Connect each jumper cable to its corresponding pin (or in this case color)

RX – White to White
TX – Green to Green
GND – Black to Black
VCC – Red to Red

For this tutorial we will not be using the Alarm pin on the EZO-CO2 Sensor. You can just ignore the last “blue” pin on the Sensors cable housing.

Make sure all your jumper cables are fully connected on both the EZO-CO2 Sensor and on the Arduino Uno board before moving on to the next step.

Step 3. Now you’re playing with power

Using the USB cable type A – B male/male, connect one end into your PC and the other into the Arduino Uno board… you’ll know which end goes where, trust me!

Once the USB cable has been connected at both ends, you will notice the LEDs on the Arduino are now lit up and blinking. Looking at the back of your EZO-CO2 Sensor, it should also be lit up and blinking.

Side Note

All Atlas Scientific Sensors have two working modes, UART and I2C. Each of these modes are identified by a color. If your sensor is blinking green you are in UART mode. If the sensor is solid blue you are in I2C mode.

By default, all Atlas Scientific Sensors should come in UART mode.

However, If your EZO-CO2 Sensor is solid blue (I2C mode) refer to the Datasheet on how to change modes.

We are all finished with the wiring, now comes the fun part.

Step 4. Flashing the code

If you do not already have the Arduino IDE software installed onto your computer, please do so now, as we’ll be using it for the last section of this tutorial. You can download the Arduino IDE software by clicking HERE.

Generally, in this part of the tutorial we would show you how to write the code for the EZO-CO2 Sensor, but who the heck has the time for all that?! So instead, let’s just download it directly by clicking HERE!

Extract the sample code *.ino file and run it. The Arduino IDE software will inform you that the *.ino file needs to be placed within its own sketch folder, just click OK to continue.

You should now be in the Arduino IDE software.

//This code was written to be easy to understand.
//Modify this code as you see fit.
//This code will output data to the Arduino serial monitor.
//Type commands into the Arduino serial monitor to control the EZO-Co2 sensor.
//This code was written in the Arduino 1.8.9 IDE
//An Arduino UNO was used to test this code.
//This code was last tested 6/2019



#include <SoftwareSerial.h>                           //we have to include the SoftwareSerial library, or else we can't use it
#define rx 2                                          //define what pin rx is going to be
#define tx 3                                          //define what pin tx is going to be

SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work


String inputstring = "";                              //a string to hold incoming data from the PC
String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false;                //have we received all the data from the PC
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
int Co2;                                              //used to hold a integer number that is the Co2



void setup() {                                        //set up the hardware
  Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  myserial.begin(9600);                               //set baud rate for the software serial port to 9600
  inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
}


void serialEvent() {                                  //if the hardware serial port_0 receives a char
  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  input_string_complete = true;                       //set the flag used to tell if we have received a completed string from the PC
}


void loop() {                                         //here we go...

  if (input_string_complete == true) {                //if a string from the PC has been received in its entirety
    myserial.print(inputstring);                      //send that string to the Atlas Scientific product
    myserial.print('r');                             //add a <CR> to the end of the string
    inputstring = "";                                 //clear the string
    input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  }

  if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
    char inchar = (char)myserial.read();              //get the char we just received
    sensorstring += inchar;                           //add the char to the var called sensorstring
    if (inchar == 'r') {                             //if the incoming character is a <CR>
      sensor_string_complete = true;                  //set the flag
    }
  }


  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.println(sensorstring);                     //send that string to the PC's serial monitor
    /*                                                //uncomment this section to see how to convert the Co2 readings from a string to an  integer 
    if (isdigit(sensorstring[0])) {                   //if the first character in the string is a digit
      Co2 = sensorstring.toInt();                     //convert the string to a integer so it can be evaluated by the Arduino
      if (Co2 >= 400) {                               //if the Co2 is greater than or equal to 400
        Serial.println("high");                       //print "high" this is demonstrating that the Arduino is evaluating the Co2 as a number and not as a string
      }
      if (Co2 <= 399) {                               //if the Co2 is less than or equal to 399
        Serial.println("low");                        //print "low" this is demonstrating that the Arduino is evaluating the Co2 as a number and not as a string
      }
    }
    */
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
}

We’re almost ready to take readings from the EZO-CO2 Sensor, but before we do, we must make sure that the Arduino IDE software knows which board we are using. Go to Tools > Board > Arduino AVR Boards > and make sure that Arduino Uno is selected.

Finally, you must tell the software which COM port on your computer the Arduino Uno board is connected to. Go to Tools > Port > and choose the correct COM port.

Now that everything has been properly setup, and the code is ready to go, press the upload button (located at the top left) and it will upload the code directly to your Arduino Uno board.

Now, you can start taking readings from the EZO-CO2 Sensor. From within the Arduino IDE Software, open the Serial Monitor (located at the top right) and make sure to set it to append carriage return only and set the baud rate to 9600.

Side Note

When the EZO-CO2 Sensor is first powered on the sensor must warm-up before it can output readings. The warm-up process takes 10 seconds to complete.

During the first 10 seconds of operation the output will be: *warm

Be sure to check out the EZO-CO2 Sensor Datasheet for commands and other useful information.

CO2 Sensor

Subscribe To Our Newsletter

Get product updates and learn from the best!

More To Explore

how-to-calibrate-ezo-complete-tmp-using-atlas-desktop-software
Blog

How to calibrate the EZO Complete-TMP using the Atlas Desktop software

The Atlas Scientific line of EZO Complete circuits makes building a custom monitoring system easy, but if you don’t calibrate your sensors, then what good are your readings; And can you trust them? The accuracy of your readings is directly related to the quality of your calibration. Calibration is not difficult, and a little bit

how-to-calibrate-ezo-complete-dissolved-oxygen-using-atlas-desktop-software
Blog

How to calibrate the EZO Complete-Dissolved Oxygen using the Atlas Desktop software

The Atlas Scientific line of EZO Complete circuits makes building a custom monitoring system easy, but if you don’t calibrate your sensors, then what good are your readings; And can you trust them? The accuracy of your readings is directly related to the quality of your calibration. Calibration is not difficult, and a little bit

Want to learn more about our products?

Scroll to Top

To track your order please enter your Order ID in the box below and press the "Track" button. This was given to you on your receipt and in the confirmation email you should have received.