Remarks
Sgp40 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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 |
Syntax
public class Sgp40 : ByteCommsSensorBase<int>, ISamplingSensor<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
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>> |