Characteristic Locus
Inheritance System.Object > BME280
Implements IAtmosphericSensor ITemperatureSensor IHumiditySensor IBarometricPressureSensor
Namespace Meadow.Foundation.Sensors.Atmospheric
Assembly BME280.dll

Syntax

public class BME280 : FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>, IAtmosphericSensor, ITemperatureSensor, IHumiditySensor, IBarometricPressureSensor

Remarks

The BME280 is a combined temperature, pressure and humidity sensor.

Purchasing

The BME280 sensor is available as a breakout board from the following suppliers:

Examples

The BME280 can operating in polling and interrupt mode. By default, this sensor operates in interrupt mode.

Interrupt Mode

When the driver is operating in interrupt mode, the driver will periodically check the sensor reading. An interrupt will be generated if the difference between the last reported reading and the current reading is greater than a threshold value.

The sensor operates in interrupt mode by default.

The following application will generate interrupts when changes to any one of the temperature, humidity or pressure readings exceed their threshold values:

using System;
using System.Threading;
using System.Threading.Tasks;
using Meadow;
using Meadow.Devices;
using Meadow.Peripherals.Sensors.Atmospheric;
using Meadow.Foundation.Sensors.Atmospheric;
using Meadow.Hardware;

namespace BME280_Sample
{
    public class Program
    {
        static IApp app;
        public static void Main(string[] args)
        {
            if (args.Length > 0 && args[0] == "--exitOnDebug") return;

            // instantiate and run new meadow app
            app = new MeadowApp();
            Thread.Sleep(Timeout.Infinite);
        }
    }

    public class MeadowApp : App<F7Micro, MeadowApp>
    {
        BME280 bme280;

        public MeadowApp()
        {
            Console.WriteLine("Initializing...");

            // configure our BME280 on the I2C Bus
            var i2c = Device.CreateI2cBus();
            bme280 = new BME280 (
                i2c,
                BME280.I2cAddress.Adddress0x77 //default
            );

            // TODO: SPI version

            // Example that uses an IObservable subscription to only be notified
            // when the temperature changes by at least a degree, and humidty by 5%.
            // (blowing hot breath on the sensor should trigger)
            bme280.Subscribe(new FilterableObserver<AtmosphericConditionChangeResult, AtmosphericConditions>(
                h => {
                    Console.WriteLine($"Temp and pressure changed by threshold; new temp: {h.New.Temperature}, old: {h.Old.Temperature}");
                },
                e => {
                    return (
                        (Math.Abs(e.Delta.Temperature) > 1)
                        &&
                        (Math.Abs(e.Delta.Pressure) > 5)
                        );
                }
            ));

            // classical .NET events can also be used:
            bme280.Updated += (object sender, AtmosphericConditionChangeResult e) => 
            {
                Console.WriteLine($"  Temperature: {e.New.Temperature}ºC");
                Console.WriteLine($"  Pressure: {e.New.Pressure}hPa");
                Console.WriteLine($"  Relative Humidity: {e.New.Humidity}%");
            };

            // get chip id
            Console.WriteLine($"ChipID: {bme280.GetChipID():X2}");            

            // get an initial reading
            ReadConditions().Wait();

            // start updating continuously
            bme280.StartUpdating();
        }

        protected async Task ReadConditions()
        {
            var conditions = await bme280.Read();
            Console.WriteLine("Initial Readings:");
            Console.WriteLine($"  Temperature: {conditions.Temperature}ºC");
            Console.WriteLine($"  Pressure: {conditions.Pressure}hPa");
            Console.WriteLine($"  Relative Humidity: {conditions.Humidity}%");
        }
    }
}

Polling Mode

In polling mode, it is the responsibility of the main application to check the sensor readings ona periodic basis. The following application creates an instance of the BME280 class using the I2C interface. The temperature, pressure and humidity are read every second and the readings displayed using the debugger.

The sensor is put into polling mode by setting the updateInterval to 0 in the constructor.

using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Atmospheric;

namespace BME280_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public class App : AppBase<F7Micro, App>
    {
        public App ()
        {
            // Create a new BME280 object and put the sensor into polling
            // mode (update interval set to 0ms).
            BME280 sensor = new BME280(updateInterval: 0);

            string message;
            while (true)
            {
                // Make the sensor take new readings.
                sensor.Update();

                // Prepare a message for the user and output to the debug console.
                message = "Temperature: " + sensor.Temperature.ToString("F1") + " C\n";
                message += "Humidity: " + sensor.Humidity.ToString("F1") + " %\n";
                message += "Pressure: " + (sensor.Pressure / 100).ToString("F0") + " hPa\n\n";
                Console.WriteLine(message);

                // Sleep for 1000ms before repeating the process.
                Thread.Sleep(1000);
            }
        }
    }
}
Example Circuit

The BME280 can be connected using I2C or SPI. Only 4 wires are required when using I2C:

  • 3.3V
  • Ground
  • SDA
  • SCL

It should be noted that the Sparkfun board is supplied with pull-up resistors enabled by default. The Adafruit board does not have any pull-up resistors onboard. It is therefore necessary to add two pull-up resistors (4.7 K should be adequate for a single device) between 3.3V and SDA and 3.3V and SCL.

Constructors

View Source

BME280(II2cBus, BME280.I2cAddress)

Initializes a new instance of the Meadow.Foundation.Sensors.Barometric.BME280 class.

Declaration
public BME280(II2cBus i2c, BME280.I2cAddress busAddress = BME280.I2cAddress.Adddress0x77)

Parameters

Type Name Description
II2cBus i2c

I2C Bus to use for communicating with the sensor

