Skip to main content

Read all values

What this example shows​

Reading temperature, humidity, pressure and altitude from the BME280, first one value at a time and then all three measurements from a single call. Both styles are in the same example, so you can pick the one that suits your project.

The example code​

This is BME280_Read_All_Values.ino from the pinned library, with one change: the file's header comment is left out, because it names the BME680 product page rather than this board. The sketch body is exactly as shipped.

Select Board
BME280_Read_All_Values.ino
#include "BME280-SOLDERED.h"

// Declare your sensors here
BME280 bme280;

void setup()
{
// Initialize the sensors
Serial.begin(115200);

// Initialize the sensors
bme280.begin();
}

void loop() // Temperature is not factory calibrated, you can add offset to it
{
float offset = 0; // offset in °C

Serial.println("Reading one by one: ");

float temperature = bme280.readTemperature() + offset;
Serial.print("Temperature: ");
Serial.println(temperature);

float humidity = bme280.readHumidity();
Serial.print("Humidity: ");
Serial.println(humidity);

float pressure = bme280.readPressure();
Serial.print("Pressure: ");
Serial.println(pressure);

float altitude = bme280.readAltitude();
Serial.print("Altitude: ");
Serial.println(altitude);

delay(1000);

Serial.println("\nReading all at once: ");

float temperature2, humidity2, pressure2;
bme280.readSensorData(temperature2, humidity2, pressure2);

Serial.print("Temperature: " + String(temperature2 + offset) + " C");
Serial.println();

Serial.print("Humidity: " + String(humidity2) + " %");
Serial.println();

Serial.print("Pressure: " + String(pressure2) + " hPa");
Serial.println();

// We need to caluclate altitude from pressure and sea level pressure
float altitude2 = bme280.calculateAltitude(pressure2);

Serial.print("Altitude: " + String(altitude2) + " m");
Serial.println();
Serial.println();
}

Expected result:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Reading one by one:
Temperature: 23.30
Humidity: 46.00
Pressure: 1002.40
Altitude: 90.73

Reading all at once:
Temperature: 23.30 C
Humidity: 46.00 %
Pressure: 1002.38 hPa
Altitude: 90.90 m

The first four lines print the bare numbers, the second four add their units, and the block repeats about once a second. The labels are the example's own, so the shape is exact. The numbers are an illustrative sample for Osijek, Croatia: about 23 °C, 46 % relative humidity and 1002.4 hPa. Against the library's 1013.25 hPa reference that pressure works out to about 90 m, close to Osijek's own elevation. Your readings depend on your room and the weather.

Functions used​

BME280()returns None

Creates the sensor object. It takes no address: begin() finds the sensor on the I2C bus by itself.

ReturnsConstructor; no return value.

begin()returns void

Starts I2C, looks for the BME280 on the bus by its chip ID, and sets it up: normal mode, 16x oversampling for temperature, humidity and pressure, the strongest noise filter, and a 1000 ms pause between measurements. If the sensor does not answer it tries again once a second, up to 10 times. Call it once in setup().

ReturnsNothing. It does not report whether the sensor was found.

readTemperature()returns float

Reads the sensor and returns the temperature.

ReturnsTemperature in °C.

readHumidity()returns float

Reads the sensor and returns the relative humidity.

ReturnsRelative humidity in %.

readPressure()returns float

Reads the sensor and returns the barometric pressure.

ReturnsPressure in hPa.

readAltitude()returns float

Reads the pressure and converts it to an altitude, assuming the standard sea-level pressure of 1013.25 hPa.

ReturnsEstimated altitude in metres.

readSensorData(float &temp, float &humidity, float &pressure)returns void

Reads all three measurements from one measurement cycle and writes them into the three variables you pass in, which is faster than three separate read calls, because each of those reads the sensor again.

ReturnsNothing. The results are written into the arguments.

Parameters

TypeNameDescription
float &tempReceives the temperature in °C.
float &humidityReceives the relative humidity in %.
float &pressureReceives the pressure in hPa.
calculateAltitude(float pressure)returns float

Converts a pressure value you already have into an altitude, using the same formula and the same 1013.25 hPa sea-level pressure as readAltitude(), without reading the sensor again.

ReturnsEstimated altitude in metres.

Parameters

TypeNameDescription
floatpressurePressure in hPa, for example the value from readSensorData().

Putting it together​

begin() does all the setup, so after it you can call the read functions in any order. readTemperature(), readHumidity(), readPressure() and readAltitude() each take their own fresh reading from the sensor. When you need several values from the same moment, readSensorData() gets all three from one measurement, and calculateAltitude() turns that pressure into an altitude without reading the sensor again.

The altitude is an estimate. Both altitude functions assume 1013.25 hPa at sea level, so the result drifts as the weather changes the real sea-level pressure.

The library notes that the temperature measurement is not factory calibrated. Compare it against a reference thermometer and set offset at the top of loop() to the difference.