Skip to content
SensorBSDv2.2.4

Adafruit BME280 Library

by Adafruit

This is Adafruit's driver for the BME280, a combined temperature, humidity and barometric pressure sensor. It reads the chip's factory calibration data and gives you simple readTemperature(), readHumidity() and readPressure() calls. It works over I2C or SPI. For I2C you pass the address to begin(), and for SPI you give the constructor a chip-select pin. When you install it from the Library Manager choose "Install all", so Adafruit Unified Sensor and Adafruit BusIO come with it.

Units trip people up: readTemperature() returns degrees Celsius, readHumidity() returns percent, and readPressure() returns pascals, so divide by 100 to get hPa (the unit used in weather reports). readAltitude(seaLevelHPa) converts pressure to an estimated height, but it is only as accurate as the sea-level pressure you give it. The value changes with the weather, so use your local current sea-level pressure instead of the standard 1013.25 hPa if you want a good altitude.

The most common error is Could not find a valid BME280 sensor, check wiring, address, sensor ID!. There are two usual causes. The first is the I2C address: the BME280 answers at 0x76 when its SDO pin is tied to GND and at 0x77 when SDO is tied to VCC. Generic modules are usually 0x76 and Adafruit's own board is 0x77, so try both. The second cause is that you have a BMP280 instead. Many cheap listings sell the BMP280 (no humidity sensor) as a BME280, and the two have different chip IDs: 0x60 is a real BME280, while 0x56 to 0x58 is a BMP280. Print bme.sensorID() when begin() fails. If it shows a BMP280, use the Adafruit BMP280 library instead. It has the same style of API, minus readHumidity(), and there is an example for it below.

The datasheet figures are: temperature −40 to 85 °C with about ±1 °C accuracy, humidity 0–100% with about ±3% RH, and pressure 300–1100 hPa with about ±1 hPa absolute accuracy. The chip itself runs at 3.3 V (1.71–3.6 V). Most modules include a regulator and tolerate 5 V on the VIN pin, but check yours before connecting 5 V.

Self-heating is the other thing to watch. In normal mode the sensor measures continuously and warms itself slightly, and a nearby ESP32 or voltage regulator warms it further, so the temperature (and therefore the humidity) reads high. For weather stations and battery projects, use forced mode as in the example below: the sensor wakes, takes one measurement and goes back to sleep. Mount it away from heat sources.

Setup

Installation

Arduino IDE — Library Manager

Sketch → Include Library → Manage Libraries → search Adafruit BME280 Library

PlatformIO

lib_deps = adafruit/Adafruit BME280 Library

Compatibility

Supported boards

Arduino UnoESP32STM32 Blue Pill

Code

Examples

Read temperature, humidity and pressure

The baseline sketch. If begin() fails it prints the chip ID, which tells you whether the problem is the address or the sensor type.

#include <Wire.h>
#include <Adafruit_BME280.h>

Adafruit_BME280 bme;

void setup() {
  Serial.begin(115200);

  if (!bme.begin(0x76)) {              // try 0x77 if this fails
    Serial.println(F("BME280 not found - check wiring and address"));
    Serial.print(F("SensorID was: 0x"));
    Serial.println(bme.sensorID(), HEX);
    while (true);
  }
}

void loop() {
  Serial.print(F("Temperature: "));
  Serial.print(bme.readTemperature());
  Serial.println(F(" C"));

  Serial.print(F("Humidity: "));
  Serial.print(bme.readHumidity());
  Serial.println(F(" %"));

  Serial.print(F("Pressure: "));
  Serial.print(bme.readPressure() / 100.0F);   // Pa -> hPa
  Serial.println(F(" hPa"));

  Serial.println();
  delay(2000);
}

What the SensorID tells you

When begin() fails, print bme.sensorID() and compare it with this list.

// SensorID printed when begin() fails:
//   0xFF           -> wrong I2C address, or no sensor on the bus (check wiring)
//   0x56 - 0x58    -> this is a BMP280 (no humidity) - use the Adafruit BMP280 library
//   0x60           -> BME280 (correct)
//   0x61           -> BME680 - use the Adafruit BME680 library

Estimate altitude from pressure

Accurate only if you supply the current local sea-level pressure. Look it up from a weather report for your area.

#define SEALEVELPRESSURE_HPA (1013.25)   // replace with today's local sea-level pressure

float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

Serial.print(F("Altitude: "));
Serial.print(altitude);
Serial.println(F(" m"));

Forced mode for weather stations and battery projects

The sensor sleeps between readings. This avoids self-heating and cuts power use, which also gives more accurate temperature and humidity.

void setup() {
  Serial.begin(115200);
  bme.begin(0x76);

  bme.setSampling(Adafruit_BME280::MODE_FORCED,
                  Adafruit_BME280::SAMPLING_X1,   // temperature
                  Adafruit_BME280::SAMPLING_X1,   // pressure
                  Adafruit_BME280::SAMPLING_X1,   // humidity
                  Adafruit_BME280::FILTER_OFF);
}

void loop() {
  bme.takeForcedMeasurement();            // wake, measure once, sleep

  float t = bme.readTemperature();
  float h = bme.readHumidity();
  float p = bme.readPressure() / 100.0F;

  Serial.print(t); Serial.print(" C  ");
  Serial.print(h); Serial.print(" %  ");
  Serial.print(p); Serial.println(" hPa");

  delay(60000);                           // one reading per minute
}

If you actually have a BMP280 (no humidity)

Same idea with the Adafruit BMP280 library. Install 'Adafruit BMP280 Library' from the Library Manager. There is no humidity reading.

#include <Wire.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(115200);

  if (!bmp.begin(0x76)) {              // try 0x77 if this fails
    Serial.println(F("BMP280 not found - check wiring and address"));
    while (true);
  }
}

void loop() {
  Serial.print(bmp.readTemperature());
  Serial.print(F(" C   "));
  Serial.print(bmp.readPressure() / 100.0F);
  Serial.println(F(" hPa"));
  delay(2000);
}