Table of Contents

STM_1B: Reading environmental data with a Bosch integrated sensor

We will read environmental data using a BME 280 sensor in this scenario. It is one of the most popular sensors in weather stations. It integrates a single chip's digital thermometer, hygrometer (air humidity), and air pressure meter. In our laboratory equipment, this sensor is located inside the yellow pressure chamber, under the fan which can blow the air onto it.
The sensor communicates with the microcontroller using the I2C bus. In all our laboratory nodes, the I2C bus uses two GPIOs: a D14 (PB_9 in Nucleo numbering) pin for the SDA line, and a D15 (PB_8 in Nucleo numbering) pin for SCL. Every integrated circuit connected to the I2C bus has its own address. BME 280 is visible under the address 0x76. For the details please refer to Table 1: STM32WB55 Node Hardware Details in SUT STM32 Laboratory Node Hardware Reference
This scenario can be run stand-alone to read weather data in the laboratory nodes' room. Still, it is also complementary to the scenario STM_1A: Use of fan and may enable you to monitor the results of the fan operation that should induct changes in the air pressure.

Prerequisites

The static temperature, humidity and air pressure values can be read using a dedicated library:

  lib_deps = adafruit/Adafruit BME280 Library@^2.2.2

To observe air pressure changes over a short time, it is necessary to implement fan PWM control as described in the STM_1A: Use of fan.
Sensor readings can be sent over the network or presented on one of the node's displays (e.g. LCD), so understanding how to handle at least one of the displays is essential:

To implement monitoring of the air pressure changes, understanding how to control a fan with PWM is necessary:

Technical documentation for the BME 280 sensor is available here:

Besides BME 280, there is another sensor in the Bosch family: BMP 280. The former one does not measure humidity, just temperature and air pressure.

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

In this scenario, we only focus on reading the sensor. Information on how to display measurements is part of other scenarios that you should refer to to create a fully functional solution (see links above).

Task to be implemented

Present the current temperature, air pressure, and humidity on any display (e.g. LCD). Remember to add units (C, %Rh, hPa).

Start

For statical measurements, ensure the fan is stopped. Note that the fan tends to spin up on itself (explained in STM1A) when the GPIO controlling the fan is not configured, so it is better to ensure it is set to output and low (0) to keep the fan stopped. Refer to the STM_1A: Use of fan for details on controlling the fan.

Steps

The steps below present only interaction with the sensor. Those steps should be supplied to present the data (or send it over the network) using other scenarios accordingly, and also with a scenario STM_1A presenting instructions on controlling the fan that can change the air pressure in the yellow pressure chamber.

Step 1

Include a BME 280 control library:

#include <Adafruit_BME280.h>

Step 2

Declare BME's address, sensor controller class and variables to store readings:

static const int BME280_addr = 0x76; //I2C address
 
static bool isBMEOk = false;
 
static float temperature;
static float pressure;
static float humidity;
 
static Adafruit_BME280 bme280; //controller class

Step 3

Initialise controller class:

isBMEOk = bme280.begin(BME280_addr);

If begin returns false, then initialisation failed. This may be due to an invalid I2C address provided as a parameter of the begin function, a broken sensor, or broken connections between the MCU and the sensor.

Step 4

Read environmental data (one at a time):

  temperature = bme280.readTemperature();
  pressure = bme280.readPressure() / 100.0F;
  humidity = bme280.readHumidity();

The temperature is Celsius, air pressure is in Pascals (so we divide it by float 100 to obtain the hPa reading), and relative air humidity is in % (frequently referenced as %Rh).

Note that the controller class has an exciting function of trading the altitude based on the sea-level pressure. We need to provide as the argument the current value of the sea-level pressure to have a correct altitude reading:

 float altitude = bme280.readAltitude(1013.00F);

Note that due to the non-linear characteristics of the air pressure drop with increasing altitude, it does not work correctly at high altitudes.

The library also has a mathematical calculation function that returns current sea level pressure if only altitude and local air pressure are known. It does not read the sensor itself, however:

  float seaLevelPressure = bme280.seaLevelForAltitude(230, bme280.readPressure());

In the example above first parameter it is the altitude (230m).

Result validation

The observable temperature is usually within the range of 19-24C, with humidity about 40-70%, strongly depending on the weather. On rainy days, it can even go higher. Air pressure depends on the current weather (assuming the fan is off) and is usually low to 890hPa (when low-pressure area) and even up to 1040hPa (when high-pressure area comes, usually during the summer). Spinning the fan may easily change air pressure by at least 1-2Pa up relative to the static pressure, depending on the fan's rotation speed.

FAQ

I've got NaN (Not a Number) readings. What to do?: Check if the I2C address corresponds to the BME280 sensor (should be 0x76), check if you initialised controller class and most of all, give the sensor some recovery time (at least 250ms) between consecutive readings.

Project information


This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.