Build Your Own High-Concentration CO2 Detector
Project Setup - STC3x Python Package
The SparkFun STC3x Python Package is based off the Arduino libraries for the STX3x family of CO2 sensors from Sensirion™. It works for Python, MicroPython and CircuitPython so you can choose which "flavor" of Python to use. This section goes over how to install the package in each supported Python environment along with the necessary tools for installation and running examples.
Python Installation
For users on a Linux-based system (Raspberry Pi, etc.), install this package in Python with PyPi using the pip3
command with the following steps:
- Set up a virtual environment from a specific directory using
venv
: `python3 -m venv path/to/venv (You can use any path here just remember to use the same one for each step). - Install the Qwiic package: `path/to/venv/bin/pip3 install sparkfun-qwiic-stcx
That's it. Now you can run any example (or your own custom scripts) by running it with a command like: path/to/venv/bin
MicroPython Installation
If you prefer to use MicroPython to run the examples on a microcontroller running MicroPython, follow these steps to install the package on your computer:
- Install mpremote on your computer.
- Connect the IoT RedBoard - ESP32 (or other MicroPython device) to your computer and install the package directly using mpremote mip:
mpremote mip install github:sparkfun/qwiic_stcx_py
- Install the examples to the IoT RedBoard with the following mip command:
mpremote mip install github:sparkfun/qwiic_stcx_py@examples
CircuitPython Installation
Lastly, users who prefer CircuitPython can install the package with the following steps:
- Install CircUp on your computer.
- Make sure you have the latest version of the SparkFun Qwiic CircuitPython module installed with this command:
circup bundle-add sparkfun/Qwiic_py
- Connect your CircuitPython device to your computer and install the package to the device with circup:
circup install --py qwiic_stcx
If you want to install specific examples from the repository, use the corresponding circup command from below:
circup example qwiic_stc3x\qwiic_stc3x_ex1_basic
circup example qwiic_stc3x\qwiic_stc3x_ex2_PHT_compensation
circup example qwiic_stc3x\qwiic_stc3x_ex4_self_test
circup example qwiic_stc3x\qwiic_stc3x_ex5_auto_calibration
Note: The syntax used here are for Windows commands; Linux and Mac have different path separators. Refer to the CircUp "example command documentation for more information.
Python Examples
Example 01 - Basic Readings
Example 1 shows how to get basic CO2 and temperature data from the STC31. It initializes the sensor with default settings to measure CO2 in air up to 25% concentration. Open the example in your preferred Python interpreter and run it. You should see some initialization messages print out followed by CO2 concentration and temperature data every second. Try breathing on the sensor or holding it up to a different CO2C source and you should see the values change.
Code to Note
The STC31 has four different measurement modes available for the set_binary_gas()
function. This lets you change between measuring either up to 25% or 100% CO2 concentration measured either in nitrogen (N2) or air. You can adjust the measurement mode by modifying the line of code below with the corresponding concentration/gas:
# Possible values are:
# kBinaryGasCO2N2_100 : Set binary gas to CO2 in N2. Range: 0 to 100 vol%
# kBinaryGasCO2Air_100 : Set binary gas to CO2 in Air. Range: 0 to 100 vol%
# kBinaryGasCO2N2_25 : Set binary gas to CO2 in N2. Range: 0 to 25 vol%
# kBinaryGasCO2Air_25 : Set binary gas to CO2 in Air. Range: 0 to 25 vol%
if mySTC31.set_binary_gas(mySTC31.kBinaryGasCO2Air_25):
Example 02 - PHT Compensation
The second example shows how set values for temperature, humidity and pressure to improve the accuracy of the CO2 data. The example initializes the STC31 to measure in air at concentrations up to 25% volume and then sets the temperature, humidity and barometric pressure to fixed reference values.
Code to Note
As mentioned above, this example sets static compensation values for temperature, pressure and humidity. You may want to adjust these values for your testing environment:
# These are example compensation variables and you can change them as needed to fit your environment
pressure = 840
humidity = 25
temperature = 25
Example 05 - Self Calibration
We are going to jump to Example 5 which demonstrates how to enable the STC31's automatic self calibration feature as well as how to perform a forced recalibration. Automatic calibration helps improve the STC31's accuracy when used in applications where the target gas is not present most of the time. Refer to the STC31 Field Calibration Guide for more information on calibrating the STC31.
After initializing the sensor, the code attempts to perform a forced recalibration of the STC31 with a CO2 value of 0. After performing the forced recalibration, the code enables the STC31's automatic calibration feature and then prints out CO2 and temperature data every second.