0
$\begingroup$

I am using BMP180 I2C sensor interface with Arduino and with the help of mentioned below code

#include <Wire.h>

// BMP180 I2C address is 0x77(109)
#define Addr 0x77

int coff[11] = {0};
unsigned int AC4 = 0, AC5 = 0, AC6 = 0;
unsigned int data[3] = {0};

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  for (int i = 0; i < 11; i++)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((170 + (2 * i)));
    // Stop I2C Transmission
    Wire.endTransmission(true);

    // Request 2 bytes of data
    Wire.requestFrom(Addr, 2);

    // Read 2 bytes of data
    // coff msb, coff lsb
    if (Wire.available() == 2)
    {
      data[0] = Wire.read();
      data[1] = Wire.read();
    }

    delay(50);
    coff[i] = data[0] * 256 + data[1];
    if ((coff[i] > 32767) && (i != 3) && (i != 4) && (i != 5))
    {
      coff[i] -= 65536;
    }
  }
  AC4 = coff[3];
  AC5 = coff[4];
  AC6 = coff[5];
  delay(300);
}

void loop()
{
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select measurement control register
  Wire.write(0xF4);
  // Enable temperature measurement
  Wire.write(0x2E);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select data register
  Wire.write(0xF6);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 2 bytes of data
  Wire.requestFrom(Addr, 2);

  // Read 2 bytes of data
  // temp msb, temp lsb
  if (Wire.available() == 2)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
  }

  // Convert the data
  float temp = ((data[0] * 256.0) + data[1]);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select measurement control register
  Wire.write(0xF4);
  // Enable pressure measurement, OSS = 1
  Wire.write(0x74);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select data register
  Wire.write(0xF6);
  // Stop I2C Transmission
  Wire.endTransmission();
  // Request 3 bytes of data
  Wire.requestFrom(Addr, 3);

  // Read 3 bytes of data
  // pres msb1, pres msb, pres lsb
  if (Wire.available() == 3)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
  }

  // Convert the data
  long long pres = (((long long)data[0] * (long long)65536) + ((long)data[1] * 256) + data[2]) / 128;

  // Callibration for Temperature
  double X1 = (temp - AC6) * AC5 / 32768.0;
  double X2 = ((double)coff[9] * 2048.0) / (X1 + coff[10]);
  double B5 = X1 + X2;
  float cTemp = ((B5 + 8.0) / 16.0) / 10.0;
  float fTemp = cTemp * 1.8 + 32;

  // Calibration for Pressure
  double B6 = B5 - 4000;
  X1 = (coff[7] * ((long)B6 * (long)B6 / 4096.0)) / 2048.0;
  X2 = coff[1] * B6 / 2048.0;
  double X3 = X1 + X2;
  double B3 = (((coff[0] * 4 + X3) * 2) + 2) / 4.0;
  X1 = coff[2] * B6 / 8192.0;
  X2 = (coff[6] * ((long)B6 * (long)B6 / 2048.0)) / 65536.0;
  X3 = ((X1 + X2) + 2) / 4.0;
  double B4 = AC4 * (X3 + 32768) / 32768.0;
  long long B7 = (((unsigned long long )pres - (unsigned long long )B3) * ((unsigned long long )25000.0));
  float pressure = 0.0;
  if (B7 < 2147483648)
  {
    pressure = (B7 * 2) / B4;
  }
  else
  {
    pressure = (B7 / B4) * 2;
  }
  X1 = (pressure / 256.0) * (pressure / 256.0);
  X1 = (X1 * 3038.0) / 65536.0;
  X2 = ((-7357) * pressure) / 65536.0;
  pressure = (pressure + (X1 + X2 + 3791) / 16.0) / 100;

  // Calculate Altitude
  float height = 44330 * (1 - pow((pressure / 1013.25), 0.1903));

  // Output data to serial monitor
  Serial.print("Altitude : ");
  Serial.print(height);
  Serial.println(" m");
  Serial.print("Pressure : ");
  Serial.print(pressure);
  Serial.println(" hPa");
  Serial.print("Temperature in Celsius : ");
  Serial.print(cTemp);
  Serial.println(" C");
  Serial.print("Temperature in Fahrenheit : ");
  Serial.print(fTemp);
  Serial.println(" F");
  delay(500);
}

I am getting the values as mentioned below from above the sea level my value for altitude is coming in negative as mentioned below.

Altitude : -3143.89 m
Pressure : 1457.78 hPa
Temperature in Celsius : 24.34 C
Temperature in Fahrenheit : 75.81 F

I want to display the altitude height in feet in positive parameters, I might be doing something wrong in formula which I am not able to under any suggestion on this will be a great help

$\endgroup$
0
$\begingroup$

The altitude is derived from the pressure measurement/calculation using the STP value of 1013.25 hPa.

Since air pressure at sea level is about 1000 hPa, depending on what the air pressure variation is from weather systems, a pressure of 1457.78 hPa places the altitude ~3000 m below sea level.

To display the altitude in feet you will need to convert from metric (1 ft = 0.3048 m).

$\endgroup$
  • $\begingroup$ But There is some issue in code "1457.78hpa" much higher and not able to find out the problem even I used every logic mentioned properly in Datasheet of BMP180 $\endgroup$ – varul jain Feb 21 at 5:52
  • $\begingroup$ If it requires a different value for the air pressure calculation in order to calibrate the device then you will need to know the air pressure and temperature at the location independently of the device. $\endgroup$ – Mick Feb 21 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.