## [SOLVED] BMP085 - how to set altitude?

Onira
Posts: 233
Joined: Tue Oct 02, 2012 6:24 pm
Location: Poland

### Re: BMP085 - how to set altitude?

today I got:

Code: Select all

``````\$ python Adafruit_BMP085_example.py
Temperature: 23.60 C
Pressure:    994.85 hPa
Altitude:    127.57``````
the MSL pressure is from yesterday...
I understand that the altitude is constant
and... if the pressure changes during the day, the MSL pressure will change too so I will get wrong altitude
am I right?
Cogito ergo sum

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: BMP085 - how to set altitude?

Yes.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

Onira
Posts: 233
Joined: Tue Oct 02, 2012 6:24 pm
Location: Poland

### Re: BMP085 - how to set altitude?

thank You for explaining
I'm just bit silly in some cases
Cogito ergo sum

Nige C
Posts: 48
Joined: Thu Jul 19, 2012 12:22 am

### Re: BMP085 - how to set altitude?

DougieLawson wrote:With any barometric altimeter you can only read pressure OR altitude you can't read both. The pressure reading assumes you're at mean sea level (MSL)

If you know an accurate MSL (aka QNH) pressure value, you can fine tune your altitude value. If your location is above sea level then you need to fine tune both values. The standard is to base everything off 1013.25 hPA. But if your local MSL pressure is higher or lower than that then your altitude will be wrong.

Above 18,000ft everyone sets their altimeter to 1013.25 as that stops them colliding with other aircraft who should be maintaining 1,000ft vertical separation if they're flying in the opposite direction to you.

http://www.stolaf.edu/people/hansonr/so ... timetr.htm has an explanation.
http://en.wikipedia.org/wiki/Atmospheric_pressure has the formula for how pressure changes as you go up from sea level.

An end-user GPS receiver is good to about 10m resolution for Z value so can be used to give a reasonable approximation of your altitude. Local trig points are well defined and accurately measured so if you can take your RPi up a mountain with a trig point you can accurately calibrate it. The contour lines on a 1:25,000 Ordnance Survey sheet are also relatively accurate but to use those relies on having an accurate X & Y location. Again with end-user GPS you can be off by 10m in either X or Y or both.

Remember the pressure changes every day with the weather so you have to re-calibrate your altimeter every time the pressure changes.
Relative pressure is the key, I know my height above sea level. I do not adjust my pressure sensor each day. I have set it to a level. The rate of change above or below indicates change of weather. That is all I need. I do not fly an aircraft, but do take account of a local military airfields public posts of weather data. Near where I live.
Retired power engineer with a lot of real life experience. Life is an adventure again

Onira
Posts: 233
Joined: Tue Oct 02, 2012 6:24 pm
Location: Poland

### Re: [SOLVED] BMP085 - how to set altitude?

how did you set the code?
Cogito ergo sum

RH314
Posts: 8
Joined: Thu Jan 30, 2014 3:23 pm

### Re: [SOLVED] BMP085 - how to set altitude?

Barometer is accurate in determining altitude change, but needs calibration from time to time for determining absolute altitude. GPS altitude accuracy although less accurate than horizontal accuracy, it still provides some good absolute reference for altitude in the long term. Maybe it’s good to devise a method to calibrate barometer using GPS altitude to get a better combined absolute altitude result.

nvdolfan
Posts: 2
Joined: Fri Jan 31, 2014 4:02 am

### Re: BMP085 - how to set altitude?

DougieLawson wrote: Pressure at Mean Sea Level is constantly varying.
Your local pressures is constantly varying.
The ratio between the two is constant for all sensible purposes.
Would this be a more accurate way for showing the altitude then:
Take the ration of your current MSL pressure, and the BMP085 measured pressure and then always multiple that ratio times the measured pressure to calculate the altitude?
For me the ratio was 1.0422 so I modified the python script as such:

