Remarks

Sgp40
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Sgp40

Code Example

Sgp40? sensor;

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

    sensor = new Sgp40(Device.CreateI2cBus());

    Resolver.Log.Info($"Sensor SN: {sensor.SerialNumber:x6}");

    if (sensor.RunSelfTest())
    {
        Resolver.Log.Info("Self test successful");
    }
    else
    {
        Resolver.Log.Warn("Self test failed");
    }

    var consumer = Sgp40.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"Observer: VOC changed by threshold; new index: {result.New}");
        },
        filter: result =>
        {
            //c# 8 pattern match syntax. checks for !null and assigns var.
            return Math.Abs(result.New - result.Old ?? 0) > 10;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) =>
    {
        Resolver.Log.Info($"  VOC: {result.New}");
    };

    return base.Initialize();
}

public override async Task Run()
{
    await ReadConditions();

    sensor?.StartUpdating(TimeSpan.FromSeconds(1));
}

async Task ReadConditions()
{
    if(sensor == null) { return; }

    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"  Temperature: {result}");
}

Sample project(s) available on GitHub

Wiring Example

To wire a Sgp40 to your Meadow board, connect the following:

Sgp40 Meadow Pin
GND GND
SCL D08 (SCL)
SDA D07 (SDA)
VCC 3V3
Characteristic Locus
Inheritance System.Object ObservableBase<System.Int32> SensorBase<System.Int32> SamplingSensorBase<System.Int32> ByteCommsSensorBase<System.Int32> > Sgp40
Implements ISamplingSensor<System.Int32> IDisposable
Inherited Members ByteCommsSensorBase<Int32>.Peripheral ByteCommsSensorBase<Int32>.ReadBuffer ByteCommsSensorBase<Int32>.WriteBuffer ByteCommsSensorBase<Int32>.Init(Int32, Int32) ByteCommsSensorBase<Int32>.Dispose(Boolean) ByteCommsSensorBase<Int32>.Dispose() SamplingSensorBase<Int32>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Int32>.StopUpdating() SensorBase<Int32>.Updated SensorBase<Int32>.samplingLock SensorBase<Int32>.SamplingTokenSource SensorBase<Int32>.Conditions SensorBase<Int32>.IsSampling SensorBase<Int32>.UpdateInterval SensorBase<Int32>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<Int32>.Read() ObservableBase<Int32>.observers ObservableBase<Int32>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.Int32>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.Int32>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly Sgp40.dll

Syntax

public class Sgp40 : ByteCommsSensorBase<int>, ISamplingSensor<int>, IDisposable

Constructors

Sgp40(II2cBus, Byte)

Creates a new SGP40 VOC sensor.

Declaration
public Sgp40(II2cBus i2cBus, byte address = null)

Parameters

Type Name Description
II2cBus i2cBus

I2CBus.

System.Byte address

Sensor address (default to 0x40).

Properties

SerialNumber

Serial number of the device.

Declaration
public ulong SerialNumber { get; }

Property Value

Type Description
System.UInt64

VocIndex

The VOC Index, from the last reading.

Declaration
public int VocIndex { get; }

Property Value

Type Description
System.Int32

Methods

ClearCompensationData()

Clear the compensation data

Declaration
public void ClearCompensationData()

Initialize()

Initalize the sensor

Declaration
protected void Initialize()

RaiseEventsAndNotify(IChangeResult<Int32>)

Inheritance-safe way to raise events and notify observers.

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<int> changeResult)

Parameters

Type Name Description
IChangeResult<System.Int32> changeResult

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<int> ReadSensor()

Returns

Type Description
Task<System.Int32>

The latest sensor reading

Overrides

Meadow.Foundation.SensorBase<System.Int32>.ReadSensor()

RunSelfTest()

This command triggers the built-in self-test checking for integrity of both hotplate and MOX material

Declaration
public bool RunSelfTest()

Returns

Type Description
System.Boolean

true on sucessful test, otherwise false

SetCompensationData(RelativeHumidity, Units.Temperature)

Set the compensation data

Declaration
public void SetCompensationData(RelativeHumidity humidity, Units.Temperature temperature)

Parameters

Type Name Description
RelativeHumidity humidity

Humidity compensation

Units.Temperature temperature

Temperature compensation

TurnHeaterOff()

This command turns the hotplate off and stops the measurement. Subsequently, the sensor enters idle mode.

Declaration
public void TurnHeaterOff()

Events

VocIndexUpdated

Declaration
public event EventHandler<ChangeResult<int>> VocIndexUpdated

Event Type

Type Description
EventHandler<ChangeResult<System.Int32>>