LeivoSepp / Lesson-Temperature-BMP280

Raspberry PI and Windows 10 IoT Core. Using Adafruit temperature and pressure sensors BMP280.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Barometric Pressure + Temperature Sensor Adafruit BMP280

This project is an example on how to use the Adafruit BMP280 Barometric Pressure + Temperature Sensor in Raspberry PI and Windows 10 IoT Core.

What is this sensor?

This sensor is great for all sorts of weather/environmental sensing. This sensor is used for measuring barometric pressure with ±1 hPa absolute accuraccy, and temperature with ±1.0°C accuracy. Because pressure changes with altitude, and the pressure measurements are so good, you can also use it as an altimeter with  ±1 meter accuracy.

https://www.adafruit.com/products/2651

image

Temperature is calculated in degrees C, you can convert this to F by using the classic F = C * 9/5 + 32 equation.

Pressure is returned in the SI units of Pascals. 100 Pascals = 1 hPa = 1 millibar. You can also calculate Altitude. However, you can only really do a good accurate job of calculating altitude if you know the hPa pressure at sea level for your location and day! The sensor is quite precise but if you do not have the data updated for the current day then it can be difficult to get more accurate than 10 meters.

How to connect this sensor into Raspberry PI?

To connect this sensor to Raspberry PI you need 4 wires. Two of the wires used for voltage Vin (+3V from Raspberry) and ground GND and remaining two are used for data. As this is digital sensor, it uses I2C protocol to communicate with the Raspberry. For I2C we need two wires, Data (SDA) and Clock (SCL). Connect sensor SDI and SCK pins accordingly to Raspberry SDA and SCL pins.

image

How do I write the code?

You need to add NuGet packages Glovebox.IoT.Devices and UnitsNet to your project and you are almost done :)

After adding these NuGet packages, you just need to write 2 lines of code.

  1. Create an object for sensor:
        BMP280 tempAndPressure = new BMP280();
  1. Write a while-loop, to read data from the sensor every 1 sec.
            while (true)
            {
                double temperature = tempAndPressure.Temperature.DegreesCelsius;
                double pressure = tempAndPressure.Pressure.Hectopascals;
                Task.Delay(1000).Wait();
            }

Final code looks like this.

If you run it, you do not see anything, because it just reads the data, but it doesnt show it anywhere. You need to integrate this project with my other example, where I teach how to send this data into Azure.

using System;
using Windows.ApplicationModel.Background;
using Glovebox.IoT.Devices.Sensors;
using System.Threading.Tasks;

namespace LessonTemperatureBMP280
{
    public sealed class StartupTask : IBackgroundTask
    {
        BMP280 tempAndPressure = new BMP280();
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            while (true)
            {
                double temperature = tempAndPressure.Temperature.DegreesCelsius;
                double pressure = tempAndPressure.Pressure.Hectopascals;
                Task.Delay(1000).Wait();
            }
        }
    }
}

Can I change I2C address?

I2C address is used to communicate with the sensor. Many sensors have I2C address hardcoded, and this sensor exactly this. You cannot change I2C address, it is fixed to 0x77.

About

Raspberry PI and Windows 10 IoT Core. Using Adafruit temperature and pressure sensors BMP280.


Languages

Language:C# 100.0%