Try Embedded

Barometer and altimeter (BMP280)

Read air pressure and temperature from a BMP280 over I²C and estimate the altitude from them.

Sensors · Beginner

In a real product: Drone flight controller (Aerospace and drones): The barometer holds the drone's altitude when GPS is too coarse.

Air pressure falls as you climb: about 12 hPa for every 100 m near the ground. A BMP280 measures it precisely enough for weather stations, drones and phones to estimate their height.

What you'll learn

  • Talking to an I²C sensor at its address (0x76 here)
  • Why raw sensor readings need the chip's own calibration values
  • Turning pressure into altitude

How it works

The BMP280 shares the I²C bus: SDA (data) on A4 and SCL (clock) on A5 on an Uno. bmp.begin(0x76) checks that a chip answers at that address with the BMP280's ID (0x58) and reads its calibration values.

The sensor gives raw 20-bit numbers. Every chip is trimmed at the factory, and the datasheet gives formulas that turn the raw values into degrees and pascals using the trimming values stored in the chip's registers. The library applies them: readTemperature() returns °C and readPressure() pascals (divide by 100 for hPa).

readAltitude(1013.25) compares the pressure with the pressure at sea level. Weather changes the sea-level pressure, so a real altimeter is set to the local value first.

Try this

  • Click the sensor while it runs and lower the pressure to 900 hPa: the altitude reads about 990 m.
  • Print the altitude change since the start, to measure how high something has been lifted.
  • Set its address to 0x77 in its settings: begin(0x76) fails, as with a wrongly wired SDO pin.

sketch.ino

// Temperature, pressure and estimated altitude from a BMP280 on I2C, every second.
#include <Wire.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(9600);
  if (!bmp.begin(0x76)) {
    Serial.println("No BMP280 at 0x76: check the wiring and the address");
    while (true) {}
  }
}

void loop() {
  Serial.print("Temperature: ");
  Serial.print(bmp.readTemperature(), 1);
  Serial.print(" C   Pressure: ");
  Serial.print(bmp.readPressure() / 100.0, 1);
  Serial.print(" hPa   Altitude: ");
  Serial.print(bmp.readAltitude(1013.25), 0);
  Serial.println(" m");
  delay(1000);
}

Lessons behind it