Calculating Sea Level Pressure

Hi all, I have had some trouble trying to calculate sea level pressure using Arduino. Using absolute pressure figures from a sensor I have been able calculate sea level pressure which is useful for monitoring weather by using an Excel spreadsheet. The problem is, I haven't been able to get the formula to work properly for Arduino. Here is the formula I use for Excel:
=F2EXP(G2/(29.3H2))
F2 is absolute pressure, G2 is altitude in meters, which I use as 556 for where I live, and H2 is temperature in Kelvin, so T+273.15 works to get that figure.
I keep getting wild readings as if the syntax isn't right in Arduino.
In this example code that I can use to get for absolute pressure, (P,2) is the function to obtain that, but I've noticed P seems to work fine as well as it rounds to 2 decimals. Can anyone help me with this so I can get it working in Arduino like in Excel? Here is my code. Thank you.

#include "BMP280.h"
#include "Wire.h"
#define P0 1013.25
BMP280 bmp;
void setup()
{
  Serial.begin(9600);
  if(!bmp.begin()){
    Serial.println("BMP init failed!");
    while(1);
  }
  else Serial.println("BMP init success!");
  
  bmp.setOversampling(4);
  
}
void loop()
{
  double T,P;
  char result = bmp.startMeasurment();
 
  if(result!=0){
    delay(result);
    result = bmp.getTemperatureAndPressure(T,P);
    
      if(result!=0)
      {
        double A = bmp.altitude(P,P0);
        
        Serial.print("T = \t");
        Serial.print(T,2); 
        Serial.print(" degC\t");
        Serial.print("P = \t");
        Serial.print(P,2); 
        Serial.print(" mBar\t");
        Serial.print("A = \t");
        Serial.print(A,2); 
        Serial.println(" m");
       
      }
      else {
        Serial.println("Error.");
      }
  }
  else {
    Serial.println("Error.");
  }
  
  delay(5000);
}

In equation 2, there is a negative sign inside the exponent:

Your formula is missing a negative. Where exactly is your formula in your code?

What BMP280 library are you using? I have found this:

But it’s clearly not what you are using as it lacks the methods you seem to be calling. Aside from anything else, this bmp.startMeasurment(); is misspelled. Who wrote this code?

Also, as liudr mntioned, you post about some equation you use, but I don’t see it anywhere in your code and I don’t know what it has to do with your sketch not working.

If you are getting weird results, possibilities are:

  • sensor not working/not wired up
  • the delay() business not being done right
  • bmp.getTemperatureAndPressure(T,P);

I suspect that

  1. you wrote the library
  2. you thought that assigning a value to a parameter would write back the result to the place where it was called from

This only happens if the parameter is declared as a reference.

I think that T and P contain ribbish off the stack. To test, explicitly assign zero to T and P before calling the methods to take the reading.

Calculate what pressure you expect to see at your altitude and "normal" sea level pressure. 1005 millibars or whatever it is.

If you are not getting close to that from your sensor, there is something wrong with the way you are getting the data.

Be aware that some of the published code for those BMP sensors is actually wrong.

liudr:
In equation 2, there is a negative sign inside the exponent:

You fail to understand the problem is computing the pressure at sea level given a reading
at altitude, not the other way. Sea level pressure is higher.

GoBraves21:
I haven't been able to get the formula to work properly for Arduino. Here is the formula I use for Excel:
=F2EXP(G2/(29.3H2))
F2 is absolute pressure, G2 is altitude in meters, which I use as 556 for where I live, and H2 is temperature in Kelvin, so T+273.15 works to get that figure.

Try this:

void setup() {
  // put your setup code here, to run once:
  Serial.begin (115200) ;
  float F2 = 101000.0;
  float G2 = 556;
  float T = 25.0 ;
  Serial.println (F2 * exp (G2 / (29.3 * (T + 273.15)))) ;
}

void loop () {}

Even simpler and close enough for practical purposes:

  Serial.println (F2 * 1.068) ;

Where is the equation in the OP? I don't see any math in that code.

MarkT:
You fail to understand the problem is computing the pressure at sea level given a reading
at altitude, not the other way. Sea level pressure is higher.

Yeah, you're right. The OP needs explain this more clearly than giving out Excel cell references.

Thanks a lot Mark! I like the spaces in there too, so it's not so cluttered. The EXP function was what was throwing it off for me I believe. I didn't realize exp in lowercase had to be used. I didn't have to use any float variables for this either.

The Adafruit library for whatever reason would not work for me as someone asked about that.

This library is another one I found on github a while back: GitHub - mahfuz195/BMP280-arduino-library: This BMP280 sensor library uses I2C communication with Arduino/ESP8266 and read the Temperature( degC) , Pressure (mBar) and Altitude (m).

There is a misspelling indeed in this part of the code bmp.startMeasurment(), but the library seems to work well and it's the simplest I've found at least with an example.

By the way, with that library, here is the formula in Arduino if you want sea level air pressure in millibars so you can compare with your local weather: (P * exp (556 / (29.3 * (T + 273.15))));

Just to let everyone know for sure so there's no confusion, P is the absolute pressure reading and T is temperature in Celsius. The 556 is height in meters, so obviously change that for you.

Being in the U.S., we mainly use pressure in inches so here is that formula:
(P * exp (556 / (29.3 * (T + 273.15)))*0.0295301);

Thanks again for the help and responses, especially to Mark!