Skip to content

Commit 0fd24e9

Browse files
committed
altitude property
1 parent 26c76cb commit 0fd24e9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

adafruit_bmp5xx.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def __init__(self, i2c: I2C, address: int = DEFAULT_ADAFRUIT_ADDR) -> None:
228228
except ValueError:
229229
raise ValueError(f"No I2C device found at address 0x{address:02X}")
230230

231+
self.sea_level_pressure = 1013.25
231232
self.reset()
232233
time.sleep(0.0025)
233234

@@ -261,6 +262,12 @@ def pressure(self) -> float:
261262
raw_p = self._pressure
262263
return raw_p / 64.0 / 100.0 # Convert raw_data->Pa->hPa
263264

265+
@property
266+
def altitude(self) -> float:
267+
"""The altitude in meters based on the currently set sea level pressure."""
268+
# see https://www.weather.gov/media/epz/wxcalc/pressureAltitude.pdf
269+
return 44307.7 * (1 - (self.pressure / self.sea_level_pressure) ** 0.190284)
270+
264271
def reset(self) -> None:
265272
"""Reset the BMP5xx device."""
266273
self.command = BMP5_SOFT_RESET_CMD

examples/bmp5xx_simpletest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
from adafruit_bmp5xx import BMP5XX
99

10+
SEALEVELPRESSURE_HPA = 1013.25
11+
1012
i2c = board.STEMMA_I2C()
1113
bmp = BMP5XX(i2c)
1214
start_time = time.monotonic()
1315

16+
bmp.sea_level_pressure = SEALEVELPRESSURE_HPA
17+
1418
while True:
1519
if bmp.data_ready:
16-
print(f"temp F: {bmp.temperature * (9 / 5) + 32} pressure: {bmp.pressure} hPa")
20+
print(
21+
f"temp F: {bmp.temperature * (9 / 5) + 32} "
22+
f"pressure: {bmp.pressure} hPa "
23+
f"Approx altitude: {bmp.altitude} m"
24+
)
1725
time.sleep(1)

0 commit comments

Comments
 (0)