SCD30 CO₂ sensor Python driver
Project description
SCD30 CO₂ sensor I²C driver in Python 3
Status: initial release
The SCD30 is a high-precision CO2 sensor based on NDIR spectroscopy. The sensor module also includes an SHT31 temperature and humidity sensor onboard (see description of the PCB layout).
Overview
This library provides a Python interface to the main I²C-level commands supported by the SCD30 as listed in the interface description.
The primary intended use case is driving the sensor directly from a Raspberry Pi using hardware I²C. However, the code may be adapted for use with other devices supporting the protocol and/or software I²C.
System setup
The library was developed using a Raspberry Pi 4B (8GB RAM) running Raspberry Pi OS Buster. For more details about the chip, see the BCM2711 datasheet.
Wiring
The Raspberry Pi can drive the SCD30 module via its hardware I²C interface directly without any additional components:
SCD30 | Raspberry Pi |
---|---|
VDD | 3V3 Power |
GND | Ground |
TX/SCL | BCM 3 (SCL) |
RX/SDA | BCM 2 (SDA) |
SEL | Ground¹ |
¹ To select I²C mode, the SEL pin should be left floating or connected to ground. This forum post suggests grounding the pin may be the more reliable option.
Note the sequential order of the power, ground, and I²C pins on the SCD30 may be different from other popular sensor breakouts. For instance, the Pimoroni breakouts use (3V3, SDA, SCL, INT, GND).
For more details, see the Raspberry Pi I2C pinout.
Software configuration and I²C clock stretching
The SCD30 supports a maximal I²C speed of 100kHz (the default of the Pi 4B).
It also requires the I²C bus to support clock stretching of up to 150ms. By default, the bcm2835-i2c
driver which is still
used by the 4B (BCM2711) hard-codes the timeout to 35ms regardless of the speed. This does not seem to matter for one-off
readings, however may interfere with the long-term stability and particularly the automatic self-calibration feature.
As a workaround, the rpi-i2c binary utility provides means to manipulate the relevant I2C controller registers directly.
Usage
Contrary to other sensors that provide one-off readings, the SCD30 is designed to run continuously. Upon activation, periodic measurements are stored in a buffer. A "data ready status" command is provided to check whether a reading is available.
The following example code will begin periodic measurements at a two-second interval and print the readings:
from scd30_i2c import SCD30
scd30 = SCD30()
scd30.set_measurement_interval(2)
scd30.start_periodic_measurement()
time.sleep(2)
while True:
if scd30.get_data_ready():
m = scd30.read_measurement()
if m is not None:
print(f"CO2: {m[0]:.2f}ppm, temp: {m[1]:.2f}'C, rh: {m[2]:.2f}%")
time.sleep(2)
else:
time.sleep(0.2)
Note that this minimal example script will NOT issue a stop command upon termination and the sensor will continue taking periodic measurements unless powered off. This may or may not be appropriate depending on the use case.
For a more complete example, see here.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for scd30_i2c-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea7c192a824cced0e1f10f5c61f13a5976c593c8e5e5335e1c1bead27e5941b9 |
|
MD5 | 817b19bfc16b142646154b12b199301e |
|
BLAKE2b-256 | 256a94bf89e0ddaca8b8bcb098be73d18a9b8254560bd3a8ed5af36a2aaf47dc |