The first couple measurements had a much more accurate altitude, but I don't know how stable it will be.
I will see how it works over the next few days as the pressure and temperature change...
If it's "close enough" could create a new equation pretty easy to log the MSL since showing the calculated altitude seems to be meaningless at this point.

Onira
Posts: 233
Joined: Tue Oct 02, 2012 6:24 pm
Location: Poland

### Re: [SOLVED] BMP085 - how to set altitude?

still, I need program to do that
Cogito ergo sum

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: BMP085 - how to set altitude?

nvdolfan wrote:
Would this be a more accurate way for showing the altitude then:
Take the ration of your current MSL pressure, and the BMP085 measured pressure and then always multiple that ratio times the measured pressure to calculate the altitude?
For me the ratio was 1.0422 so I modified the python script as such:

The first couple measurements had a much more accurate altitude, but I don't know how stable it will be.
I will see how it works over the next few days as the pressure and temperature change...
If it's "close enough" could create a new equation pretty easy to log the MSL since showing the calculated altitude seems to be meaningless at this point.
The problem is that you need a simultaneous reading at both sites. Readings six or more hours apart are OK for landing aircraft (because the two humans in the front seats compensate for the inaccuracies) but probably not for your purpose.

That's where your national weather bureau has a massive advantage over the amateur meteorologists. They own all of the sensors and have them on a synchronised time base.

You may be able to use http://www.wunderground.com/ if you can get reliable data from local stations at known altitudes you can join that all up.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

nvdolfan
Posts: 2
Joined: Fri Jan 31, 2014 4:02 am

### Re: BMP085 - how to set altitude?

DougieLawson wrote:
The problem is that you need a simultaneous reading at both sites. Readings six or more hours apart are OK for landing aircraft (because the two humans in the front seats compensate for the inaccuracies) but probably not for your purpose.

That's where your national weather bureau has a massive advantage over the amateur meteorologists. They own all of the sensors and have them on a synchronised time base.

You may be able to use http://www.wunderground.com/ if you can get reliable data from local stations at known altitudes you can join that all up.
If the ratio is constant, you only need the simultaneous reading once, and then you can calculate the approximate MSL each time. You could keep tweaking the ratio until the altitude doesn't change.

Using the equation I put above, overnight my elevation shifted back and forth by +/- 1.5 meters while the pressure dropped ~1.5, so I don't think it will work, but it's still an interesting experiment.

ant_thomas
Posts: 115
Joined: Fri May 04, 2012 4:33 pm

### Re: [SOLVED] BMP085 - how to set altitude?

I'm probably not getting my head around this properly.

Just installed a BMP180 sensor (same code as BMP085)

My altitude is constant (and known). Is it possible to set a constant altitude?

Every reading is coming out with a different altitude, I kind of understand why, but I'd like to set a constant altitude and watch the pressure change. Is this possible? Or do I not quite understand how this works?

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: [SOLVED] BMP085 - how to set altitude?

Look at the code. It reads pressure from the sensor and calculates altitude. If you don't pass any parms the altitude is based on 1013.5 hPa (standard pressure). You should be able to work from that and have a fixed constant for your local altitude.

There is a function in the code to return an uncorrected pressure value. There's also a flag to emit some debugging messages.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

ant_thomas
Posts: 115
Joined: Fri May 04, 2012 4:33 pm

### Re: [SOLVED] BMP085 - how to set altitude?

DougieLawson wrote:Look at the code. It reads pressure from the sensor and calculates altitude. If you don't pass any parms the altitude is based on 1013.5 hPa (standard pressure). You should be able to work from that and have a fixed constant for your local altitude.

There is a function in the code to return an uncorrected pressure value. There's also a flag to emit some debugging messages.
That's great.

