Read environmental conditions from a sensor

One of the most common scenarios for IoT devices is detection of environmental conditions. A variety of sensors are available to monitor temperature, humidity, barometric pressure, and more.

In this topic, you will use .NET to read environmental conditions from a sensor.

Prerequisites

  • Raspberry Pi (2 or greater) with Raspberry Pi OS installed
  • BME280 humidity/barometric pressure/temperature sensor breakout
  • Jumper wires
  • Breadboard (optional)
  • Raspberry Pi GPIO breakout board (optional)
  • .NET SDK version 5.0.100 RC2 or later

Important

There are many manufacturers of BME280 breakouts. Most designs are similar, and the manufacturer shouldn't make any difference to the functionality. This tutorial attempts to account for variations. Ensure your BME280 breakout includes an Inter-Integrated Circuit (I2C) interface.

Prepare the Raspberry Pi

Use the raspi-config command to ensure the following two services are enabled:

  • SSH
  • I2C

For more information on raspi-config, refer to the Raspberry Pi documentation .

Prepare the hardware

Use the hardware components to build the circuit as depicted in the following diagram:

A Fritzing diagram showing the connection from Raspberry Pi to BME280 breakout board

The following are the connections from the Raspberry Pi to the BME280 breakout:

  • 3.3V to VIN OR 3V3 (shown in red)
  • Ground to GND (black)
  • SDA (GPIO 2) to SDI OR SDA (blue)
  • SCL (GPIO 3) to SCK OR SCL (orange)

Refer to the following pinout diagram as needed:

A diagram showing the pinout of the Raspberry Pi GPIO header. Image courtesy Raspberry Pi Foundation.
Image courtesy Raspberry Pi Foundation .

Tip

A GPIO breakout board in conjunction with a breadboard is recommended to streamline connections to the GPIO header.

Create the app

Complete the following steps in your preferred development environment:

  1. Create a new .NET Console App using either the .NET CLI or Visual Studio. Name it SensorTutorial.

    dotnet new console -o SensorTutorial
    
  2. Add the Iot.Device.Bindings package to the project. Use either .NET CLI from the project directory or Visual Studio.

    dotnet add package Iot.Device.Bindings --version 1.1.0-*
    
  3. Replace the contents of Program.cs with the following code:

    using System;
    using System.Device.I2c;
    using System.Threading;
    using Iot.Device.Bmxx80;
    using Iot.Device.Bmxx80.PowerMode;
    
    var i2cSettings = new I2cConnectionSettings(1, Bme280.DefaultI2cAddress);
    using I2cDevice i2cDevice = I2cDevice.Create(i2cSettings);
    using var bme280 = new Bme280(i2cDevice);
    
    int measurementTime = bme280.GetMeasurementDuration();
    
    while (true)
    {
        Console.Clear();
    
        bme280.SetPowerMode(Bmx280PowerMode.Forced);
        Thread.Sleep(measurementTime);
    
        bme280.TryReadTemperature(out var tempValue);
        bme280.TryReadPressure(out var preValue);
        bme280.TryReadHumidity(out var humValue);
        bme280.TryReadAltitude(out var altValue);
    
        Console.WriteLine($"Temperature: {tempValue.DegreesCelsius:0.#}\u00B0C");
        Console.WriteLine($"Pressure: {preValue.Hectopascals:#.##} hPa");
        Console.WriteLine($"Relative humidity: {humValue.Percent:#.##}%");
        Console.WriteLine($"Estimated altitude: {altValue.Meters:#} m");
    
        Thread.Sleep(1000);
    }
    

    In the preceding code:

    • i2cSettings is set to a new instance of I2cConnectionSettings. The constructor sets the busId parameter to 1 and the deviceAddress parameter to Bme280.DefaultI2cAddress.

      Important

      Some BME280 breakout manufacturers use the secondary address value. For those devices, use Bme280.SecondaryI2cAddress.

    • A using declaration creates an instance of I2cDevice by calling I2cDevice.Create and passing in i2cSettings. This I2cDevice represents the I2C bus. The using declaration ensures the object is disposed and hardware resources are released properly.

    • Another using declaration creates an instance of Bme280 to represent the sensor. The I2cDevice is passed in the constructor.

    • The time required for the chip to take measurements with the chip's current (default) settings is retrieved by calling GetMeasurementDuration.

    • A while loop runs indefinitely. Each iteration:

      1. Clears the console.

      2. Sets the power mode to Bmx280PowerMode.Forced. This forces the chip to perform one measurement, store the results, and then sleep.

      3. Reads the values for temperature, pressure, humidity, and altitude.

        Note

        Altitude is calculated by the device binding. This overload of TryReadAltitude uses mean sea level pressure to generate an estimate.

      4. Writes the current environmental conditions to the console.

      5. Sleeps 1000 ms.

  4. Build the app. If using the .NET CLI, run dotnet build. To build in Visual Studio, press Ctrl+Shift+B.

  5. Deploy the app to the Raspberry Pi as a self-contained app. For instructions, see Deploy .NET apps to Raspberry Pi. Make sure to give the executable execute permission using chmod +x.

  6. Run the app on the Raspberry Pi by switching to the deployment directory and running the executable.

    ./SensorTutorial
    

    Observe the sensor output in the console.

  7. Terminate the program by pressing Ctrl+C.

Congratulations! You've used I2C to read values from a temperature/humidity/barometric pressure sensor!

Get the source code

The source for this tutorial is available on GitHub .

Next steps