Calculating altitude above ground level with BMP280

So for the past week, I have been frantically looking around the internet trying to find out how I can use a BMP280 pressure sensor to calculate altitude above ground level.

I understand that I need to know the pressure at ground level, and some math roughly along the lines of:

"if pressure is = X, then altitude is =Y"

I have heard that it is possible for the sensor to read pressure as 0 when turned on, and then every meter in altitude, the pressure increases by 0.121738 millibars. How would I go about getting the sensor to read 0 when turned on?

Let me know if you need more clarification!

-Lucas Rozanski

with a library?

Most people calibrate a pressure sensor so that it reads the correct altitude when turned on. Then you can track changes in altitude.

The basic approach to convert pressure readings to altitude is described here: Air Pressure at Altitude Calculator

I understand that. But I think that it would be much easier for the pressure sensor to just read 0 once turned on, and then track the pressure as it rises in altitude.

Calibrate the sensor in exactly the same way, but enter zero for the current altitude.

Okay. could you maybe provide me with some code? maybe a link? And where would I enter zero as the altitude?

I am very new to using sensors and using their data to preform actions. My end goal is to use the maximum altitude of a model rocket to calculate it's velocity when it hits the ground. from this information, I can determine when to ignite a rocket motor to land it similar to BPS.space.

You have code sample from a library in post #2

You can read the sensor when it is turned on, then store that value as the zero altitude reference, making all the following calculations based on the difference between that value and the current value.

To be honest, i still have no idea what to do.

You will also need to consider other factors, such as air density, temperature, wind speed and direction, drag of the rocket, etc. Propulsive landing using a solid rocket motor is going to be extremely difficult.

I know that it is going to be difficult, and it does not need to be exact. I am planning on incorporating those factors later on.

Let me clarify a little, right now, I need to find out how to code this:

"When the BMP280 turns on, it reads the pressure at the current altitude. Using the number that air pressure drops at per meter of increased altitude, calculate the altitude in meters. "

That is all I need. I have figured out some equations to calculate the best time to burn the motor on my own.

Correction,

"calculate the altitude in meters ABOVE GROUND LEVEL".

That seems very straightforward, you take a reading of the pressure at ground level (presumably when the power is turned on the rocket is sitting on the ground). Store that value as the ground reference. To calculate altitude about ground level, take a reading of the current pressure, subtract the ground reference, and you now have the pressure differential with which to calculate the altitude.

What would the math for calculating the altitude look like?

in the library above, if in your setup you call bmp.readAltitude(1013.25) then you get some sort of a reference altitude if the sea level hPa was 1013.25 (which is the average pressure at mean sea-level (MSL) in the International Standard Atmosphere (ISA)). Store this in a float variable startAltitude.

Then later when you call again bmp.readAltitude(1013.25), if you want the relative altitude, just subtract startAltitude.

You could modify the library to provide a setReference() and a readRelativeAltitude() methods that would "hide" this math away for you...

Okay, that sounds pretty simple. I m definitely going to need to come back and read this post a few more times though! HAHAHA.
Thanks for all of the help!

Last question.

Do i need to use ALL of the 600 lines of code in what was posted, or do i only need to use the highlighted part?

install and use the library. Something like this if you have an I2C module

#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // I2C
float startAltitude = 0;

void setup() {
  Serial.begin(115200);
  Serial.println(F("BMP280 test"));

  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring / I2C address"));
    while (true) yield();
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

  startAltitude = bmp.readAltitude(1013.25);
}

void loop() {
  Serial.print(F("Relative altitude = "));
  Serial.print(bmp.readAltitude(1013.25) - startAltitude);  // approximative
  Serial.println(" m");
  delay(1000);
}

Serial monitor at 115200 bauds will print the relative altitude every second