BMP280: measure air pressure, temperature and altitude

The Bosch BMP280 is a small digital barometer. It measures the absolute air pressure (300–1100 hPa) and the temperature. It sends the data through I²C or SPI. The air pressure decreases by about 12 Pa (0.12 hPa) for each meter of height near sea level. The relative accuracy is about ±0.12 hPa, so the sensor can find a height change of about one meter. You can measure the weather, the floor of a building, or the height of a drone or a rocket.

The BME280 looks almost the same and also measures humidity. Many listings for a “BME280” module send a BMP280. To find the chip type, read the chip ID: 0x58 is a BMP280 and 0x60 is a BME280. For humidity only, refer also to the DHT22 page.

Close-up of a blue BMP280 breakout board with the small metal sensor package that has a vent hole, three capacitors and a regulator
BMP280 breakout. The sensor is the 2 × 2.5 mm metal package with a small vent hole.
Photo: AGermanBeck, CC BY-SA 4.0

A sensor in an elevator

Drag the elevator car or use the sliders. Then change the weather. The pink line uses the standard 1013.25 hPa in readAltitude(). The blue line uses the correct sea-level pressure for today. The dashed line is the true altitude.

I²C address and chip ID

Set the SDO pin, the chip on your module, and the library and address in your code. The panel shows the I²C scanner output and the result of begin().

SDO pin on the module
Chip on the module
Library in your sketch
Address in begin()

How it works

A piezo-resistive membrane

A thin silicon membrane closes a small vacuum chamber. The air pressure bends the membrane, and resistors on the membrane change their value. A 20-bit converter measures the change. The chip also measures its own temperature, because the library uses the temperature to correct the pressure. Each chip has factory calibration values in its memory.

Altitude from pressure

The library uses the barometric formula of the standard atmosphere:

h = 44330 × (1 − (p / p0)1/5.255)

Here p is the measured pressure and p0 is the pressure at sea level. The result is in meters above sea level.

The sea-level reference p0

The weather changes p0 by up to ±30 hPa. Each 1 hPa of error gives about 8 m of altitude error. If you use the default 1013.25 hPa, the altitude can be wrong by more than 100 m. Get the local p0 (“sea-level pressure” or QNH) from a weather service or an airport report. For a height change only, measure the pressure at the start and use that value as p0.

Oversampling and the IIR filter

Oversampling takes 1 to 16 samples and averages them. More samples give less noise, but each measurement takes longer and uses more current. The IIR filter smooths fast changes, for example a door that closes or wind. A high filter coefficient gives less noise, but the reading then follows a real change more slowly. Look at this delay in the simulator when the elevator moves.

Self-heating and placement

The sensor measures the temperature of its own chip. The ESP32, a voltage regulator and a power LED heat the board, so the temperature can read 1–3 °C high. Keep the sensor away from these parts. Use forced mode with a low measurement rate for weather stations. The pressure value does not change much with this heat.

Specifications (BMP280)

Pressure
300–1100 hPa
Absolute accuracy
±1 hPa
Relative accuracy
±0.12 hPa (≈ ±1 m)
Temperature
±1 °C (0–65 °C)
Supply
1.71–3.6 V
Current
≈ 2.7 µA at 1 Hz
Interface
I²C or SPI

Wiring

ESP32 3V3 GND GPIO 22 (SCL) GPIO 21 (SDA) BMP280 VCC GND SCL SDA CSB SDO CSB high = I²C SDO low = 0x76 3.3 V only
Most 6-pin modules connect CSB and SDO with resistors, so you only need four wires. The dashed wires show the fixed levels. Connect SDO to 3V3 for address 0x77. Do not connect CSB to GND, because the chip then changes to SPI mode until the next power-up.

Connections

Module pinESP32Notes
VCC / VIN3V3Many purple 6-pin modules have no regulator. Do not connect them to 5 V.
GNDGNDCommon ground.
SCL / SCKGPIO 22I²C clock.
SDA / SDIGPIO 21I²C data.
CSBopen or 3V3HIGH selects I²C. SPI: chip select.
SDOGND or 3V3LOW: 0x76. HIGH: 0x77. SPI: data out (MISO).

The Adafruit board pulls SDO HIGH (0x77, the library default). Many low-cost modules pull SDO LOW (0x76). To find the address, run the I²C scanner from the OLED display page.

