ElectroPeak
Published © GPL3+

Determining the Pressure and Altitude Using GY-68 BMP180 and

In this tutorial, you’ll learn how to use the BMP180 sensor, which is one of the most commonly used sensors for measuring the pressure.

BeginnerProtip30,339

Things used in this project

Hardware components

Arduino UNO R3
×1
ElectroPeak BOSH BMP180
×1
ElectroPeak Jumper Wire
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code 1

Arduino
*/

  BMP180 

  modified on 23 Apr 2019
  by Saeed Hosseini @ Electropeak
  https://electropeak.com/learn

*/

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

SFE_BMP180 pressure;

#define ALTITUDE 943.7 // Altitude of Electropeak Co. in meters

void setup() {
  Serial.begin(9600);
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    Serial.println("Check connection");
    while (1);
  }

}

void loop() {

  char status;
  double T, P, p0, a;

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T, 2);
      Serial.println(" deg C ");

      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);
        status = pressure.getPressure(P, T);
        if (status != 0)
        {

          Serial.print("absolute pressure: ");
          Serial.print(P, 2);
          Serial.print(" hpa = ");
          Serial.print(P * 100, 2);
          Serial.print(" pa = ");
          Serial.print(P * 0.000986923, 2);
          Serial.print(" atm = ");
          Serial.print(P * 0.750063755, 2);
          Serial.print(" mmHg = ");
          Serial.print(P * 0.750061683, 2);
          Serial.print(" torr = ");
          Serial.print(P * 0.014503774, 2);
          Serial.println(" psi");


          p0 = pressure.sealevel(P, ALTITUDE); // we're at 943.7 meters 
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0, 2);
          Serial.println(" hpa ");;


          a = pressure.altitude(P, p0);
          Serial.print("your altitude: ");
          Serial.print(a, 0);
          Serial.println(" meters ");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
Serial.println("==========================================================================");
  delay(5000);
}

Code2

Arduino
/*

  BMP180 - altimeter

  modified on 23 Apr 2019
  by Saeed Hosseini @ Electropeak
  https://electropeak.com/learn

*/

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


SFE_BMP180 pressure;

double baseline;

void setup()
{
  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while (1);
  }

  baseline = readPressure();

  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" hpa");
}

void loop()
{
  double a, P;


  P = readPressure();

  a = pressure.altitude(P, baseline);

  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" ");
  Serial.print(a, 1);
  Serial.println(" meters, ");
  delay(500);
}


double readPressure()
{
  char status;
  double T, P, p0, a;
  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);
        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          return (P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

Credits

ElectroPeak

ElectroPeak

57 projects • 736 followers
At ElectroPeak we want to teach you to enjoy electronics more. We offer Top-notch guides and worry-free shopping experience.

Comments