BME280 (finitespace): pressure, temperature and humidity in one read
The BME280 is Bosch鈥檚 combined pressure, temperature and humidity sensor, and the BME280 library by Tyler Glenn (published under finitespace) is a compact driver for it. Its class for the I2C module is BME280I2C, and one read() call fills three floats at once in the units you choose: Celsius or Fahrenheit, and pascals, hectopascals, inches of mercury and more for pressure.
Built in on Mokxi: #include <BME280I2C.h> and it builds, with nothing to install. Free, and no account needed.
What BME280 does
The library ships a second header, EnvironmentCalculations.h, with the formulas weather sketches always need: altitude from pressure, dew point, sea-level pressure, absolute humidity and heat index. They are plain functions, so they work on readings from any sensor.
Two things to know. Only the I2C class is here, not the SPI one. And the SparkFun BME280 library also names its class BME280, so the two libraries cannot be included in one sketch, exactly as on a real board.
| Library Manager name | BME280 |
|---|---|
| Author | Tyler Glenn |
| Include | #include <BME280I2C.h>#include <EnvironmentCalculations.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Pressure, humidity and temperature, BME280 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <BME280I2C.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "BME280" by Tyler Glenn from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add BME280 to lib_deps in PlatformIO. Mokxi follows the calls of release 3.0.0, so a sketch written here builds there too.
What to know in the simulator: SPI is not here. The SparkFun BME280 library also names its class BME280, so the two cannot share a sketch, as upstream.
ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
BME280I2C bme
Creates the sensor with default settings at address 0x76.
bme.begin()
Starts the sensor. Returns false when no BME280 or BMP280 answers. Call Wire.begin() first.
bme.chipModel()
Tells a BME280 from a BMP280, which has no humidity sensor. Compare with BME280::ChipModel_BME280.
bme.read(pres, temp, hum, tempUnit, presUnit)
Reads all three values into float variables.
float temp, hum, pres;
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_hPa);
bme.read(pres, temp, hum, tempUnit, presUnit);EnvironmentCalculations::Altitude(pres, EnvironmentCalculations::AltitudeUnit_Meters)
Height above sea level from pressure, against the standard 1013.25 hPa unless you pass your local sea-level pressure.
EnvironmentCalculations::DewPoint(temp, hum, EnvironmentCalculations::TempUnit_Celsius)
The dew point from a temperature and a humidity.
bme.setSettings(settings)
Applies a BME280I2C::Settings object: oversampling for each reading, the mode, the standby time, the filter and the I2C address (0x77 for some boards).
Example: Temperature, humidity and pressure with BME280I2C
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Wire.h>
#include <BME280I2C.h>
#include <EnvironmentCalculations.h>
BME280I2C bme; // forced mode, 0x76
void setup() {
Serial.begin(9600);
Wire.begin();
while (!bme.begin()) {
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
switch (bme.chipModel()) {
case BME280::ChipModel_BME280: Serial.println("Found BME280 sensor! Success."); break;
case BME280::ChipModel_BMP280: Serial.println("Found BMP280 sensor! No Humidity available."); break;
default: Serial.println("Found UNKNOWN sensor! Error!");
}
}
void loop() {
float temp(NAN), hum(NAN), pres(NAN);
bme.read(pres, temp, hum, BME280::TempUnit_Celsius, BME280::PresUnit_hPa);
Serial.print("Temp: ");
Serial.print(temp);
Serial.print(" C\tHumidity: ");
Serial.print(hum);
Serial.print("% RH\tPressure: ");
Serial.print(pres);
Serial.println(" hPa");
Serial.print("Altitude: ");
Serial.print(EnvironmentCalculations::Altitude(pres, EnvironmentCalculations::AltitudeUnit_Meters, 1013.25, temp));
Serial.print(" m\tDew point: ");
Serial.print(EnvironmentCalculations::DewPoint(temp, hum));
Serial.println(" C");
delay(1000);
}Wiring
- Arduino Uno R3 A4 to Pressure, humidity and temperature, BME280 SDA
- Arduino Uno R3 A5 to Pressure, humidity and temperature, BME280 SCL
- Arduino Uno R3 5V to Pressure, humidity and temperature, BME280 VIN
- Arduino Uno R3 GND to Pressure, humidity and temperature, BME280 GND
Common errors and how to fix them
Could not find BME280 sensor!
begin() returned false. Many boards sit at 0x77 instead of 0x76: set the address in a BME280I2C::Settings object and pass it to the constructor. Check the I2C wires and that Wire.begin() ran.
Humidity reads NaN or 0 all the time
The chip is probably a BMP280, which looks the same and has no humidity sensor. chipModel() tells you which you have.
conflicting declaration 'class BME280'
Two BME280 libraries are in one sketch, this one and SparkFun鈥檚, and both define a class called BME280. Keep one library and remove the other鈥檚 include.
Keep going
Try BME280 without installing anything
Open the example, change the sketch, and keep your version in a free account.