Software source reference
"""
Contains some helper functions to initialize and communicate with the BMP390 sensor, which
shares the same code base as the BMP388 sensor.
Authors:
Min Tai Yap
"""
import time
import board
import busio
import adafruit_bmp3xx
import asyncio
from adafruit_blinka.microcontroller.bcm283x.pin import Pin
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
# SPI setup
# from digitalio import DigitalInOut, Direction
# spi = board.SPI()
# cs = DigitalInOut(board.D5)
# bmp = adafruit_bmp3xx.BMP3XX_SPI(spi, cs)
[docs]
async def bmp388_init(i2c, bmp_slp):
"""
Initializes the BMP390 sensor on a given I2C bus. Tries both possible I2C addresses, which are 0x76 and 0x77.
The driver object for the BMP390 sensor is initialized with pressure_oversampling set to 8 and
temperature_oversampling set to 2. Also, the sea level pressure of the driver object is initialized to
the bmp_slp value specified in parameter/parameters.txt.
:param i2c: A busio.I2C object for the communication on the I2C bus
:param bmp_slp: The sea level pressure in hPa in the deployment region
:returns: adafruit_bmp3xx.BMP3XX_I2C driver object for the I2C bus-connected BMP390 sensor
"""
for address in [0x76, 0x77]:
try:
bmp = adafruit_bmp3xx.BMP3XX_I2C(i2c, address=address)
# Configure sensor settings
bmp.pressure_oversampling = 8
bmp.temperature_oversampling = 2
bmp.sea_level_pressure = bmp_slp
print(f"BMP390 initialized at I2C address 0x{address:X}")
return bmp # Return the initialized sensor object
except ValueError:
print(f"No sensor found at address 0x{address:X}, trying next...")
except Exception as e:
print(f"Error initializing BMP390 at 0x{address:X}: {e}")
print("BMP390 sensor not found on any I2C address.")
return None
[docs]
async def bmp388_read(bmp):
"""
Reads temperature, pressure, and altitude from an initialized BMP390 sensor.
:param bmp: adafruit_bmp3xx.BMP3XX_I2C driver object for the I2C bus-connected BMP390 sensor
:returns: A tuple containing the temperature in °C, the pressure in hPa, and the altitude in m upon successful reading,
otherwise -1 repeated three times
"""
try:
# Read sensor values
bmp_temp = bmp.temperature
bmp_pressure = bmp.pressure
bmp_altitude = bmp.altitude
print(f'Temperature: {bmp_temp:.2f} °C, '
f'Pressure: {bmp_pressure:.2f} hPa, '
f'Altitude: {bmp_altitude:.2f} m')
return bmp_temp, bmp_pressure, bmp_altitude
except Exception as e:
print(f"Error reading BMP390 sensor: {e}")
return -1, -1, -1
