Arduino library · Sensors

Sensirion SCD4x: CO2 readings from an SCD40 or SCD41

The SCD40 and SCD41 measure carbon dioxide itself, with an optical photoacoustic cell, which sets them apart from metal-oxide sensors such as the SGP30 whose eCO2 is estimated from other gases. They report temperature and humidity as well, all over I2C at 0x62.

Built in on Mokxi: #include <SensirionI2cScd4x.h> and it builds, with nothing to install. Free, and no account needed.

What Sensirion I2C SCD4x does

Sensirion’s library has a style of its own. Almost every call returns an int16_t error code, 0 (NO_ERROR) on success, and values come back through reference arguments, so readMeasurement(co2, temperature, humidity) fills three variables you declared. The usual start is wakeUp(), stopPeriodicMeasurement() and reinit(), which clear whatever state the sensor was left in, then startPeriodicMeasurement(); a new reading follows every five seconds, and getDataReadyStatus() says when one is waiting.

The SCD41 adds measureSingleShot() for battery projects that wake it now and then. Automatic self-calibration assumes the sensor sees fresh outdoor air regularly; performForcedRecalibration() sets the reference by hand instead. In Mokxi the scd4x part has sliders for CO2, temperature and humidity. Altitude, ambient pressure and the temperature offset are stored and read back but do not change the reading. The older library’s class, SensirionI2CScd4x, is included too.

Library Manager nameSensirion I2C SCD4x
AuthorSensirion
Include#include <SensirionI2cScd4x.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesCO2 sensor, SCD41

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <SensirionI2cScd4x.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Sensirion I2C SCD4x" by Sensirion from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Sensirion I2C SCD4x to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.0, so a sketch written here builds there too.

What to know in the simulator: The older SensirionI2CScd4x class is here too. Altitude, pressure and the temperature offset are held and read back but do not change the reading.

The calls you will use

In the order a sketch meets them.

SensirionI2cScd4x sensor

Creates the driver.

sensor.begin(Wire, SCD41_I2C_ADDR_62)

Attaches the driver to a bus and address. It sends nothing and returns nothing.

sensor.wakeUp()

Wakes a sensor left in power-down; harmless when it is already awake.

sensor.stopPeriodicMeasurement()

Stops a measurement left running from before a reset. Takes 500 ms; most settings are refused until it is done.

sensor.startPeriodicMeasurement()

Starts a reading every five seconds and returns an error code.

sensor.getDataReadyStatus(dataReady)

Sets a bool to true when a new reading is waiting.

sensor.readMeasurement(co2Concentration, temperature, relativeHumidity)

Fills CO2 in ppm (uint16_t), temperature in °C and humidity in percent (floats).

uint16_t co2 = 0;
float t = 0.0, rh = 0.0;
if (sensor.readMeasurement(co2, t, rh) == NO_ERROR) {
  Serial.println(co2);
}

sensor.measureSingleShot()

SCD41 only: one measurement on request, waiting the five seconds it takes.

sensor.performForcedRecalibration(420, correction)

Tells the sensor the true CO2 level it is in right now and returns the correction applied. Stop periodic measurement first.

sensor.setAutomaticSelfCalibrationEnabled(0)

Turns automatic self-calibration off (1 turns it on); persistSettings() keeps the choice across power cycles.

Example: CO2 every five seconds from an SCD41

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Arduino.h>
#include <SensirionI2cScd4x.h>
#include <Wire.h>

SensirionI2cScd4x sensor;

static char errorMessage[64];
static int16_t error;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  sensor.begin(Wire, SCD41_I2C_ADDR_62);
  delay(30);
  sensor.wakeUp();
  sensor.stopPeriodicMeasurement();
  sensor.reinit();
  error = sensor.startPeriodicMeasurement();
  if (error != NO_ERROR) {
    Serial.print("Error trying to execute startPeriodicMeasurement(): ");
    errorToString(error, errorMessage, sizeof errorMessage);
    Serial.println(errorMessage);
    return;
  }
  Serial.println("Waiting for the first measurement...");
}

void loop() {
  bool dataReady = false;
  uint16_t co2Concentration = 0;
  float temperature = 0.0;
  float relativeHumidity = 0.0;
  delay(1000);
  error = sensor.getDataReadyStatus(dataReady);
  if (error != NO_ERROR || !dataReady) {
    return;
  }
  error = sensor.readMeasurement(co2Concentration, temperature, relativeHumidity);
  if (error != NO_ERROR) {
    errorToString(error, errorMessage, sizeof errorMessage);
    Serial.println(errorMessage);
    return;
  }
  Serial.print("CO2 concentration [ppm]: ");
  Serial.println(co2Concentration);
  Serial.print("Temperature [C]: ");
  Serial.println(temperature, 1);
  Serial.print("Relative Humidity [RH]: ");
  Serial.println(relativeHumidity, 1);
}

Wiring

  • Arduino Uno R3 A4 to CO2 sensor, SCD41 SDA
  • Arduino Uno R3 A5 to CO2 sensor, SCD41 SCL
  • Arduino Uno R3 5V to CO2 sensor, SCD41 VIN
  • Arduino Uno R3 GND to CO2 sensor, SCD41 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Error trying to execute startPeriodicMeasurement():

The sensor was still measuring from before a reset and refused the command, or it is not on the bus. Send stopPeriodicMeasurement() first, as the example does, then check the wiring.

Nothing prints for the first few seconds

The first reading arrives five seconds after startPeriodicMeasurement(), and getDataReadyStatus() reports false until then. That is normal; keep polling.

CO2 slowly settles toward a low value in a room that never airs out

Self-calibration took the lowest level it saw as fresh air. Turn it off with setAutomaticSelfCalibrationEnabled(0) and run a forced recalibration in fresh air instead.

'SensirionI2cScd4x' does not name a type

The tutorial was written for an older version of the library, whose class is SensirionI2CScd4x with a capital C. Match the class name and include to the version you have; Mokxi accepts both.

Questions and answers

What is the difference between the SCD40 and SCD41?

They share the same commands for periodic measurement. The SCD41 adds single-shot measurement, which lets a battery project wake it only when it needs a reading.

Why does every Sensirion call return a number?

It is the error code, 0 for success. errorToString() turns any other code into text, which the example prints when a step fails.

Projects that use Sensirion I2C SCD4x

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try Sensirion I2C SCD4x without installing anything

Open the example, change the sketch, and keep your version in a free account.