Remarks

Scd40
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Environmental.Scd4x

Code Example

Scd40 sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    var i2cBus = Device.CreateI2cBus(Meadow.Hardware.I2cBusSpeed.Standard);

    sensor = new Scd40(i2cBus);
    var serialNum = sensor.GetSerialNumber();
    Resolver.Log.Info($"Serial: {BitConverter.ToString(serialNum)}");

    var consumer = Scd40.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );

    sensor?.Subscribe(consumer);

    if (sensor != null)
    {
        sensor.Updated += (sender, result) =>
        {
            Resolver.Log.Info($"  Concentration: {result.New.Concentration?.PartsPerMillion:N0}ppm");
            Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N1}C");
            Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N0}%");
        };
    }

    sensor?.StartUpdating(TimeSpan.FromSeconds(6));

    return base.Initialize();
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance object ObservableBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> PollingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> Scd4xBase > Scd40
Implements IObservable<IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>> ISamplingSensor<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> ISensor<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)> IDisposable ITemperatureSensor ISamplingSensor<Temperature> ISensor<Temperature> IHumiditySensor ISamplingSensor<RelativeHumidity> ISensor<RelativeHumidity> IConcentrationSensor ISamplingSensor<Concentration> ISensor<Concentration> II2cPeripheral
Inherited Members Scd4xBase.ConcentrationUpdated Scd4xBase.TemperatureUpdated Scd4xBase.HumidityUpdated Scd4xBase.Concentration Scd4xBase.Temperature Scd4xBase.Humidity Scd4xBase.DefaultI2cAddress Scd4xBase.ReInitialize() Scd4xBase.PerformForcedRecalibration() Scd4xBase.PersistSettings() Scd4xBase.PerformFactoryReset() Scd4xBase.GetSerialNumber() Scd4xBase.IsDataReady() Scd4xBase.StartUpdating(TimeSpan?) Scd4xBase.StopUpdating() Scd4xBase.ReadSensor() Scd4xBase.RaiseChangedAndNotify(IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>) ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.BusComms ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.ReadBuffer ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.WriteBuffer ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Init(int, int) ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose(bool) ByteCommsSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Dispose() PollingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.StartUpdating(TimeSpan?) PollingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.StopUpdating() SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.samplingLock SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Updated SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.SamplingTokenSource SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Conditions SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.IsSampling SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.UpdateInterval SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.ReadSensor() SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.RaiseEventsAndNotify(IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>) SamplingSensorBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Read() ObservableBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.observers ObservableBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.NotifyObservers(IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>) ObservableBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.Subscribe(IObserver<IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>>) ObservableBase<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>.CreateObserver(Action<IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>>, Predicate<IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>>) object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Sensors.Environmental
Assembly Scd4x.dll

Syntax

public class Scd40 : Scd4xBase, IObservable<IChangeResult<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>>, ISamplingSensor<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>, ISensor<(Concentration? Concentration, Temperature? Temperature, RelativeHumidity? Humidity)>, IDisposable, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, IHumiditySensor, ISamplingSensor<RelativeHumidity>, ISensor<RelativeHumidity>, IConcentrationSensor, ISamplingSensor<Concentration>, ISensor<Concentration>, II2cPeripheral

Constructors

Scd40(II2cBus, byte)

Create a new Scd40 object

Declaration
public Scd40(II2cBus i2cBus, byte address = 98)

Parameters

Type Name Description
II2cBus i2cBus

The I2C bus

byte address

The I2C address

Remarks

Scd40
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Environmental.Scd4x

Code Example

Scd40 sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    var i2cBus = Device.CreateI2cBus(Meadow.Hardware.I2cBusSpeed.Standard);

    sensor = new Scd40(i2cBus);
    var serialNum = sensor.GetSerialNumber();
    Resolver.Log.Info($"Serial: {BitConverter.ToString(serialNum)}");

    var consumer = Scd40.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            if (result.Old?.Temperature is { } oldTemp &&
                result.Old?.Humidity is { } oldHumidity &&
                result.New.Temperature is { } newTemp &&
                result.New.Humidity is { } newHumidity)
            {
                return ((newTemp - oldTemp).Abs().Celsius > 0.5 &&
                        (newHumidity - oldHumidity).Percent > 0.05);
            }
            return false;
        }
    );

    sensor?.Subscribe(consumer);

    if (sensor != null)
    {
        sensor.Updated += (sender, result) =>
        {
            Resolver.Log.Info($"  Concentration: {result.New.Concentration?.PartsPerMillion:N0}ppm");
            Resolver.Log.Info($"  Temperature: {result.New.Temperature?.Celsius:N1}C");
            Resolver.Log.Info($"  Relative Humidity: {result.New.Humidity:N0}%");
        };
    }

    sensor?.StartUpdating(TimeSpan.FromSeconds(6));

    return base.Initialize();
}

Sample project(s) available on GitHub