Build steps

  1. Install the libraryIn the Library Manager, install “Adafruit BMP280 Library”. Accept the dependencies (Adafruit Unified Sensor, Adafruit BusIO).
  2. Connect four wiresConnect VCC to 3V3, GND to GND, SCL to GPIO 22 and SDA to GPIO 21.
  3. Run the I²C scannerWrite down the address: 0x76 or 0x77.
  4. Read the chip IDUpload the chip-ID sketch below. If the ID is 0x60, use the Adafruit BME280 library.
  5. Set p0Find today’s sea-level pressure for your location. Put it in readAltitude().

Troubleshooting

  • begin() returns false: the address is wrong, or the chip ID does not agree with the library. Run the scanner and the chip-ID sketch. Use bmp.begin(0x76) for most low-cost modules.
  • The chip ID is 0x60: the module has a BME280. Use the Adafruit BME280 library. The BMP280 library does not accept this chip.
  • The altitude is wrong by tens of meters: p0 is not correct. Update p0 from a weather report, or set p0 to the pressure at a known height.
  • The altitude changes slowly during the day: the weather changes the pressure. Measure a reference sensor at a fixed height and subtract its change.
  • The temperature is too high: the board is near the ESP32 or a regulator. Put the sensor on a short cable away from heat.

Code

1 · Temperature, pressure and altitude

Library: Adafruit BMP280 Library. readPressure() gives pascals. Divide by 100 to get hPa.

bmp280_basic.ino


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

              Adafruit_BMP280 bmp;                      // I2C on Wire
              const float SEA_LEVEL_HPA = 1013.25;      // replace with today's local value

              void setup() {
                Serial.begin(115200);
                Wire.begin();                           // SDA = GPIO 21, SCL = GPIO 22
                if (!bmp.begin(0x76)) {                 // 0x77 on Adafruit boards
                  Serial.printf("No BMP280. Sensor ID: 0x%02X\n", bmp.sensorID());
                  while (true) delay(1000);
                }
                // weather station settings: low noise, one sample each 500 ms
                bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                                Adafruit_BMP280::SAMPLING_X2,    // temperature
                                Adafruit_BMP280::SAMPLING_X16,   // pressure
                                Adafruit_BMP280::FILTER_X16,
                                Adafruit_BMP280::STANDBY_MS_500);
              }

              void loop() {
                float t = bmp.readTemperature();         // °C
                float p = bmp.readPressure() / 100.0;    // hPa
                float h = bmp.readAltitude(SEA_LEVEL_HPA); // m above sea level
                Serial.printf("%.2f C  %.2f hPa  %.1f m\n", t, p, h);
                delay(1000);
              }
            

2 · Read the chip ID

This sketch uses only Wire. Register 0xD0 contains the chip ID.

bmx280_chip_id.ino


              #include <Wire.h>

              void setup() {
                Serial.begin(115200);
                Wire.begin();
                for (uint8_t addr : {0x76, 0x77}) {
                  Wire.beginTransmission(addr);
                  Wire.write(0xD0);                     // chip ID register
                  if (Wire.endTransmission(false) != 0) continue;
                  Wire.requestFrom((int)addr, 1);
                  uint8_t id = Wire.read();
                  Serial.printf("0x%02X: chip ID 0x%02X = %s\n", addr, id,
                                id == 0x58 ? "BMP280" : id == 0x60 ? "BME280" : "other");
                }
              }

              void loop() {}
            

3 · Relative height (no weather report)

Measure the pressure at the start and use it as p0. The result is the height above the start point.

bmp280_relative.ino


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

              Adafruit_BMP280 bmp;
              float startHpa;

              void setup() {
                Serial.begin(115200);
                Wire.begin();
                bmp.begin(0x76);
                delay(500);
                startHpa = bmp.readPressure() / 100.0;  // p0 = here, now
              }

              void loop() {
                Serial.printf("Height: %.2f m\n", bmp.readAltitude(startHpa));
                delay(250);
              }
            

4 · BME280: also humidity

Install Adafruit BME280 Library. The functions are almost the same. readAltitude() needs the p0 argument. You can also give the measured pressure to an SCD41 CO₂ sensor for pressure compensation.

bme280_basic.ino


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

            Adafruit_BME280 bme;

            void setup() {
              Serial.begin(115200);
              Wire.begin();
              if (!bme.begin(0x76)) {
                Serial.println("No BME280 (chip ID 0x60) found");
                while (true) delay(1000);
              }
            }

            void loop() {
              Serial.printf("%.2f C  %.1f %%RH  %.2f hPa  %.1f m\n",
                            bme.readTemperature(), bme.readHumidity(),
                            bme.readPressure() / 100.0, bme.readAltitude(1013.25));
              delay(2000);
            }
          

Resources