Skip to content

Commit 2e832c8

Browse files
authored
Merge pull request #2 from SolderedElectronics/dev
Add Bosch BMP388 sensor MicroPython module
2 parents b120cda + 12d4ebc commit 2e832c8

8 files changed

Lines changed: 1067 additions & 0 deletions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# FILE: bmp388-allValues.py
2+
# AUTHOR: Josip Šimun Kuči @ Soldered
3+
# BRIEF: An example for the BMP388 sensor that reads temperature, pressure
4+
# and calculates the altitude using the pressure measurement
5+
# WORKS WITH: Pressure & Temperature sensor BMP388 Breakout: www.solde.red/333316
6+
# LAST UPDATED: 2025-01-XX
7+
from machine import Pin, I2C
8+
from bmp388 import BMP388
9+
import time
10+
11+
# If you aren't using the Qwiic connector, manually enter your I2C pins
12+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
13+
# bmp388 = BMP388(i2c)
14+
15+
# Initialize sensor over Qwiic
16+
bmp388 = BMP388()
17+
18+
# Set sea level pressure for accurate altitude readings (adjust to your local value)
19+
bmp388.setSeaLevelPressure(1013.25)
20+
21+
# Start continuous measurement in normal mode
22+
bmp388.startNormalConversion()
23+
24+
# Wait a bit for the first measurement to be ready
25+
# (depends on oversampling settings, give it extra time)
26+
time.sleep(0.5)
27+
28+
# Infinite loop
29+
while 1:
30+
# Variables for storing measurement data (matching Arduino example)
31+
temperature, pressure, altitude = bmp388.getMeasurements()
32+
33+
# Check if the data is ready (matching Arduino: if (bmp388.getMeasurements(...)))
34+
if temperature is not None:
35+
# Print the results (matching Arduino format)
36+
print("{:.2f}*C {:.2f}hPa {:.2f}m".format(temperature, pressure, altitude))
37+
# Note: Arduino example doesn't print "waiting" message, it just silently waits
38+
39+
# Wait a little bit (matching Arduino: delay(250))
40+
time.sleep(0.25)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# FILE: bmp388-forcedMeasurement.py
2+
# AUTHOR: Josip Šimun Kuči @ Soldered
3+
# BRIEF: Forced measurement example for the BMP388
4+
# WORKS WITH: Pressure & Temperature sensor BMP388 Breakout: www.solde.red/333316
5+
# LAST UPDATED: 2025-01-15
6+
7+
from machine import Pin, I2C
8+
from bmp388 import BMP388
9+
import time
10+
11+
# If you aren't using the Qwiic connector, manually enter your I2C pins
12+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
13+
# bmp388 = BMP388(i2c)
14+
15+
# Initialize sensor over Qwiic
16+
bmp388 = BMP388()
17+
18+
# Set sea level pressure for accurate altitude readings
19+
bmp388.setSeaLevelPressure(1025.0)
20+
21+
while True:
22+
# Request a new measurement
23+
bmp388.startForcedConversion()
24+
25+
# Wait until data is ready
26+
while True:
27+
temperature, pressure, altitude = bmp388.getMeasurements()
28+
if temperature is not None:
29+
print("{:.2f}*C {:.2f}hPa {:.2f}m".format(temperature, pressure, altitude))
30+
break
31+
time.sleep(0.02)
32+
33+
# Wait a little bit before next measurement
34+
time.sleep(1)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# FILE: bmp388-forcedModeWithInterrupt.py
2+
# AUTHOR: Josip Šimun Kuči @ Soldered
3+
# BRIEF: Forced mode example using data-ready polling
4+
# WORKS WITH: Pressure & Temperature sensor BMP388 Breakout: www.solde.red/333316
5+
# LAST UPDATED: 2025-01-15
6+
7+
from machine import Pin, I2C
8+
from bmp388 import BMP388
9+
import time
10+
11+
dataReady = False
12+
13+
14+
def interruptHandler(pin):
15+
global dataReady
16+
dataReady = True
17+
18+
# If you aren't using the Qwiic connector, manually enter your I2C pins
19+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
20+
# bmp388 = BMP388(i2c)
21+
22+
# Initialize sensor over Qwiic
23+
bmp388 = BMP388()
24+
25+
# Set sea level pressure for accurate altitude readings
26+
bmp388.setSeaLevelPressure(1025.0)
27+
28+
# Enable data-ready interrupt
29+
bmp388.enableInterrupt()
30+
31+
# Connect sensor INT pin to GPIO2 (adjust for your board)
32+
intPin = Pin(2, Pin.IN, Pin.PULL_UP)
33+
intPin.irq(trigger=Pin.IRQ_RISING, handler=interruptHandler)
34+
35+
while True:
36+
# Request a new measurement
37+
bmp388.startForcedConversion()
38+
39+
# Wait for interrupt flag
40+
while not dataReady:
41+
time.sleep(0.005)
42+
43+
temperature, pressure, altitude = bmp388.getMeasurements()
44+
if temperature is not None:
45+
print("{:.2f}*C {:.2f}hPa {:.2f}m".format(temperature, pressure, altitude))
46+
47+
dataReady = False
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# FILE: bmp388-normalModeWithFifo.py
2+
# AUTHOR: Josip Šimun Kuči @ Soldered
3+
# BRIEF: Normal mode example that collects multiple samples
4+
# WORKS WITH: Pressure & Temperature sensor BMP388 Breakout: www.solde.red/333316
5+
# LAST UPDATED: 2025-01-15
6+
7+
from machine import Pin, I2C
8+
from bmp388 import BMP388
9+
from bmp388_constants import TIME_STANDBY_1280MS, FIFO_DATA_READY, FIFO_CONFIG_ERROR
10+
import time
11+
12+
NO_OF_MEASUREMENTS = 10
13+
14+
# If you aren't using the Qwiic connector, manually enter your I2C pins
15+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
16+
# bmp388 = BMP388(i2c)
17+
18+
# Initialize sensor over Qwiic
19+
bmp388 = BMP388()
20+
21+
# Set sea level pressure for accurate altitude readings
22+
bmp388.setSeaLevelPressure(1025.0)
23+
24+
# Set standby time to roughly 1.3 seconds
25+
bmp388.setTimeStandby(TIME_STANDBY_1280MS)
26+
27+
# Enable FIFO and set watermark
28+
bmp388.enableFIFO()
29+
bmp388.setFIFONoOfMeasurements(NO_OF_MEASUREMENTS)
30+
31+
# Start continuous measurement in normal mode
32+
bmp388.startNormalConversion()
33+
34+
print("Please wait for 13 seconds...")
35+
36+
while True:
37+
status, temperatures, pressures, altitudes, sensorTime = bmp388.getFIFOData()
38+
39+
if status == FIFO_DATA_READY:
40+
for i in range(len(temperatures)):
41+
altitude = altitudes[i] if i < len(altitudes) else 0.0
42+
print("{}: {:.2f}*C {:.2f}hPa {:.2f}m".format(
43+
i + 1, temperatures[i], pressures[i], altitude
44+
))
45+
46+
print("Sensor Time: {} ms".format(sensorTime))
47+
print()
48+
print("Please wait for 13 seconds...")
49+
elif status == FIFO_CONFIG_ERROR:
50+
print("FIFO configuration error.")
51+
52+
time.sleep(0.05)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# FILE: bmp388-normalModeWithInterrupt.py
2+
# AUTHOR: Josip Šimun Kuči @ Soldered
3+
# BRIEF: Normal mode example using data-ready polling
4+
# WORKS WITH: Pressure & Temperature sensor BMP388 Breakout: www.solde.red/333316
5+
# LAST UPDATED: 2025-01-15
6+
7+
from machine import Pin, I2C
8+
from bmp388 import BMP388
9+
from bmp388_constants import TIME_STANDBY_1280MS
10+
import time
11+
12+
dataReady = False
13+
14+
15+
def interruptHandler(pin):
16+
global dataReady
17+
dataReady = True
18+
19+
# If you aren't using the Qwiic connector, manually enter your I2C pins
20+
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
21+
# bmp388 = BMP388(i2c)
22+
23+
# Initialize sensor over Qwiic
24+
bmp388 = BMP388()
25+
26+
# Set sea level pressure for accurate altitude readings
27+
bmp388.setSeaLevelPressure(1025.0)
28+
29+
# Set standby time to roughly 1.3 seconds
30+
bmp388.setTimeStandby(TIME_STANDBY_1280MS)
31+
32+
# Enable data-ready interrupt
33+
bmp388.enableInterrupt()
34+
35+
# Connect sensor INT pin to GPIO2 (adjust for your board)
36+
intPin = Pin(2, Pin.IN, Pin.PULL_UP)
37+
intPin.irq(trigger=Pin.IRQ_RISING, handler=interruptHandler)
38+
39+
# Start continuous measurement in normal mode
40+
bmp388.startNormalConversion()
41+
42+
while True:
43+
if dataReady:
44+
temperature, pressure, altitude = bmp388.getMeasurements()
45+
if temperature is not None:
46+
print("{:.2f}*C {:.2f}hPa {:.2f}m".format(temperature, pressure, altitude))
47+
dataReady = False
48+
time.sleep(0.01)

0 commit comments

Comments
 (0)