# DIY - Air Pressure Sensor

## Aim of this lesson

This lesson shows how to read measurements from the BMP280 air pressure sensor.

## Material

• BMP280 air pressure & -temperature sensor

## Basics

The BMP280 is capable of measuring air pressure in hectopascal (hPa) as well as air temperature (°C). This sensor communicates via the I²C protocol, and requires an operating voltage of 3.3 - 5.0 volts.

The I²C address of the BMP280 can be changed through it's SDO pin: If SDO is connected to GND, the address will be 0x76, otherwise 0x77.

Air pressure varies with the height above sea level: The higher you are, the lower the air pressure will get. This means, that we can deduce our height above sea level from a pressure measurement!

To do so, we need a reference value of the current pressure at sea level, usually called P0. Because this value constantly changes through the weather, and depends roughly on the current location, we have to recalibrate this value regularly for accurate measurements.

## Construction

The operating voltage is provided to the sensor through the wires 3.3V -> VCC and GND -> GND. The I²C bus is set up as usual via the SDA and SCL pins.

Additionally this sensor requires a wire from GND to SDO on the BMP280. This changes the sensors address from 0x77 to 0x76, which is the configured value in the BMP280.h library.

## Program

The sensor can be controlled with the BMP280.h library. Once this file is included in the sketch, we can create an instance bmp of it. Now we are able to call the libraries functions on this object:

#include <BMP280.h>
#include <Wire.h>
BMP280 bmp;


In the setup() function we initialize the sensor with the following lines:

if (!bmp.begin()) {
Serial.println("BMP init failed!");
}
bmp.setOversampling(4); // select resolution of the measurements


Now we need to can issue a measurement in the loop() function. The variables temp and pressure will then contain the current measurement values:

double temp, pressure;
char bmpStatus = bmp.startMeasurment();

// if an error occured on the sensor: stop
if (bmpStatus == 0) {
Serial.println(bmp.getError());
return;
}

delay(bmpStatus); // wait for duration of the measurement
bmpStatus = bmp.getTemperatureAndPressure(temp, pressure);


## Exercises

Connect the BMP280 sensor with the arduino, and create a sketch that prints the current air pressure and temperature on the serial monitor.

Consider the code above which reads from the BMP280. What is the variable bmpStatus used for?

You learned that you can derive the height of your senseBox from the current air pressure. Use the function bmp.altitude(...) to calculate the height, and print it on the serial monitor as well.

Hint: *Have a look at the example file in the BMP280 library. For correct values, the reference value P0 needs to be adapted to the current weather: You can find your local value here.