Table of Contents

Class Scd4x

Namespace
Pamoja.Sensors
Assembly
Pamoja.Sensors.dll

A Sensirion SCD40 or SCD41 carbon dioxide sensor, and a driver for one on an I2C bus.

public sealed class Scd4x : IDisposable
Inheritance
Scd4x
Implements
Inherited Members

Remarks

The static members are the part's datasheet, decoded exactly as the manufacturer specifies, and Scd4x.Sim, a part that is not there.

An instance drives the part over an I2cBus in periodic measurement: the part produces a result every five seconds, and Measure() waits for the next one. The part has one address, Address. Nothing is sent until Init() or the first Measure().

Constructors

Scd4x(I2cBus)

Creates a driver for the part on bus.

public Scd4x(I2cBus bus)

Parameters

bus I2cBus

The bus the part is on.

Exceptions

PamojaException

The native driver could not be created.

Fields

Address

The one address the part answers to.

public const byte Address = 98

Field Value

byte

Properties

Serial

The 48-bit serial number read at initialization, or null before it.

public ulong? Serial { get; }

Property Value

ulong?

Methods

AllowedDuringMeasurement(ushort)

Reports whether an SCD4x accepts a command while it is measuring.

public static bool AllowedDuringMeasurement(ushort command)

Parameters

command ushort

The command.

Returns

bool

The value the C ABI computes.

AmbientPressurePascals(ushort)

Reads an SCD4x ambient-pressure word back as a pressure.

public static uint AmbientPressurePascals(ushort word)

Parameters

word ushort

The word.

Returns

uint

The value the C ABI computes.

AmbientPressureWord(uint)

Builds the SCD4x ambient-pressure word for a pressure.

public static ushort AmbientPressureWord(uint pascals)

Parameters

pascals uint

The pascals.

Returns

ushort

The value the C ABI computes.

AutomaticSelfCalibrationEnabled(ushort)

Reports whether an SCD4x word says automatic self-calibration is on.

public static bool AutomaticSelfCalibrationEnabled(ushort word)

Parameters

word ushort

The word.

Returns

bool

The value the C ABI computes.

AutomaticSelfCalibrationWord(bool)

Builds the SCD4x word that turns automatic self-calibration on or off.

public static ushort AutomaticSelfCalibrationWord(bool enabled)

Parameters

enabled bool

The enabled.

Returns

ushort

The value the C ABI computes.

Celsius(ushort)

Converts a raw SCD4x temperature word to degrees Celsius.

public static float Celsius(ushort raw)

Parameters

raw ushort

The raw.

Returns

float

The value the C ABI computes.

CommandFrame(ushort)

Builds the two bytes that address an SCD4x command, most significant first.

public static byte[] CommandFrame(ushort command)

Parameters

command ushort

The command.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.

Crc(ReadOnlySpan<byte>)

Computes the CRC-8 an SCD4x appends to every data word.