So to confirm, if I have absolutely no interest in altitude (especially since it's calculated) that can just be ignored and the pressure reading is correct for the location of the sensor?

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: [SOLVED] BMP085 - how to set altitude?

This

Code: Select all

``````#!/usr/bin/python

bmp = BMP085(0x77)
should do what you want and get a current pressure value for the location and altitude where you've got the sensor.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

ant_thomas
Posts: 115
Joined: Fri May 04, 2012 4:33 pm

### Re: [SOLVED] BMP085 - how to set altitude?

DougieLawson wrote:This

Code: Select all

``````#!/usr/bin/python

bmp = BMP085(0x77)
should do what you want and get a current pressure value for the location and altitude where you've got the sensor.
Thanks, that's exactly what I'm doing.

Si_B
Posts: 2
Joined: Mon Jan 08, 2018 6:23 pm

### Re: [SOLVED] BMP085 - how to set altitude?

could you clarify where you can edit these parameters in the code. I followed the Adafruit_python_BMP install, all connected up and reporting using simpletest.py

Unfortunately I cannot get the correct pressure and altitude to display

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: [SOLVED] BMP085 - how to set altitude?

You have a barometric sensor that gives you ONE value. That's your air pressure (in hPa) at your elevation (QFE).

To know your elevation you need air pressure at mean sea-level (QNH).
If you know your elevation (in metres) you can calculate MSL pressure using a standard formula.

You can't calculate elevation without knowing the other two values (local pressure & sea-level pressure).
You can't calculate MSL without knowing the other two values (local pressure and local elevation).

Code: Select all

``````#!/usr/bin/python
import math
import sys
import argparse

if __name__ == '__main__':
parser = argparse.ArgumentParser()
args = parser.parse_args()
if args.elevation is None:
args.elevation = 112.2  # Value from my GPS for my house
if args.pressure is None:
print "Pressure value required"
sys.exit(40)
if args.msl is None:
args.msl = 1013.25
args.pressure = float(args.pressure)
args.elevation = float(args.elevation)
args.msl = float(args.msl)

print "Elevation:", args.elevation, "Pressure:", args.pressure, "MSL:", args.msl
print "Sealevel:",args.pressure/pow(1-(args.elevation/44330.0),5.255)
print "Elevation:",(44330.0*(1-pow(args.pressure/args.msl,1/5.255)))
``````
With a pressure measurement of 1002.68 hPa from my BMP180.

Code: Select all

``````pi@beaufort:~ \$ python ./elev_press.py -p 1002.68 -e 112
Elevation: 112.0 Pressure: 1002.68 MSL: 1013.25
Sealevel: 1016.09820523
Elevation: 88.3741544612
pi@beaufort:~ \$ python ./elev_press.py -p 1002.68 -m 1016.10
Elevation: 112.2 Pressure: 1002.68 MSL: 1016.1
Sealevel: 1016.1223568
Elevation: 112.014862752
pi@beaufort:~ \$
``````
So knowing local pressure and elevation gives us MSL == 1016.098
Knowing MSL = 1016.1 and local pressure gives us elevation of 112.01 metres

In both cases two values are needed to return the third value.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

Si_B
Posts: 2
Joined: Mon Jan 08, 2018 6:23 pm

### Re: [SOLVED] BMP085 - how to set altitude?

Ok, I am still maybe missing something!

I get what you are explaning here but do i need the code you have posted to get the correct pressure/ altitude? I have the Adafruit_Python_BMP installed and have been trying to run the the following simpletest.py from the examples folder?

Code: Select all

``````#!/usr/bin/python
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Can enable debug output by uncommenting:
#import logging
#logging.basicConfig(level=logging.DEBUG)

# Default constructor will pick a default I2C bus.
#
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
# from the main GPIO header and the library will figure out the bus number based
# on the Pi's revision.
#
# For the Beaglebone Black the library will assume bus 1 by default, which is
# exposed with SCL = P9_19 and SDA = P9_20.
sensor = BMP085.BMP085()

# Optionally you can override the bus number:
#sensor = BMP085.BMP085(busnum=2)

# You can also optionally change the BMP085 mode to one of BMP085_ULTRALOWPOWER,
# BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES.  See the BMP085
# datasheet for more details on the meanings of each mode (accuracy and power
# consumption are primarily the differences).  The default mode is STANDARD.
#sensor = BMP085.BMP085(mode=BMP085.BMP085_ULTRAHIGHRES)

``````

do I not input the Sealevel and Altitude calibrations in the BMP085.py at the bottom?

Code: Select all

``````# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import division
import logging
import time

# Operating Modes
BMP085_ULTRALOWPOWER     = 0
BMP085_STANDARD          = 1
BMP085_HIGHRES           = 2
BMP085_ULTRAHIGHRES      = 3

# BMP085 Registers
BMP085_CAL_AC1           = 0xAA  # R   Calibration data (16 bits)
BMP085_CAL_AC2           = 0xAC  # R   Calibration data (16 bits)
BMP085_CAL_AC3           = 0xAE  # R   Calibration data (16 bits)
BMP085_CAL_AC4           = 0xB0  # R   Calibration data (16 bits)
BMP085_CAL_AC5           = 0xB2  # R   Calibration data (16 bits)
BMP085_CAL_AC6           = 0xB4  # R   Calibration data (16 bits)
BMP085_CAL_B1            = 0xB6  # R   Calibration data (16 bits)
BMP085_CAL_B2            = 0xB8  # R   Calibration data (16 bits)
BMP085_CAL_MB            = 0xBA  # R   Calibration data (16 bits)
BMP085_CAL_MC            = 0xBC  # R   Calibration data (16 bits)
BMP085_CAL_MD            = 0xBE  # R   Calibration data (16 bits)
BMP085_CONTROL           = 0xF4
BMP085_TEMPDATA          = 0xF6
BMP085_PRESSUREDATA      = 0xF6

# Commands

class BMP085(object):
# Check that mode is valid.
if mode not in [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES]:
raise ValueError('Unexpected mode value {0}.  Set mode to one of BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES'.format(mode))
self._mode = mode
# Create I2C device.
if i2c is None:
i2c = I2C

self._logger.debug('AC1 = {0:6d}'.format(self.cal_AC1))
self._logger.debug('AC2 = {0:6d}'.format(self.cal_AC2))
self._logger.debug('AC3 = {0:6d}'.format(self.cal_AC3))
self._logger.debug('AC4 = {0:6d}'.format(self.cal_AC4))
self._logger.debug('AC5 = {0:6d}'.format(self.cal_AC5))
self._logger.debug('AC6 = {0:6d}'.format(self.cal_AC6))
self._logger.debug('B1 = {0:6d}'.format(self.cal_B1))
self._logger.debug('B2 = {0:6d}'.format(self.cal_B2))
self._logger.debug('MB = {0:6d}'.format(self.cal_MB))
self._logger.debug('MC = {0:6d}'.format(self.cal_MC))
self._logger.debug('MD = {0:6d}'.format(self.cal_MD))

# Set calibration from values in the datasheet example.  Useful for debugging the
# temp and pressure calculation accuracy.
self.cal_AC1 = 408
self.cal_AC2 = -72
self.cal_AC3 = -14383
self.cal_AC4 = 32741
self.cal_AC5 = 32757
self.cal_AC6 = 23153
self.cal_B1 = 6190
self.cal_B2 = 4
self.cal_MB = -32767
self.cal_MC = -8711
self.cal_MD = 2868

"""Reads the raw (uncompensated) temperature from the sensor."""
time.sleep(0.005)  # Wait 5ms
self._logger.debug('Raw temp 0x{0:X} ({1})'.format(raw & 0xFFFF, raw))
return raw

"""Reads the raw (uncompensated) pressure level from the sensor."""
self._device.write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (self._mode << 6))
if self._mode == BMP085_ULTRALOWPOWER:
time.sleep(0.005)
elif self._mode == BMP085_HIGHRES:
time.sleep(0.014)
elif self._mode == BMP085_ULTRAHIGHRES:
time.sleep(0.026)
else:
time.sleep(0.008)
raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self._mode)
self._logger.debug('Raw pressure 0x{0:04X} ({1})'.format(raw & 0xFFFF, raw))
return raw

