SCD30 - CO2, humidity and temperature sensor

Description

Library for interfacing Sensirion SCD30 CO2, humidity and temperature sensor.

The SCD30 is an I2C-bus controlled CO2, humidity and temperature sensor. -) I2C interface with communication speed up to 100kHz -) Fixed I2C adress -) CO2 measurement range 400 – 40000 ppm

OVERVIEW

SCD30_Abstract:
This library can be used to handle the Sensirion SCD30 CO2, humidity and temperature sensor.

Interface: 1x I2C

The SCD30 is an I2C-bus controlled CO2, humidity and temperature sensor.
  • NDIR CO2 sensor technology
  • Integrated temperature and humidity sensor
  • Best performance-to-price ratio
  • Dual-channel detection for superior stability
  • Small form factor: 35 mm x 23 mm x 7 mm
  • Measurement range: 400 ppm – 10.000 ppm
  • Accuracy: ±(30 ppm + 3%)

  • For more information, see the Sensirion website
    SCD30_How_to_use:
    /* Hardware setup */ const { PORT_I2C = 0, // The first I2C interface should be used. MEASUREMENT_INTERVAL = 2, // [s] Measurement interval ALTITUDE = 247, // [m] Altitude of current position } /* Handles for available sensors */ static sCfg[TSCD30_Config]; // Handle to manage the SCD30 #callback ReadMeasurement() { new iResult; new Float:fCo2; new Float:fTemperature; new Float:fHumidity; // Init the I2C interface catch(rM2M_I2cInit(PORT_I2C, SCD30_CLOCK, 0)); // Read measurement data from the SCD30 iResult = SCD30_ReadData(sCfg, fCo2, fTemperature, fHumidity); if (iResult != OK) { #log("SCD30_ReadData (%d): Measurement not available, retry in 1s", iResult); // Retry in 1s if data is not yet available setTimeout(ReadMeasurement, 1000); } else { #log("SCD30_ReadData: %0.1fppm %0.1f°C %0.1f%%rH", fCo2, fTemperature, fHumidity); // Wait the measurement interval before reading the next measurment setTimeout(ReadMeasurement, 1000 * MEASUREMENT_INTERVAL); } // Close the I2C interface catch(rM2M_I2cClose(PORT_I2C)); } #callback EnableSCD30() { new iResult; // Init the I2C interface catch(rM2M_I2cInit(PORT_I2C, SCD30_CLOCK, 0)); // Init the SDC30 iResult = SCD30_Init(sCfg, PORT_I2C); if (iResult != OK) { #log("SDC30_Init failed (%d), retry in 1s", iResult); // Retry in 1s if sensor is not yet available setTimeout(EnableSCD30, 1000); } else { // Start periodic measurements of the SDC30 with altitude (optional) // for altitude compensation iResult = SCD30_Enable(sCfg, MEASUREMENT_INTERVAL, ALTITUDE); if (iResult != OK) { #log("SDC30_Enable failed (%d), retry in 1s", iResult); // Retry in 1s if sensor is not yet ready setTimeout(EnableSCD30, 1000); } else { // Wait the measurement interval before reading the measurment setTimeout(ReadMeasurement, 1000 * MEASUREMENT_INTERVAL); } } // Close the I2C interface catch(rM2M_I2cClose(PORT_I2C)); } #callback InitSCD30() { // Wait for 2s for boot-up before enabling SCD30 setTimeout(EnableSCD30, 2000); } /* Application entry point */ main() { salve(InitSCD30); }

    BASIC

    SCD30_CLOCK -

    typical I2C clock speed to be used (40kHz)

    SCD30_Init(sCfg[TSCD30_Config], iI2C)

    Initializes I2C communication with the SCD30.


    It copies the I2C port, I2C address to the transferred empty device handle and checks if the SCD30 is available and responds.

    sCfg : TSCD30_Config - Empty device handle for a SCD30
    iI2C : s32 - I2C interface where SCD30 is connected to
    The I2C interface must be initialized before using rM2M_I2cInit().
    returns : s32
    OK - If successful
    < OK - If another error occurs
    SCD30_Enable(sCfg[TSCD30_Config], iInterval, iAltitude=-1, iAmbientPressure=-1)

    Enables periodic measurements of the SCD30.


    First the given measurement interval is set, then - if available - the altitude is set for altitude compensation. The periodic measurement is started using the ambient pressure for compensation (if available).

    Only one compensation mechanism is possible - altitude or pressure. Altitude compensation is used, if both are activated.

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iInterval : u32 - Measure interval in seconds
    Available range: 2 ... 1800 s
    iAltitude : u16 - Altitude compensation configuration
    -1 - Altitude compensation deactivated
    >=0 - Height above sea level in m.
    iAmbientPressure : u16 - Ambient pressure compensation configuration
    -1 - Ambient pressure compensation deactivated
    700 ... 1200 - Pressure (in mBar) used for ambient pressure compensation
    returns : s32
    OK - if successful
    ERROR - if one of the following errors occurs
  • Measure interval is invalid
  • Altitude used for altitude compensation is invalid
  • Pressure used for ambient pressure compensation is invalid
  • < OK - If another error occurs
    SCD30_ReadData(sCfg[TSCD30_Config], &Float:fCo2, &Float:fTemperature, &Float:fHumidity)

    Reads out measurements of the SCD30.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    fCo2 : f32 - Float variable for storing the CO2 measurement in ppm.
    fTemperature : f32 - Float variable for storing the temperature measurement °C.
    fHumidity : f32 - Float variable for storing the humidity measurement in % rH.
    returns : s32
    OK - if successful
    ERROR - if one of the following errors occurs
  • No data is (yet) available from the SCD30
  • CRC mismatch
  • CO2 measurement value is 0.0
  • < OK - If another error occurs
    SCD30_Disable(sCfg[TSCD30_Config])

    Disables periodic measurements.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )

    EXPERT

    SCD30_CLOCK_MAX -

    absolute maximum I2C clock speed (100kHz)

    SCD30_Probe(sCfg[TSCD30_Config])

    Checks, if the SCD30 is ready to receive commands.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_StartPeriodicMeas(sCfg[TSCD30_Config], iAmbientPressure=0)

    Starts continuous measurement of the CO2 concentration, humidity and temperature.


    Measurement data which is not read from the sensor by using SCD30_ReadMeas() will be overwritten. The measurement interval is adjustable via the command SCD30_SetMeasInterval(). The initial value is 2sec.

    Continuous measurement status and chosen measurement interval are stored in non-volatile memory and thus is not reset after power up.

    The CO2 measurement value can be compensated for ambient pressure by feeding the pressure value in mBar to the sensor. Setting the argument to zero will deactivate the ambient pressure compensation. For setting a new ambient pressure when continuous measurement is running the whole command has to be written to SCD30

    Setting the ambient pressure will overwrite previous and future settings of altitude compensation set by using SCD30_SetAltitude().

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iAmbientPressure : u32 - Deactivate ambient pressure compensation / Pressure used for compensation
    0 - Ambient pressure compensation deactivated
    700 ... 1200 - Pressure (in mBar) used for ambient pressure compensation
    returns : s32
    OK - if successful
    ERROR - Pressure used for ambient pressure compensation is invalid
    < OK - If another error occurs
    SCD30_StopPeriodicMeas(sCfg[TSCD30_Config])

    Stops the continuous measurement of the SCD30.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    returns : s32
    OK - if successful
    < OK - if an error occurs
    SCD30_SetMeasInterval(sCfg[TSCD30_Config], iInterval)

    Sets the interval used by the SCD30 sensor to measure in continuous measurement mode.


    Initial value is 2s. The chosen measurement interval is saved in non-volatile memory.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iInterval : u32 - Measure interval in [s]
    Available range: 2 ... 1800 s
    returns : s32
    OK - if successful
    ERROR - Measure interval is invalid
    < OK - If another error occurs
    SCD30_GetMeasInterval(sCfg[TSCD30_Config], &iInterval)

    Reads the current interval used by the SCD30 sensor to measure in continuous measurement mode.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iInterval : u32 - Variable for storing the read measure interval in [s]
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_GetDataReady(sCfg[TSCD30_Config], &bool:bDataReady)

    The data ready function is used to determine if a measurement can be read from the sensor’s buffer.


    Whenever there is a measurement available in the internal buffer of the SCD30 "bDataReady" of this function is set to "true". Otherwise it is set to "false". As soon as the measurement has been read using SCD30_ReadMeas() "bDataReady" changes to false.

    It is recommended to use data ready status byte before readout of the measurement values using SCD30_ReadMeas().

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    bDataReady : bool - Variable for storing the data ready status.
    true - Measurement data is available
    false - There are no new measurement data in the SCD30's buffer
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_ReadMeas(sCfg[TSCD30_Config], &Float:fCo2, &Float:fTemperature, &Float:fHumidity)

    Reads measurement data from the SCD30.


    When new measurement data is available it can be read out with this function.

    Make sure that the measurement is completed by reading the data ready status (using SCD30_GetDataReady() ) before reading out the measurement data.

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    fCo2 : f32 - Float variable for storing the CO2 measurement in [ppm]
    fTemperature : f32 - Float variable for storing the temperature measurement in [°C]
    fHumidity : f32 - Float variable for storing the humidity measurement in [%rH]
    returns : s32
    OK - if successful
    ERROR - if one of the following errors occurs
  • CRC mismatch
  • CO2 measurement value is 0.0
  • < OK - If another error occurs
    SCD30_SetTemperatureOffset(sCfg[TSCD30_Config], iTemperatureOffset)

    Sets an offset value of the temperature.


    The on-board rH/T sensor is influenced by thermal self-heating of SCD30 and other electrical components. Design-in alters the thermal properties of SCD30 such that temperature and humidity offsets may occur when operating the sensor in end-customer devices. Compensation of those effects is achievable by writing the temperature offset found in continuous operation of the device into the sensor.

    Temperature offset value is saved in non-volatile memory. The last set value will be used for temperature offset compensation after repowering.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iTemperatureOffset : u16 - Temperature offset in [0.01 °C]
    returns : s32
    OK - if successful
    < OK - if an error occurs
    SCD30_GetTemperatureOffset(sCfg[TSCD30_Config], &iTemperatureOffset)

    Reads the current offset value of the temperature.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iTemperatureOffset : u16 - Variable for storing temperature offset in [0.01 °C]
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_SetAltitude(sCfg[TSCD30_Config], iAltitude)

    Sets the height above sea level.


    Measurements of CO2 concentration based on the NDIR principle are influenced by altitude. SCD30 offers to compensate deviations due to altitude.

    Altitude value is saved in non-volatile memory. The last set value will be used for altitude compensation after repowering.

    Setting altitude is disregarded when an ambient pressure is given to the sensor with the function SCD30_StartPeriodicMeas().

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iAltitude : u16 - Height above sea level in [m] above 0.
    returns : s32
    OK - if successful
    < OK - if an error occurs
    SCD30_GetAltitude(sCfg[TSCD30_Config], &iAltitude)

    Reads the current value for the height above sea level.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iAltitude : u16 - Variable for storing the height above sea level in [m] above 0.
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_SetAutoSelfCalib(sCfg[TSCD30_Config], bool:bEnableASC)

    (De-)Activates Automatic Self-Calibration (ASC).


    Continuous automatic self-calibration can be (de-)activated with this function. When activated for the first time a period of minimum 7 days is needed so that the algorithm can find its initial parameter set for ASC. The sensor has to be exposed to fresh air for at least 1 hour every day. Also during that period, the sensor may not be disconnected from the power supply, otherwise the procedure to find calibration parameters is aborted and has to be restarted from the beginning. The successfully calculated parameters are stored in non-volatile memory of the SCD30 having the effect that after a restart the previously found parameters for ASC are still present. Note that the most recently found self-calibration parameters will be actively used for selfcalibration disregarding the status of this feature. Finding a new parameter set by the here described method will always overwrite the settings from external recalibration (see SCD30_SetForcedRecalib()) and vice-versa. The feature is switched off by default.

    To work properly SCD30 has to see fresh air on a regular basis. Optimal working conditions are given when the sensor sees fresh air for one hour every day so that ASC can constantly re-calibrate. ASC only works in continuous measurement mode.

    ASC status is saved in non-volatile memory. When the sensor is powered down while ASC is activated SCD30 will continue with automatic self-calibration after repowering without sending this command.

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    bEnableASC : bool - (De-)Activate Automatic Self-Calibration (ASC)
    true - Enable ASC
    false - Disable ASC
    returns : s32
    OK - if successful
    < OK - if an error occurs
    SCD30_GetAutoSelfCalib(sCfg[TSCD30_Config], &bool:bASCenabled)

    Reads the current Automatic Self-Calibration (ASC) mode.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    bASCenabled : bool - Variable for storing current Automatic Self-Calibration ASC mode.
    true - ASC is enabled
    false - ASC is disabled
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_SetForcedRecalib(sCfg[TSCD30_Config], iCo2)

    Sets the Forced Recalibration value (FRC)


    Forced recalibration (FRC) is used to compensate for sensor drifts when a reference value of the CO2 concentration in close proximity to the SCD30 is available. For best results the sensor has to be run in a stable environment in continuous mode at a measurement rate of 2s for at least two minutes before applying the calibration command and sending the reference value.

    Setting a reference CO2 concentration by the here described method will always overwrite the corrections from the ASC (see SCD30_SetAutoSelfCalib()) and vice-versa.
    The FRC method imposes a permanent update of the CO2 calibration curve which persists after repowering the sensor. The most recently used reference value is retained in volatile memory and can be read out with the function SCD30_GetForcedRecalib(). After repowering the sensor, the function will return the standard reference value of 400 ppm.

    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iCo2 : u32 - Current Co2 concentration in [ppm].
    Available range: 400 ... 2000 ppm
    returns : s32
    OK - if successful
    < OK - if an error occurs
    SCD30_GetForcedRecalib(sCfg[TSCD30_Config], &iCo2)

    Reads the current Forced Recalibration value (FRC).


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iCo2 : u32 - Variable for storing the current FRC Co2 concentration in [ppm].
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_ReadFirmwareVersion(sCfg[TSCD30_Config], &iMajor, &iMinor)

    Reads the firmware version of the SCD30.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    iMajor : u32 - Variable for storing the major version number
    iMinor : u32 - Variable for storing the minor version number
    returns : s32
    OK - if successful
    ERROR - CRC mismatch
    < OK - If another error occurs
    SCD30_SoftReset(sCfg[TSCD30_Config])

    Resets the SCD30.


    The SCD30 provides a soft reset mechanism that forces the sensor into the same state as after powering up without the need for removing the power-supply. It does so by restarting its system controller. After soft reset the sensor will reload all calibrated data. However, it is worth noting that the sensor reloads calibration data prior to every measurement by default. This includes previously set reference values from ASC or FRC as well as temperature offset values last setting.

    The sensor is able to receive the command at any time, regardless of its internal state.


    sCfg : TSCD30_Config - Device configuration of the SCD30 (Initialized by SCD30_Init() )
    returns : s32
    OK - if successful
    < OK - if an error occurs

    DEBUG

    SCD30_Debug_Config:
    To configure debug mode add the following macro to the main.dde file.
    /** * Use this macro to configure the debug mode: * * 0: No debugging * 1: Issues the following information via the console: * -) When initializing: I2C address used to access the SCD30 * and firmware version of the SCD30 * -) I2C address used, content that was read and result * of the function _SCD30_Read * -) I2C address used, content that should be written and * result of the function _SCD30_Write */ #define SCD30_DEBUG 1

    On this page

    SCD30 - CO2, humidity and temperature sensor