Meadow.Foundation.Sensors.Atmospheric.Ccs811
Ccs811 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Ccs811 sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initializing...");
var i2cBus = Device.CreateI2cBus(Meadow.Hardware.I2cBusSpeed.Fast);
sensor = new Ccs811(i2cBus);
var consumer = Ccs811.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer triggered:");
Resolver.Log.Info($" new CO2: {result.New.Co2?.PartsPerMillion:N1}ppm, old: {result.Old?.Co2?.PartsPerMillion:N1}ppm.");
Resolver.Log.Info($" new VOC: {result.New.Voc?.PartsPerBillion:N1}ppb, old: {result.Old?.Voc?.PartsPerBillion:N1}ppb.");
},
filter: result =>
{
if (result.Old is { } old)
{
return (
(result.New.Co2.Value - old.Co2.Value).Abs().PartsPerMillion > 1000 // 1000ppm
&&
(result.New.Voc.Value - old.Voc.Value).Abs().PartsPerBillion > 100 // 100ppb
);
}
return false;
}
);
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"CO2: {result.New.Co2.Value.PartsPerMillion:n1}ppm, VOC: {result.New.Voc.Value.PartsPerBillion:n1}ppb");
};
return Task.CompletedTask;
}
public override async Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($" CO2: {result.Co2.Value.PartsPerMillion:n1}ppm");
Resolver.Log.Info($" VOC: {result.Voc.Value.PartsPerBillion:n1}ppb");
sensor.StartUpdating(TimeSpan.FromSeconds(1));
}
Sample project(s) available on GitHub
Wiring Example
To wire a Ccs811 to your Meadow board, connect the following:
Ccs811 | Meadow Pin |
---|---|
GND | GND |
VCC | 3V3 |
SCL | D08 |
SDA | D07 |
It should look like the following diagram:
Class Ccs811
Provide access to the CCS811 C02 and VOC Air Quality Sensor
Assembly: Ccs811.dll
View Source
public class Ccs811 : PollingSensorBase<(Concentration? Co2, Concentration? Voc)>, IObservable<IChangeResult<(Concentration? Co2, Concentration? Voc)>>, ISamplingSensor<(Concentration? Co2, Concentration? Voc)>, ISensor<(Concentration? Co2, Concentration? Voc)>, ICo2Sensor, IVocSensor, ISamplingSensor<Concentration>, ISensor<Concentration>, ISensor, ISamplingSensor, II2cPeripheral
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
Expand
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>>
, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>
, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>
, Meadow.Peripherals.Sensors.Atmospheric.ICo2Sensor
, Meadow.Peripherals.Sensors.Atmospheric.IVocSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Concentration>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Concentration>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.II2cPeripheral
Properties
Co2
The measured CO2 concentration
View Source
public Concentration? Co2 { get; }
Voc
The measured VOC concentration
View Source
public Concentration? Voc { get; }
DefaultI2cAddress
The default I2C address for the peripheral
View Source
public byte DefaultI2cAddress { get; }
Methods
Initialize()
Initialize the sensor
View Source
protected void Initialize()
Exceptions
System.Exception
Raised if HW_ID register returns an invalid id
GetBaseline()
Get baseline value
View Source
public ushort GetBaseline()
Returns
System.UInt16
: The baseline value### SetBaseline(ushort)
Set the baseline value
View Source
public void SetBaseline(ushort value)
Parameters
Type | Name | Description |
---|---|---|
System.UInt16 | value | The new baseline |
GetMeasurementMode()
Get the current measurement mode
View Source
public Ccs811.MeasurementMode GetMeasurementMode()
Returns
Meadow.Foundation.Sensors.Atmospheric.Ccs811.MeasurementMode: The measurement mode### SetMeasurementMode(MeasurementMode) Set the Measurement mode
View Source
public void SetMeasurementMode(Ccs811.MeasurementMode mode)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Foundation.Sensors.Atmospheric.Ccs811.MeasurementMode | mode | The new mode |
ReadSensor()
Reads data from the sensor
View Source
protected override Task<(Concentration? Co2, Concentration? Voc)> ReadSensor()
Returns
System.Threading.Tasks.Task<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>
: The latest sensor reading### RaiseEventsAndNotify(IChangeResult<(Concentration? Co2, Concentration? Voc)>)
Raise events for subscribers and notify of value changes
View Source
protected override void RaiseEventsAndNotify(IChangeResult<(Concentration? Co2, Concentration? Voc)> changeResult)
Parameters
Type | Name | Description |
---|---|---|
Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>> | changeResult | The updated sensor data |
Events
VocUpdated
Event raised when the VOC concentration value changes
View Source
public event EventHandler<ChangeResult<Concentration>> VocUpdated
Event Type
System.EventHandler<Meadow.ChangeResult<Meadow.Units.Concentration>>
Implements
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>>
Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>
Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Concentration>,System.Nullable<Meadow.Units.Concentration>>>
Meadow.Peripherals.Sensors.Atmospheric.ICo2Sensor
Meadow.Peripherals.Sensors.Atmospheric.IVocSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Concentration>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Concentration>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.II2cPeripheral