Connecting an Atlas Scientific CO2 Sensor to a Raspberry Pi 4.0

co2-sensor-raspberry-pi

Share This Post

In this tutorial, we will show you how to connect an Atlas Scientific EZO-CO2™ Embedded NDIR Carbon Dioxide Sensor to a Raspberry Pi 4.0. There are multiple ways to connect an Atlas Scientific sensor to a Raspberry Pi. Today, we will be following a wiring diagram that will get the CO2 sensor running in UART mode (which is the default mode for the CO2 sensor).

Before we jump right into this tutorial, we have to setup a Raspberry Pi 4.0. It is possible that your Raspberry Pi has already been setup and is ready to go. If this is the case, Awesome! Skip ahead to Step 6. However, we are going to assume this is a brand-new Raspberry Pi 4.0, which will need some small tweaks before we begin. So, buckle up, here we go!

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 Raspberry Pi 4
  • 1x Blank micro SD 16gb card (and card reader if needed)
  • 1x breadboard
  • 1x USB keyboard
  • 1x USB mouse
  • 1x monitor with power cable
  • 1x HDMI to mini-HDMI adapter or cable
  • 2x 4.7K pull-up resistors
  • 4x Different colored male/female jumper cables

Download the Raspberry pi imager and the latest OS

Assuming your Raspberry Pi 4.0 does not have an OS, let us begin by installing the latest OS onto it.  Click HERE and download both the latest OS update and the Raspberry Pi Imager.

After both files have downloaded, extract them both to a location on your computer.
Insert the Blank micro-SD card into your computer and run the Raspberry Pi Imager.

First you will need to select the OS you had recently downloaded and extracted. Click on the button labeled “CHOOSE OS”, a pop will appear, scroll down to the last option “use custom” and point to the extracted OS.

Next, click on the “CHOOSE STORAGE” button and select the blank micro-SD card. Then Click on the Write button. This installation process may take ~ 5-10 mins.

Once the installation process has completed, insert the micro-SD card into the Raspberry Pi. Plug in your USB mouse and Keyboard, along with the mini-HDMI cable or adapter (depending on your needs) and finally plug in the USB power cable and boot up the Raspberry Pi.

Once your Raspberry Pi has booted up, you will need to set your location, language, and network settings.  Now that your Raspberry Pi is operational, we can begin to make a few tweaks before we can start taking CO2 readings.

Step 1. Expand File system

All the tweaks we will be making, are made using the terminal software that is part of the OS. To access the terminal software, click on this icon located at the top left corner of the screen.

From within the Raspberry Pi’s terminal enter this command.

sudo raspi-config

You should see a blue screen with options in a gray box in the center, like so

Select #6 Advanced Options, then select “Expand Filesystem”.

Selecting this option will expand your installation to fill the rest of the SD card, giving you more space to use for files. You will need to reboot the Raspberry Pi to make this available.

Step 2. Update and Upgrade Packages

After your Raspberry Pi has rebooted, return to the terminal program. You will need to update your system’s package list by entering the following command in terminal.

sudo apt-get update

Next, upgrade your installed packages to their latest versions with the command.

sudo apt-get upgrade

Running the upgrade may take up to 30 minutes depending on which version of the Raspberry Pi you have. On a new Raspberry Pi 4.0 this process took ~10 seconds.

Step 3. Download the sample code

To download the Atlas Scientific™ sample code, run the following commands within the Raspberry Pi’s terminal.

cd ~

git clone https://github.com/AtlasScientific/Raspberry-Pi-sample-code.git

Now that the sample code has finished downloading, we are almost ready to connect the EZO-CO2™ Embedded NDIR Carbon Dioxide Sensor to your Raspberry Pi. However, since the CO2 sensor is set to UART mode as its default, we will be reading from the Raspberry Pi in UART mode. To do so, we will need to make just a few more tweaks to the boot command line.

Run the following command line.

sudo nano /boot/cmdline.txt

You should see something that looks a lot like this:

You might see two separate commands listed for the “console”.
console=tty1               console=serial0,115200

This can cause a conflict in the serial port.

To correct this issue, delete the command:

console=serial0,115200

The command line should now look like this:

When finished, press “CTRL+X”, then “Y” and hit Enter to save & exit.

Next, we need to ensure PySerial is installed for Python.

sudo pip install pyserial

Step 4. UART mode

The Raspberry Pi 4 has 6 UARTs, for this tutorial we will be using UART 5 which uses pins 32 (TX) and 33 (RX) on the raspberry pi 40 pin header.

Note that other UARTs share their pins with other peripherals, so those peripherals may have to be disabled to use them.

Go into the boot configuration

sudo nano /boot/config.tx

Scroll down to the bottom and add these two lines.

enable_uart=1
dtoverlay=uart5

Again, when finished press “CTRL+X”, then “Y” and hit Enter to save & exit.

Step 5. The final tweak

We need to make one last tweak, in order for the Raspberry Pi to use UART 5

 cd Raspberry-Pi-sample-code
 sudo nano uart.py 

Scroll down to line 72 and add a “1” at the end of this line:

usbport = ‘/dev/ttyAMA

So it now looks like this:

 usbport = ‘/dev/ttyAMA1’

Note that it may be a different ttyAMA depending on your setup.

When finished, press “CTRL+X”, then “Y” and hit Enter to save & exit, then restart the Raspberry Pi. No more tweaks.

Finally… after what seemed like an eternity making small tweaks to the Raspberry Pi, we are now ready to wire up the Atlas Scientific EZO-CO2 Sensor.

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.

Step 6. Wiring the Raspberry Pi

When connecting a sensor to 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 jumper 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

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

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

Insert the sensors data cable with the 5-pin male header directly into the breadboard, like so.

Place one of the 4.7K pull-up resistors in to VCC (red) and TX (green) on the adjacent row, like so.

Place the other 4.7K pull-up resistors in to VCC (red) and RX (white) on the adjacent row, like so.
Make sure the pull-up resistors are not touching!

Next, place the 4 jumper wires into its corresponding spots into the adjacent row, on the bread board.

White to White
Green to Green
Black to Black
Red to Red

Now that the breadboard is all wired up, we are now ready to connect the jumper cables to the Raspberry Pi. Looking back at our wiring diagram, we see that:

VCC (red) goes to Pin 4
GND (black) goes to Pin 6
RX (green) goes to Pin 32
TX (white) goes to Pin 33

Step 7. Power up!

Now that the EZO-CO2 Sensor is connected, go ahead, and turn on the Raspberry Pi. You will notice the LEDs on the Raspberry Pi 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.

Step 8. Run the code

Now that EZO-CO2 Sensor and Raspberry Pi have been properly connected, its time to run the sample code.

Back in the terminal software, make sure you are in the correct folder, and then run the code.

 cd Raspberry-Pi-sample-code/
 sudo pyton uart.py 

Within the sample code, you can give special commands to the EZO-CO2 Sensor.
(Be sure to check out the EZO-CO2 Sensor Datasheet for a full list of UART commands and other useful information.)

Without entering any commands, you can pull the CO2 readings off the sensor, by simply pressing “Enter”.

Make sure not to breathe directly onto the EZO-CO2 Sensor, as your breath will affect the readings.

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.

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.