BME280 Environmental Sensor

This documentation includes installation guidelines and sample code for your hardware.

Windows 10 IoT Core Library

We upgraded the BME280 sensor to BME680 in the IoT HAT revision 2. We're keeping this documentation for backward compatibility. If you have the IoT HAT revision 2, please use the BME680 library.

The new BME680 sensor adds gas resistance (hence, air quality) reading to the IoT HAT.

BME280Sensor library is responsible for communicating with the BME280 sensor.

To use the library, add the following using statement to the top of your C# code.

using TurtaIoTHAT;

Then, create an instance of the BME280Sensor class.

BME280Sensor bme;

Now you're ready to access the library by calling the bme instance.

Initialization

BME280Sensor

Initiates the BME280 sensor to get temperature, humidity, pressure and altitude.

BME280Sensor()
  • Parameters

    • None

Basic Members

Read Temperature

Reads the temperature in Celcius.

double ReadTemperature()
  • Parameters

    • None

  • Returns

    • Double: Temperature in Celcius.

Read Humidity

Reads the relative humidity.

double ReadHumidity()
  • Parameters

    • None

  • Returns

    • Double: Relative humidity.

Read Pressure

Reads the pressure in Pa.

double ReadPressure()
  • Parameters

    • None

  • Returns

    • Double: Pressure in Pa.

Calculate Altitude

Reads the altitude from the sea level in meters.

double ReadAltitude(double meanSeaLevelPressureInBar)
  • Parameters

    • Double meanSeaLevelPressureInBar: Mean sea level pressure in bar.

  • Returns

    • Double: Altitude from the sea level in meters.

Advanced Members

Set Oversamplings and Mode

Sets the oversamplings and sensor mode.

async Task<bool> SetOversamplingsAndMode(HumidityOversampling ho, TemperatureOversampling to, PressureOversampling po, SensorMode mode)
  • Parameters

    • HumidityOversampling ho: Humidity oversampling.

    • TemperatureOversampling to: Temperature oversampling.

    • PressureOversampling po: Pressure oversampling.

    • SensorMode mode: Sensor mode.

  • Returns

    • Bool: True if successful. False if not.

Set Configuration

Sets the sensor configuration.

async Task<bool> SetConfig(InactiveDuration id, FilterCoefficient fc)
  • Parameters

    • InactiveDuration id: Inactive duration between normal mode measurements.

    • FilterCoefficient fc: Filter coefficient.

  • Returns

    • Bool: True if successful. False if not.

Disposal

To clean up the resources used by the class, call the Dispose() method it when the application is closing.

void Dispose()

This method cleans the resources used by the class.

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There is one example of this library.

BME280 Sample App

This application demonstrates reading environmental data from the sensor.

To run the example:

  1. Open the sample project you downloaded.

  2. Make sure "ARM" is selected from the solution platforms selection.

  3. Deploy the sample to the Raspberry Pi using the "F5" key.

Sample Code

MainPage.xaml.cs
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using TurtaIoTHAT;

namespace BME280SampleApp
{
    public sealed partial class MainPage : Page
    {
        // BME280 Sensor
        static BME280Sensor bme;

        // Sea level pressure in bar
        // Update this from weather forecast to get precise altitude
        static double slp = 1033.0;

        // Sensor timer
        Timer sensorTimer;

        public MainPage()
        {
            this.InitializeComponent();

            // Initialize sensor and timer
            Initialize();
        }

        private async void Initialize()
        {
            // Initialize and configure sensor
            await InitializeBME280();

            // Configure timer to 2000ms delayed start and 2000ms interval
            sensorTimer = new Timer(new TimerCallback(SensorTimerTick), null, 2000, 2000);
        }

        private async Task InitializeBME280()
        {
            // Create sensor instance
            bme = new BME280Sensor();

            // Optional advanced sensor configuration
            await bme.SetOversamplingsAndMode(
                BME280Sensor.HumidityOversampling.x04,
                BME280Sensor.TemperatureOversampling.x04,
                BME280Sensor.PressureOversampling.x04,
                BME280Sensor.SensorMode.Normal);

            // Optional advanced sensor configuration
            await bme.SetConfig(
                BME280Sensor.InactiveDuration.ms0500,
                BME280Sensor.FilterCoefficient.fc04);
        }

        private static void SensorTimerTick(object state)
        {
            // Write sensor data to output / immediate window
            Debug.WriteLine("Temperature..: " + bme.ReadTemperature().ToString("00.0") + "C");
            Debug.WriteLine("Humidity.....: %" + bme.ReadHumidity().ToString("00.0" + "RH"));
            Debug.WriteLine("Pressure.....: " + bme.ReadPressure().ToString(".0") + "Pa");
            Debug.WriteLine("Altitude.....: " + bme.ReadAltitude(slp).ToString(".0") + "m");
            Debug.WriteLine("-----");
        }
    }
}

Result

When you run the sample, it prints the environmental data to the Visual Studio Output / Immediate Window.

Temperature..: 24.5C
Humidity.....: %56.7RH
Pressure.....: 100000.0Pa
Altitude.....: 100.0m
-----

To stop the application, use the "Stop" button on the Visual Studio.

Last updated