Language: EN

medir-presion-del-aire-y-altitud-con-arduino-y-barometro-bmp180

How to Measure Air Pressure and Altitude with Arduino and BMP180 Barometer

What is a BMP180?

The BMP180 is a digital thermometer and barometer. Functioning as a barometer, it is a device that measures air pressure and can be used as an altimeter. This sensor can be connected to a controller or processor like Arduino to record air pressure measurements or estimate the altitude of the sensor relative to sea level.

Barometric pressure is caused by the weight of the column of air in the atmosphere. Barometric pressure depends on various factors, especially temperature, as it influences the density of the air and, therefore, the weight of the column of air. Other factors affecting the measurement are humidity and wind.

Since barometric pressure depends on the height of the column of air above the sensor, it can also be used to estimate the altitude at which the sensor is located relative to sea level.

It is important to note that barometric pressure varies continuously due to weather conditions, so it does not provide an absolute measurement of altitude with precision. However, it can be useful in differential altitude measurements, i.e., differences in elevation in a vertical displacement.

The BMP180 is a digital thermometer and barometer that uses its temperature sensor to compensate for its effects on barometric pressure measurement. Furthermore, the BMP180 is an improved version of the BMP085, and both models are compatible with each other in terms of hardware and software.

The BMP180 is a sensor with high precision and low power consumption. The measuring range is from 300hPa to 1110 hPa, equivalent to an altitude of -500m to 9000m above sea level. The absolute precision is 1.0 hPa, and the relative precision is 0.12 hPa, equivalent to an altitude precision of approximately 1m.

Communication is done via the I2C bus, making it easy to obtain measured data. The operating voltage is low, between 1.8 to 3.6V.

They are often integrated into modules such as the GY-68, which include the necessary electronics for easy connection to an Arduino. In most modules, this includes a voltage regulator that allows direct power supply at 5V.

The average power consumption is 0.1µA on standby and 650µA during measurement, resulting in an average consumption of 12µA when taking 1 sample per second at standard precision. The response time is about 5ms at standard resolution and 17ms at high resolution

The BMP180 and BMP085 barometers are widely used in meteorological applications, such as recording stations, weather clocks, or predicting rainfall, among others. They can also be used in air vehicles such as airplanes or quadcopters, or in applications that require measuring vertical displacement speed, for example, the ascent of a machine or the fall time of a device.

Price

The price of both BMP085 and BMP180 sensors is very similar. We can find either model for €0.90 from international sellers on eBay or AliExpress.

arduino-barometro-bmp180-componente

Since the BMP180 sensor is an improved version of the BMP085 and both have the same price, and are fully compatible in terms of hardware and software, the normal choice would be the BMP180.

Assembly scheme

The connection is simple; we simply power the module from Arduino using GND and 5V and connect the SDA and SCL pins of Arduino with the corresponding pins of the BMP180 barometer.

arduino-barometro-bmp180-esquema

When looking at the connection from the Arduino side, it would look like this.

arduino-barometro-bmp180-conexion

On Arduino Uno, Nano, and Mini Pro, SDA is pin A4 and SCK is pin A5. For other Arduino models, refer to the pinout diagram.

Before connecting to Arduino, make sure your board is 5V compatible. If not, you will need to use a level shifter.

Code Examples

To read the BMP180, we will use the library developed by Sparkfun, available at this link.

The library provides code examples that are worth reviewing. The following examples are modifications based on those available in the library.

Get Pressure and Temperature Values

The first example obtains the raw pressure and temperature values and displays them on the screen. These values are useful, for example, for creating a weather station.

#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 bmp180;

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

  if (bmp180.begin())
    Serial.println("BMP180 initialized");
  else
  {
    Serial.println("Error initializing BMP180");
    while(1); // infinite loop
  }
}

void loop()
{
  char status;
  double T,P;

  status = bmp180.startTemperature(); //Start temperature reading
  if (status != 0)
  {   
    delay(status); //Pause for reading to finish
    status = bmp180.getTemperature(T); //Get the temperature
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Start pressure reading
      if (status != 0)
      {        
        delay(status); //Pause for reading to finish        
        status = bmp180.getPressure(P,T); //Get the pressure
        if (status != 0)
        {                  
          Serial.print("Temperature: ");
          Serial.print(T,2);
          Serial.print(" *C , ");
          Serial.print("Pressure: ");
          Serial.print(P,2);
          Serial.println(" mb");          
        }      
      }      
    }   
  } 
  delay(1000);
}

Estimate Altitude Above Sea Level

The following example estimates the altitude above sea level. To do this, we use the standard pressure value for sea level as the reference.

#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 bmp180;

double SeaLevelPressure = 1013.25; //pressure at sea level in mbar

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

  if (bmp180.begin())
    Serial.println("BMP180 initialized");
  else
  {
    Serial.println("Error initializing BMP180");
    while(1);
  }
}

void loop()
{
  char status;
  double T,P,A;
  
  status = bmp180.startTemperature(); //Start temperature reading
  if (status != 0)
  {   
    delay(status); //Pause for reading to finish
    status = bmp180.getTemperature(T); //Get the temperature
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Start pressure reading
      if (status != 0)
      {        
        delay(status); //Pause for reading to finish        
        status = bmp180.getPressure(P,T); //Get the pressure
        if (status != 0)
        {                  
          Serial.print("Temperature: ");
          Serial.print(T);
          Serial.print(" *C , ");
          Serial.print("Pressure: ");
          Serial.print(P);
          Serial.print(" mb , ");     
          
          A= bmp180.altitude(P,SeaLevelPressure); //Calculate altitude
          Serial.print("Altitude: ");
          Serial.print(A);
          Serial.println(" m");    
        }      
      }      
    }   
  } 
  delay(1000);
}

Estimate Height Difference Between Two Points

In the last example, we measure the difference in height between two points. We simply measure the difference in elevation between two pressure points, but instead of taking the sea level as a reference, we take the first point as a reference.

#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 bmp180;

double Po; //pressure at the initial point for h=0;
char status;
double T,P,A;
void setup()
{
  Serial.begin(9600);

  if (bmp180.begin())
  {
    Serial.println("BMP180 initialized");
    status = bmp180.startTemperature(); //Start temperature reading
    if (status != 0)
    {   
      delay(status); //Pause for reading to finish
      status = bmp180.getTemperature(T);//Get the temperature
      if (status != 0)
      {
        status = bmp180.startPressure(3); //Start pressure reading
        if (status != 0)
        {        
          delay(status); //Pause for reading to finish        
          status = bmp180.getPressure(P,T); //Get the pressure
          if (status != 0)
          {                  
            Po=P; //Assign the pressure value as the reference point
            Serial.println("Reference point set: h=0");  
          }      
        }      
      }   
    }
    
  }
  else
  {
    Serial.println("Error initializing BMP180");
    while(1);
  }
}

void loop()
{
  status = bmp180.startTemperature(); //Start temperature reading
  if (status != 0)
  {   
    delay(status); //Pause for reading to finish
    status = bmp180.getTemperature(T); //Get the temperature
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Start pressure reading
      if (status != 0)
      {        
        delay(status); //Pause for reading to finish        
        status = bmp180.getPressure(P,T); //Get the pressure
        if (status != 0)
        {                    
          A= bmp180.altitude(P,Po); //Calculate altitude relative to the reference point
          Serial.print("h=");
          Serial.print(A);
          Serial.println(" m");    
        }      
      }      
    }   
  } 
  delay(1000);
}

Download the Code

All the code from this post is available for download on Github. github-full