Package: scd30-i2c
SCD30 CO₂	sensor I²C driver in Python 3Status: initial releaseTheSCD30is a high-precision CO2 sensor based onNDIRspectroscopy.
The sensor module also includes anSHT31temperature and humidity sensor onboard (seedescription of the PCB layout).OverviewThis library provides a Python interface to the main I²C-level commands supported by the SCD30 as listed in theinterface 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.InstallationThe library is available for download from the Python Package Index (tested with Python 3.7.3):python3-mpipinstallscd30_i2cSystem setupThe library was developed using a Raspberry Pi 4B (8GB RAM) running Raspberry Pi OS Buster. For more details about the chip, see theBCM2711 datasheet.WiringThe Raspberry Pi can drive the SCD30 module via its hardware I²C interface directly without any additional components:SCD30Raspberry PiVDD3V3 PowerGNDGroundTX/SCLBCM 3 (SCL)RX/SDABCM 2 (SDA)SELGround¹¹ To select I²C mode, the SEL pin should be left floating or connected to ground.This forum postsuggests 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, thePimoroni breakoutsuse (3V3, SDA, SCL, INT, GND).For more details, see theRaspberry Pi I2C pinout.Software configuration and I²C clock stretchingThe 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, thebcm2835-i2cdriver 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, therpi-i2cbinary utility provides means to
manipulate the relevant I2C controller registers directly.UsageContrary 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.Sample codeThe following example code will begin periodic measurements at a two-second interval and print the readings:fromscd30_i2cimportSCD30scd30=SCD30()scd30.set_measurement_interval(2)scd30.start_periodic_measurement()time.sleep(2)whileTrue:ifscd30.get_data_ready():m=scd30.read_measurement()ifmisnotNone: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, seehere.Temperature calibrationThe SCD30 module contains a temperature and humidity sensor, which allows for temperature compensation of the CO₂ sensor
signal. Therefore, the correctness of the temperature measurements is critical to achieving highly accurate CO₂ readings.Due to the small size of the module, the inherent self-heating of the various electrical components on and around the
PCB are likely to cause values above ambient temperature to be reported. To counteract this, a temperature offset can be
configured via the I²C interface. The correct value will depend on the placement and configuration of the sensor and
should be updated if any changes are made. For instance, setting a different measurement interval can change the average
power draw of the sensor, and in turn, the heat produced by its components. Changing its position relative to other
components, altering the airflow or installing additional sensors nearby may similarly change the offset required.By default, the temperature offset is disabled, i.e. set to 0'C. However, the following calculations apply in the general
case, even with non-zero temperature offsets already set.To determine the correct temperature offset, consider the following values:T_ambient: the "reference" ambient temperature, measured through means other than the SCD30.T_measured: the raw temperature reading obtained internally onboard the SCD30; we assumeT_measured >= T_ambient.T_reported: the temperature reported by the SCD30 after applying the configured offset, i.e.T_reported = T_measured - T_offset.Clearly, the end goal is to minimize the error:Δ = |T_reported - T_ambient|
  = |T_measured - T_offset - T_ambient|
  = |T_measured - T_ambient - T_offset|Consequently:T_offset = T_measured - T_ambientNote that the SCD30 does not exposeT_measureddirectly; the value returned byread_measurement()already has the
current offset applied, i.e.T_reportedis returned instead. Recall that:T_reported = T_measured - T_offsetTherefore, the raw valueT_measuredcan be computed by factoring in the current offsetT_offset_old(obtained usingget_temperature_offset()):T_measured = T_reported + T_offset_oldHaving obtainedT_measuredand a "true" reference temperatureT_ambient(e.g. using a different thermometer) a new
offset can be calculated:T_offset_new = T_measured - T_ambient = (T_reported + T_offset_old) - T_ambient.