"""Gets the compensated temperature in degrees celsius."""
# Datasheet value for debugging:
#UT = 27898
# Calculations below are taken straight from section 3.5 of the datasheet.
X1 = ((UT - self.cal_AC6) * self.cal_AC5) >> 15
X2 = (self.cal_MC << 11) // (X1 + self.cal_MD)
B5 = X1 + X2
temp = ((B5 + 8) >> 4) / 10.0
self._logger.debug('Calibrated temperature {0} C'.format(temp))
return temp

"""Gets the compensated pressure in Pascals."""
# Datasheet values for debugging:
#UT = 27898
#UP = 23843
# Calculations below are taken straight from section 3.5 of the datasheet.
# Calculate true temperature coefficient B5.
X1 = ((UT - self.cal_AC6) * self.cal_AC5) >> 15
X2 = (self.cal_MC << 11) // (X1 + self.cal_MD)
B5 = X1 + X2
self._logger.debug('B5 = {0}'.format(B5))
# Pressure Calculations
B6 = B5 - 4000
self._logger.debug('B6 = {0}'.format(B6))
X1 = (self.cal_B2 * (B6 * B6) >> 12) >> 11
X2 = (self.cal_AC2 * B6) >> 11
X3 = X1 + X2
B3 = (((self.cal_AC1 * 4 + X3) << self._mode) + 2) // 4
self._logger.debug('B3 = {0}'.format(B3))
X1 = (self.cal_AC3 * B6) >> 13
X2 = (self.cal_B1 * ((B6 * B6) >> 12)) >> 16
X3 = ((X1 + X2) + 2) >> 2
B4 = (self.cal_AC4 * (X3 + 32768)) >> 15
self._logger.debug('B4 = {0}'.format(B4))
B7 = (UP - B3) * (50000 >> self._mode)
self._logger.debug('B7 = {0}'.format(B7))
if B7 < 0x80000000:
p = (B7 * 2) // B4
else:
p = (B7 // B4) * 2
X1 = (p >> 8) * (p >> 8)
X1 = (X1 * 3038) >> 16
X2 = (-7357 * p) >> 16
p = p + ((X1 + X2 + 3791) >> 4)
self._logger.debug('Pressure {0} Pa'.format(p))
return p

"""Calculates the altitude in meters."""
# Calculation taken straight from section 3.6 of the datasheet.
altitude = 44330.0 * (1.0 - pow(pressure / sealevel_pa, (1.0/5.255)))
self._logger.debug('Altitude {0} m'.format(altitude))
return altitude

"""Calculates the pressure at sealevel when given a known altitude in
meters. Returns a value in Pascals."""
p0 = pressure / pow(1.0 - altitude_m/44330.0, 5.255)
self._logger.debug('Sealevel pressure {0} Pa'.format(p0))
return p0/code]``````

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: [SOLVED] BMP085 - how to set altitude?

YOU NEED TWO VALUES TO CALCULATE THE THIRD!

Do I have to shout that from the rooftop (which is approx 117m above sea level in my case)?

The bmp085.py program gives you ONE value, current air pressure at current elevation, nothing more, nothing less (apart from ambient temperature).

If you don't know current pressure at mean sea-level or don't know current elevation (from a GPS receiver, for example, or from a triangulation point on a map) you cannot use the current air pressure at current elevation for anything useful except as a relative value to yesterday's pressure (unless you live on the beach).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

scruss
Posts: 3327
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

### Re: [SOLVED] BMP085 - how to set altitude?

That library really should have some better setup code, though. Barometric pressure sensors are typically used in either fixed station mode (where you want to measure variable air pressure for a known elevation) or mobile mode (where you want to measure elevation based on known weather conditions). Leaving both up to the user is a little lightweight.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

DougieLawson
Posts: 39563
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK