Skip to main content

Read all values

What this example shows​

Temperature, pressure, humidity and the altitude worked out from the pressure, printed on four lines every 5 seconds. The example's own header names this product, so there is no doubt which board it is for.

The example code​

This is Examples/bme280-allValues.py from the pinned module, unchanged. Save it to your board and run it.

# FILE: bme280-allValues.py
# AUTHOR: Josip Šimun Kuči @ Soldered
# BRIEF: An example for the BME280 sensor that reads temperature, humidity,
# pressure and calculates the altitude using the pressure measurement
# WORKS WITH: Enviromental sensor BME280 breakout: www.solde.red/333036
# LAST UPDATED: 2025-05-23
from machine import Pin, I2C
from bme280 import BME280
import time

# If you aren't using the Qwiic connector, manually enter your I2C pins
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# bme280 = BME280(i2c)

# Initialize sensor over Qwiic
bme280 = BME280()

# Infinite loop
while 1:
# Read the temperature, humidity and pressure values and store them in their respective variables
temp, pres, hum = bme280.readAllValues()
# Calculate the altitude using the pressure read by the sensor
altitude = bme280.calculateAltitude()

# Print the measured values, each in their own line
print("Temperature: {:.2f} °C".format(temp))
print("Pressure: {:.2f} hPa".format(pres))
print("Humidity: {:.2f} %".format(hum))
print("Altitude: {:.2f} m".format(altitude))

# Pause for 5 seconds
time.sleep(5.0)

Expected result:

ShellMicroPython
Temperature: 23.30 °C
Pressure: 1002.40 hPa
Humidity: 46.00 %
Altitude: 90.73 m

Each line is one of the example's own print() calls with the value to two decimals, 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. The block repeats every 5 seconds.

Functions used​

BME280(i2c=None, address=0x76)returns BME280

Creates the sensor object. It reads the calibration values out of the sensor and configures the measurement, so it is the only setup step you need. Called with no arguments it uses the module's own I2C pins, which only exist on an ESP32, an ESP8266 or a Soldered Dasduino CONNECTPLUS; on any other board it raises an exception and asks you for the pins, so pass in your own I2C object.

ReturnsA ready sensor object.

Parameters

TypeNameDescription
I2Ci2cAn initialised I2C object. Optional; when you leave it out the module creates its own I2C(0) with SCL on GPIO22 and SDA on GPIO21.
intaddressThe sensor's I2C address. Optional, and there is no reason to change it: this board is fixed at 0x76.
readAllValues()returns tuple

Takes one measurement and returns all three values from it, already compensated with the calibration data and in their own units. The three values come from the same reading, so they describe the same moment.

ReturnsA tuple of three floats: temperature in °C, pressure in hPa and relative humidity in %.

calculateAltitude()returns float

Takes a fresh pressure reading and converts it to an altitude, assuming the standard sea-level pressure of 1013.25 hPa. It does not use the values you already read, so it measures the sensor again.

ReturnsEstimated altitude in metres.

Putting it together​

BME280() does the whole setup, so the example goes straight into the loop: readAllValues() for the three measurements, then calculateAltitude() for the height. Everything is printed with two decimals, and the loop pauses for 5 seconds so the readings do not scroll past.

The altitude is an estimate. calculateAltitude() assumes 1013.25 hPa at sea level, so it drifts as the weather changes the real sea-level pressure, and it is accurate to roughly tens of metres rather than metres.

If you want the raw route instead, the module exposes it: read_raw_data() returns the three uncompensated readings, and readTemperature(adc_T), readPressure(adc_P) and readHumidity(adc_H) convert them one at a time. The two converters expect the matching raw value from read_raw_data(), and readTemperature() has to run first, because the pressure and humidity conversions use the temperature it works out.