BME280.I2cAddress busAddress

I2C address of the sensor (default = 0x77).

View Source

BME280(ISpiBus, IDigitalOutputPort)

Declaration
public BME280(ISpiBus spi, IDigitalOutputPort chipSelect)

Parameters

Type Name Description
ISpiBus spi
IDigitalOutputPort chipSelect

Fields

View Source

_compensationData

Compensation data from the sensor.

Declaration
protected BME280.CompensationData _compensationData

Field Value

Type Description
BME280.CompensationData
View Source

configuration

Declaration
protected BME280.Configuration configuration

Field Value

Type Description
BME280.Configuration

Properties

View Source

Conditions

The AtmosphericConditions from the last reading.

Declaration
public AtmosphericConditions Conditions { get; protected set; }

Property Value

Type Description
AtmosphericConditions
View Source

Humidity

The humidity, in percent relative humidity, from the last reading..

Declaration
public float Humidity { get; }

Property Value

Type Description
System.Single
View Source

IsSampling

Gets a value indicating whether the analog input port is currently sampling the ADC. Call StartSampling() to spin up the sampling process.

Declaration
public bool IsSampling { get; protected set; }

Property Value

Type Description
System.Boolean

true if sampling; otherwise, false.

View Source

Pressure

The pressure, in hectopascals (hPa), from the last reading. 1 hPa is equal to one millibar, or 1/10th of a kilopascal (kPa)/centibar.

Declaration
public float Pressure { get; }

Property Value

Type Description
System.Single
View Source

Temperature

The temperature, in degrees celsius (ºC), from the last reading.

Declaration
public float Temperature { get; }

Property Value

Type Description
System.Single

Methods

View Source

GetChipID()

Declaration
public byte GetChipID()

Returns

Type Description
System.Byte
View Source

Init()

Declaration
protected void Init()
View Source

RaiseChangedAndNotify(AtmosphericConditionChangeResult)

Declaration
protected void RaiseChangedAndNotify(AtmosphericConditionChangeResult changeResult)

Parameters

Type Name Description
AtmosphericConditionChangeResult changeResult
View Source

Read()

Update the sensor information from the BME280.

Declaration
protected Task<AtmosphericConditions> Read()

Returns

Type Description
System.Threading.Tasks.Task<AtmosphericConditions>

Remarks

Reads the raw temperature, pressure and humidity data from the BME280 and applies the compensation data to get the actual readings. These are made available through the Temperature, Pressure and Humidity properties. All three readings are taken at once to ensure that the three readings are consistent. Register locations and formulas taken from the Bosch BME280 datasheet revision 1.1, May 2015. Register locations - section 5.3 Memory Map Formulas - section 4.2.3 Compensation Formulas The integer formulas have been used to try and keep the calculations performant.

View Source

Read(BME280.Oversample, BME280.Oversample, BME280.Oversample)

Convenience method to get the current sensor readings. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

Declaration
public Task<AtmosphericConditions> Read(BME280.Oversample temperatureSampleCount = BME280.Oversample.OversampleX8, BME280.Oversample pressureSampleCount = BME280.Oversample.OversampleX8, BME280.Oversample humiditySampleCount = BME280.Oversample.OversampleX8)

Parameters

Type Name Description
BME280.Oversample temperatureSampleCount

The number of sample readings to take. Must be greater than 0. These samples are automatically averaged.

BME280.Oversample pressureSampleCount
BME280.Oversample humiditySampleCount

Returns

Type Description
System.Threading.Tasks.Task<AtmosphericConditions>
View Source

ReadCompensationData()

Reads the compensation data.

Declaration
protected void ReadCompensationData()

Remarks

The compensation data is written to the chip at the time of manufacture and cannot be changed. This information is used to convert the readings from the sensor into actual temperature, pressure and humidity readings. From the data sheet, the register addresses and length are: Temperature and pressure: start address 0x88, end address 0x9F (length = 24) Humidity 1: 0xa1, length = 1 Humidity 2 and 3: start address 0xe1, end address 0xe7, (length = 8)

View Source

Reset()

Reset the sensor.

Declaration
public void Reset()

Remarks

Perform a full power-on-reset of the sensor and reset the configuration of the sensor.

View Source

StartUpdating(BME280.Oversample, BME280.Oversample, BME280.Oversample, Int32)

Declaration
public void StartUpdating(BME280.Oversample temperatureSampleCount = BME280.Oversample.OversampleX8, BME280.Oversample pressureSampleCount = BME280.Oversample.OversampleX8, BME280.Oversample humiditySampleCount = BME280.Oversample.OversampleX1, int standbyDuration = 1000)

Parameters

Type Name Description
BME280.Oversample temperatureSampleCount
BME280.Oversample pressureSampleCount
BME280.Oversample humiditySampleCount
System.Int32 standbyDuration
View Source

StopUpdating()

Stops sampling the temperature.

Declaration
public void StopUpdating()
View Source

UpdateConfiguration(BME280.Configuration)

Update the configuration for the BME280.

Declaration
protected void UpdateConfiguration(BME280.Configuration configuration)

Parameters

Type Name Description
BME280.Configuration configuration

Remarks

This method uses the data in the configuration properties in order to set up the BME280. Ensure that the following are set correctly before calling this method:

  • Standby
  • Filter
  • HumidityOverSampling
  • TemperatureOverSampling
  • PressureOverSampling
  • Mode

Events

View Source

Updated

Declaration
public event EventHandler<AtmosphericConditionChangeResult> Updated

Event Type

Type Description
System.EventHandler<AtmosphericConditionChangeResult>