public static byte Crc(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The data.

Returns

byte

The value the C ABI computes.

DataReady()

Asks the part whether a periodic result is waiting.

public bool DataReady()

Returns

bool

Whether Measure() would read without waiting.

Exceptions

PamojaException

The transfer failed, or the status word failed its checksum.

DataReady(ushort)

Reports whether an SCD4x data-ready word says a fresh result is waiting.

public static bool DataReady(ushort word)

Parameters

word ushort

The word.

Returns

bool

The value the C ABI computes.

Dispose()

Releases the driver and its share of the bus.

public void Dispose()

ForcedRecalibrationCorrectionPpm(ushort)

Reads the correction a forced recalibration applied.

public static int? ForcedRecalibrationCorrectionPpm(ushort word)

Parameters

word ushort

The word.

Returns

int?

The value, or null where the part defines none.

ForcedRecalibrationWord(bool, int)

Builds the word an SCD4x returns for a forced-recalibration outcome.

public static ushort ForcedRecalibrationWord(bool succeeded, int correctionPpm)

Parameters

succeeded bool

The succeeded.

correctionPpm int

The correction ppm.

Returns

ushort

The value the C ABI computes.

HumidityMilliPercent(ushort)

Converts a raw SCD4x humidity word to milli-percent.

public static uint HumidityMilliPercent(ushort raw)

Parameters

raw ushort

The raw.

Returns

uint

The value the C ABI computes.

HumidityRaw(uint)

Builds the SCD4x humidity word that decodes to a relative humidity.

public static ushort HumidityRaw(uint milliPercent)

Parameters

milliPercent uint

The milli percent.

Returns

ushort

The value the C ABI computes.

Init()

Stops any running measurement, reads the serial number, and starts periodic measurement.

public void Init()

Exceptions

PamojaException

Nothing answered, or the serial number failed its checksum.

MaxDurationMs(ushort)

Returns how long an SCD4x command may take before its result can be read.

public static ushort? MaxDurationMs(ushort command)

Parameters

command ushort

The command.

Returns

ushort?

The value, or null where the part defines none.

Measure()

Waits for the next periodic result and reads it, initializing the part first if needed.

public PamojaScd4xMeasurement Measure()

Returns

PamojaScd4xMeasurement

The carbon dioxide, temperature, and humidity.

Exceptions

PamojaException

As Init(), when no result becomes ready, and when a word fails its checksum.

MeasureSingleShot()

Runs one on-demand measurement on an SCD41, which takes five seconds.

public PamojaScd4xMeasurement MeasureSingleShot()

Returns

PamojaScd4xMeasurement

The measurement.

Remarks

The part must not be measuring periodically: call Stop() first, or use this instead of Init().

Exceptions

PamojaException

As Measure().

MeasurementBytes(ushort, ushort, ushort)

Builds the nine bytes an SCD4x sends for a set of raw words, each CRC included.

public static byte[] MeasurementBytes(ushort co2Ppm, ushort temperatureRaw, ushort humidityRaw)

Parameters

co2Ppm ushort

The co2 ppm.

temperatureRaw ushort

The temperature raw.

humidityRaw ushort

The humidity raw.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.

MeasurementFromPhysical(ushort, int, uint)

Builds the SCD4x measurement a sensor reporting these physical values would send.

public static PamojaScd4xMeasurement MeasurementFromPhysical(ushort co2Ppm, int milliCelsius, uint humidityMilliPercent)

Parameters

co2Ppm ushort

The co2 ppm.

milliCelsius int

The milli celsius.

humidityMilliPercent uint

The humidity milli percent.

Returns

PamojaScd4xMeasurement

The decoded value.

Exceptions

PamojaException

The device data is not valid.

MilliCelsius(ushort)

Converts a raw SCD4x temperature word to milli-degrees Celsius.

public static int MilliCelsius(ushort raw)

Parameters

raw ushort

The raw.

Returns

int

The value the C ABI computes.

ParseMeasurement(ReadOnlySpan<byte>)

Parses and CRC-checks a nine-byte SCD4x measurement frame.

public static PamojaScd4xMeasurement ParseMeasurement(ReadOnlySpan<byte> frame)

Parameters

frame ReadOnlySpan<byte>

The frame.

Returns

PamojaScd4xMeasurement

The decoded value.

Exceptions

PamojaException

The device data is not valid.

RelativeHumidityPercent(ushort)

Converts a raw SCD4x humidity word to a relative humidity percentage.

public static float RelativeHumidityPercent(ushort raw)

Parameters

raw ushort

The raw.

Returns

float

The value the C ABI computes.

SelfTestPassed(ushort)

Reports whether an SCD4x self-test word says the part is healthy.

public static bool SelfTestPassed(ushort word)

Parameters

word ushort

The word.

Returns

bool

The value the C ABI computes.

SerialNumber(ReadOnlySpan<byte>)

Reads the 48-bit serial number out of a nine-byte SCD4x frame.

public static ulong SerialNumber(ReadOnlySpan<byte> frame)

Parameters

frame ReadOnlySpan<byte>

The frame.

Returns

ulong

The decoded value.

Exceptions

PamojaException

The device data is not valid.

SerialNumberFrame(ulong)

Builds the nine bytes an SCD4x sends for a serial number, each CRC included.

public static byte[] SerialNumberFrame(ulong serial)

Parameters

serial ulong

The serial.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.

SetSensorAltitude(ushort)

Sets the altitude the part corrects its carbon dioxide reading for.

public void SetSensorAltitude(ushort meters)

Parameters

meters ushort

The altitude above sea level.

Exceptions

PamojaException

The transfer failed.

SetTemperatureOffset(uint)

Sets the temperature offset that compensates the part's own warmth, until power is lost.

public void SetTemperatureOffset(uint milliCelsius)

Parameters

milliCelsius uint

The offset to subtract, in millidegrees.

Exceptions

PamojaException

The transfer failed.

Start()

Starts periodic measurement.

public void Start()

Exceptions

PamojaException

The transfer failed.

Stop()

Stops periodic measurement, after which the part takes its settings commands.

public void Stop()

Exceptions

PamojaException

The transfer failed.

TemperatureOffsetMilliCelsius(ushort)

Reads an SCD4x temperature-offset word back as an offset.

public static uint TemperatureOffsetMilliCelsius(ushort word)

Parameters

word ushort

The word.

Returns

uint

The value the C ABI computes.

TemperatureOffsetWord(uint)

Builds the SCD4x temperature-offset word for an offset.

public static ushort TemperatureOffsetWord(uint milliCelsius)

Parameters

milliCelsius uint

The milli celsius.

Returns

ushort

The value the C ABI computes.

TemperatureRaw(int)

Builds the SCD4x temperature word that decodes to a temperature.

public static ushort TemperatureRaw(int milliCelsius)

Parameters

milliCelsius int

The milli celsius.

Returns

ushort

The value the C ABI computes.

Word(ReadOnlySpan<byte>)

Reads a CRC-checked three-byte SCD4x word frame.

public static ushort Word(ReadOnlySpan<byte> frame)

Parameters

frame ReadOnlySpan<byte>

The frame.

Returns

ushort

The decoded value.

Exceptions

PamojaException

The device data is not valid.

WordFrame(ushort)

Builds the three bytes an SCD4x sends for a word: the word then its CRC.

public static byte[] WordFrame(ushort value)

Parameters

value ushort

The value.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.

WriteFrame(ushort, ushort)

Builds the five bytes that write a word to an SCD4x: command, word, CRC.

public static byte[] WriteFrame(ushort command, ushort value)

Parameters

command ushort

The command.

value ushort

The value.

Returns

byte[]

The decoded value.

Exceptions

PamojaException

The device data is not valid.