Arduino library · Sensors

Adafruit BME280 library: temperature, humidity and pressure on I2C

Yes. You can use the Adafruit BME280 Library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Pressure, humidity and temperature, BME280). The example below, "Temperature, pressure, altitude and humidity from a BME280", runs on the Arduino Uno R3.

Built in on Mokxi: #include <Adafruit_BME280.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit BME280 Library does

The BME280 is a small Bosch sensor that measures temperature, relative humidity and air pressure, and the Adafruit BME280 library is the usual way to read it from an Arduino or ESP32. It talks over I2C with just four wires (power, ground, SDA and SCL), so on an Uno the data lines are A4 and A5.

The address is the first thing to get right. begin() with no argument looks at 0x77, which is where Adafruit’s own board puts the sensor. Most of the purple modules sold for Arduino are strapped to 0x76, and so is the sensor in Mokxi by default, so tutorials written for them say bme.begin(0x76). When begin() returns false, the address is the first thing to check.

A reading is one call: readTemperature() in degrees Celsius, readHumidity() in percent and readPressure() in pascals, so the sketches divide by 100.0F to print hectopascals. readAltitude() works the height out from the pressure and needs the sea-level pressure for your day, 1013.25 hPa being the standard value.

Library Manager nameAdafruit BME280 Library
AuthorAdafruit
Include#include <Adafruit_BME280.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesPressure, humidity and temperature, BME280

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_BME280.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit BME280 Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit BME280 Library to lib_deps in PlatformIO. Mokxi follows the calls of release 2.3.0, so a sketch written here builds there too.

What to know in the simulator: The SPI constructors build, and begin() says on Serial that the SPI module is not simulated: Mokxi’s BME280 is the I2C module.

The calls you will use

In the order a sketch meets them.

Adafruit_BME280 bme

Makes the sensor object for the I2C version. Everything else is a call on it.

bme.begin(0x76)

Starts the sensor at the given I2C address and returns false if nothing answers there. With no argument it uses 0x77.

if (!bme.begin(0x76)) {
  Serial.println("No BME280 found, check wiring and address");
  while (1) delay(10);
}

bme.readTemperature()

Temperature in degrees Celsius, to the hundredth.

bme.readHumidity()

Relative humidity in percent.

bme.readPressure()

Air pressure in pascals. Divide by 100.0F for hectopascals.

bme.readAltitude(SEALEVELPRESSURE_HPA)

Altitude in meters, worked out from the pressure and the sea-level pressure you give it in hectopascals.

bme.sensorID()

The chip ID the sensor reports, which is how you tell a BME280 from a look-alike when begin() fails.

bme.setSampling(Adafruit_BME280::MODE_NORMAL)

Chooses the measuring mode, the oversampling of each reading and the filter. The defaults suit most sketches.

Example: Temperature, pressure, altitude and humidity from a BME280

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

void setup() {
  Serial.begin(9600);
  Serial.println(F("BME280 test"));
  // Most purple BME280 modules are at 0x76; Adafruit's own board is at 0x77.
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    Serial.print("SensorID was: 0x");
    Serial.println(bme.sensorID(), 16);
    while (1) delay(10);
  }
  Serial.println("-- Default Test --");
  Serial.println();
}

void loop() {
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" °C");
  Serial.print("Pressure = ");
  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");
  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");
  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");
  Serial.println();
  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

Open it in the editor, wired and ready to run

Common errors and how to fix them

Could not find a valid BME280 sensor, check wiring, address, sensor ID!

This is the example sketch’s own message when begin() returns false. Try bme.begin(0x76) if you used the default and bme.begin(0x77) if you used 0x76, check that SDA goes to A4 and SCL to A5 on an Uno, and that the module has power and ground.

SensorID was: 0x58

The chip is a BMP280, not a BME280. It measures temperature and pressure only, so it has no humidity. Use the Adafruit BMP280 library for it, or buy a module that really says BME280 on the chip.

The pressure reads about 101325 instead of 1013

readPressure() returns pascals, not hectopascals. Divide it by 100.0F before printing it as hPa, as the example does.

Altitude is hundreds of meters wrong

readAltitude() compares the pressure with the sea-level pressure you pass. The standard 1013.25 is only right on an average day, so use today’s local sea-level pressure from a weather report for a better number.

Try Adafruit BME280 Library without installing anything

Open the example, change the sketch, and keep your version in a free account.