This project is an example on how to use the Adafruit BMP280 Barometric Pressure + Temperature Sensor in Raspberry PI and Windows 10 IoT Core.
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
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.
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.
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.
- Create an object for sensor:
BMP280 tempAndPressure = new BMP280();
- 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();
}
}
}
}
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.