Products IN-STOCK ready to Ship Next Day for orders placed by 2pm EST. Use Code SENSE for 10% OFF.


CO2 Sensor and Raspberry Pi Example Code

co2 sensor raspberry pi 

What is a Raspberry Pi?

A Raspberry Pi is recognized as a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation in association with Broadcom.The Raspberry Pi project originally leaned towards the promotion of teaching basic computer science in schools and in developing countries.

CO2 sensor Raspberry Pi

For many individuals, including CO2Meter customers, as the Raspberry Pi's grew more popular, a wide range of individuals paired these computers with CO2 sensors to create a variety of experiments. Research and projects varied including robotics, agriculture, weather monitoring, indoor air quality monitoring, industrial and commercial automation, and much more.

Currently, because of its low cost, modularity, and open design, the Raspberry Pi still is recognized as the best-selling British computer.

How does a Raspberry Pi work?

The Raspberry Pi is a credit-card sized computer, which typically includes USB ports, plus Wi-Fi and Bluetooth connectivity. In addition, the module also includes a HDMI output that requires an HDMI cable with appropriate HDMI-capable monitor. Dependent upon preference, a USB keyboard and USB mouse are also always a necessity prior to operation.

To provide some perspective on operation of a Raspberry Pi, we will be using one of the Raspberry Pi models - the RPi3 model B and a K30 CO2 Sensor.

Example code for both the K30 sensor is included below.

The RPi3 offers a UART TXD-RXD connection for serial communication.

In addition, the Raspian Linux operating system used is installed using the NOOBS installer

Initial Set-Up:

  • Connect your Raspberry Pi to your keyboard, mouse, monitor and 5VDC power supply.
  • Power the RPi3 and LCD monitor. To access the attached python files, you will need an internet connection. RPi3 offers a built0in WiFi device.
  • On your monitor, find the icon of a terminal. Select the icon, select your WiFi network, and enter your network password.
  • Verify that you are connected via Wi‐Fi. On your monitor, select the globe icon to open an internet browser.
  • Verify your internet connection by going to
  • Review CO2Meter App Note AN-137
  • Download the zip file to your downloads directory
  • Unzip this directory to access the python files. 

Run the Blink Example:

We recommend you run the Blink example to verify your hardware and software is operating properly before you connect the K30 CO2 Sensor to your Raspberry Pi.

  • Before you connect the CO2 sensor. Included in the downloaded folder is the python code. 
  • You can open it with the Geany, the Raspian code editor.
  • Connect a LED and resistor to the RPi pin header to GPIO pins 9 and  12.  The longer lead on the LED is positive, and connects through a  270 ohm, ¼ watt resistor to pin.
  • The other LED lead connects to pin 9 which is GND (ground).
  • Select Menu > Programming > Geany.
  • Select File > Open > pi > AN137 > Blink.
  • You should see the screen below. 
  • Select Build > Compile.
  • Select Build > Execute. 
  • The LED will blink 12 times if everything is set up correctly. Otherwise, go back and retrace your steps before continuing! 

Connecting a K30 CO2 Sensor to Raspberry Pi:

  • Power the sensor with an external 6‐9VDC  (recommended) power supply capable of 300mA output. 
  • When the K‐30 sensor takes a measurement, the internal circuitry will draw a comparatively larger peak current for approximately 100 milliseconds.
  • Note: If power drops below 5.5VDC, the sensor will not work as described

Creating your RPi3-K30 Project:

  • Select Menu > Programming > Geany
  • Select File > Open > pi > AN127 > K30
  • Observe the following screen:  

Geany K30 and Raspberry Pi Operation*Note: On line 6, the UART connection is ttyS0. Your tty connection may be different* 

  • Select Build > compile 
  • Select Build > execute
  • The output is shown below:
Sample Output K30 CO2 Sensor and Raspberry Pi

K-30 CO2 Sensor Python Code

 Python Code

The future for Raspberry Pi

The possibilities for integration with a Raspberry Pi, are typically endless. To date, more than 40 million boards have been sold with many uses in between. For CO2Meter customers specifically, the importance of a Raspberry Pi is in the ability to incorporate it with CO2 sensors to further advance research, experimentation, and analysis. Some of our customers have even broken barriers by use of CO2 sensors and Raspberry Pi configuration ranging from:

  • Photography experimentation
  • Air Quality monitoring systems for COVID-19
  • Weather and Pollution monitoring system
  • IoT smart home technologies
  • Hydroponic Farming
  • Safety Alarms
  • and more

For more information on CO2 sensing technologies or to speak to a CO2Meter expert, contact us at or 877-678-4259


Older Post Newer Post