Skip to content
Larry Bank edited this page Jun 23, 2023 · 2 revisions

Public API

The SCD41 class exposes the methods detailed below.

This is the entire class definition for SCD41:

class SCD41 {
  public:
    int init(int iSDA=-1, int iSCL=-1, bool bBitBang=false, int32_t iSpeed=100000L);
    int start(int iMode = SCD41_MODE_PERIODIC);
    int stop();
    void wakeup();
    void getSample(); // trigger + read the latest data
    int recalibrate(uint16_t u16CO2);
    void setAutoCalibrate(bool bOn);
    int temperature(); // temperature (C) as an int 10x (e.g. 25.5 = 255)
    int humidity(); // humidity as an int 10x (e.g. 50.5% = 505)
    int co2(); // CO2 as an int
    void shutdown(); // turn off sensor (if possible)

  private:
    uint32_t _iCO2, _iHumidity;
    int32_t _iTemperature;
    int _iType; // sensor type (SCD40/SCD41)
    int _iAddr; // I2C address of device
    int _iMode;
    BBI2C _bbi2c;
};

int init()
There are four optional parameters for the init method - the first two define the GPIO pins used for the I2C signals SDA & SCL, the third specifies whether to use hardware I2C (false) or bit bang I2C (true) and the fourth sets the I2C speed. My usage of the SCD4x indicates that it doesn't like speeds above 100Kbs. For Arduino targets with default I2C pins, the init method can be called with no parameters. For MCUs with flexible I/O pins such as the ESP32 family, you can specify almost any GPIO pins to use. If the I2C initializes, the device is found and it responds correctly, SCD41_SUCCESS will be returned, otherwise SCD41_ERROR.

int start(int iMode)
The sensor powers up in idle mode. This is due to the large amount of power needed to take samples. This function tells it to start sampling using the mode you specify. The SCD40 supports normal (samples every 5 seconds), and low power (samples every 30 seconds). The SCD41 supports these 2 modes, but adds the option for "one shot" mode which allows you to trigger a sample at any time. This can be used for lower power situations where you wants samples taken less frequently than every 30 seconds.
The mode value can be one of: SCD41_MODE_PERIODIC, SCD41_MODE_LP_PERIODIC or SCD41_MODE_SINGLE_SHOT

stop()
Returns the SCD4x to idle mode (stops sampling).

wakeup()
Returns the SCD41 to the sampling mode set before calling stop().

getSample()
Triggers (if in single shot mode) and reads the current sample values for CO2, Temperature and Humidity from the SCD41 (into internal variables).

recalibrate(uint16_t u16CO2)
Starts the recalibration procedure to the given value. For example, the Earth currently measures about 423ppm of CO2. The procedure is to run the SCD4x in periodic (fast) mode for at least 3 minutes, then call this function with the value of the ambient air. A return value of SCD41_SUCCESS indicates that the calibration operation succeeded.

setAutoCalibrate(bool bOn)
The auto-calibration mode is factory set to ON. This tells the SCD41 to use the lowest value of CO2 it reads during a week of usage as the ambient air value. If you only use this sensor indoors, I would recommend hard-calibrating it in ambient air, and turning this setting to off.

int temperature()
Returns the last temperature reading as an integer value representing 10x the reading. For example, a temperature of 25.4C would return the value 254.

int humidity()
Returns the last humidity reading as an integer representing the percentage of saturation.

int co2()
Returns the last CO2 reading as an integer value representing the number of parts per million.

shutdown()
Puts the SCD4x to sleep for lowest power usage.

Clone this wiki locally