Arduino library · Sensors

SparkFun SCD4x Arduino Library: an example that runs

Yes. You can use the SparkFun SCD4x Arduino Library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (CO2 sensor, SCD41). The example below, "CO2, temperature and humidity from an SCD41 the SparkFun way", runs on the Arduino Uno R3.

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

What SparkFun SCD4x Arduino Library does

SCD4x mySensor: begin(), readMeasurement(), getCO2(), getTemperature() and getHumidity(), the offset, altitude, pressure and self-calibration settings, forced recalibration and the SCD41 single shots. That is what SparkFun SCD4x Arduino Library covers. The sketch below uses it on the Arduino Uno: “CO2, temperature and humidity from an SCD41 the SparkFun way”.

SparkFun's library for the Sensirion SCD40 and SCD41 CO2 sensors.

begin() stops any periodic measurement (500 ms), reads the serial number and the sensor variant, sets automatic self-calibration and starts periodic measurement, a result every 5 s; it is false if any step failed. Commands are sixteen bits, a word argument and every answer word carry a CRC (CRC-8, polynomial 0x31 from 0xFF).

The calls follow release 1.1.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameSparkFun SCD4x Arduino Library
AuthorSparkFun
Include#include <SparkFun_SCD4x_Arduino_Library.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 <SparkFun_SCD4x_Arduino_Library.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: The part keeps the altitude, pressure, temperature offset and recalibration and reads them back, but they do not change its CO2 reading. enableDebugging() prints nothing.

The calls you will use

In the order a sketch meets them.

SCD4x mySensor

Makes mySensor, the SCD4x object the calls below are made on.

mySensor.begin()

Starts it up and returns true if it started, false if it did not. Call it once in setup().

mySensor.readMeasurement()

Returns the measurement as true or false.

mySensor.getCO2()

Returns the co2 as a whole number.

mySensor.getTemperature()

Returns the temperature as a decimal number.

mySensor.getHumidity()

Returns the humidity as a decimal number.

Example: CO2, temperature and humidity from an SCD41 the SparkFun way

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

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

#include "SparkFun_SCD4x_Arduino_Library.h"
SCD4x mySensor;

void setup() {
  Serial.begin(115200);
  Serial.println(F("SCD4x Example"));
  Wire.begin();
  delay(30); // the SCD4x answers 30 ms after power-up

  if (mySensor.begin() == false) {
    Serial.println(F("Sensor not detected. Please check wiring. Freezing..."));
    while (1)
      ;
  }
}

void loop() {
  if (mySensor.readMeasurement()) { // true when fresh data is available
    Serial.println();

    Serial.print(F("CO2(ppm):"));
    Serial.print(mySensor.getCO2());

    Serial.print(F("\tTemperature(C):"));
    Serial.print(mySensor.getTemperature(), 1);

    Serial.print(F("\tHumidity(%RH):"));
    Serial.print(mySensor.getHumidity(), 1);

    Serial.println();
  } else
    Serial.print(F("."));

  delay(500);
}

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

Try SparkFun SCD4x Arduino Library without installing anything

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