Skip to content

Commit 8cddc22

Browse files
committed
docs(bme280): Add README, examples, and hardware test scenarios.
1 parent 2af3fd9 commit 8cddc22

5 files changed

Lines changed: 414 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This repository contains all the drivers for the main components of the [STeaMi]
2626
| ISM330DL | [`ism330dl`](lib/ism330dl/README.md) | `0x6B` | 6-axis IMU (accel + gyro) |
2727
| LIS2MDL | [`lis2mdl`](lib/lis2mdl/README.md) | `0x1E` | 3-axis magnetometer |
2828
| IM34DT05 | `im34dt05` *(not yet implemented)* | — (PDM) | Digital microphone |
29-
| BME280 | `bme280` *(not yet implemented)* | `0x76` | Pressure + humidity + temperature |
29+
| BME280 | [`bme280`](lib/bme280/README.md) | `0x76` | Pressure + humidity + temperature |
3030
| GC9A01 | `gc9a01` *(not yet implemented)* | — (SPI) | Round color LCD display |
3131
| STeaMi Config | [`steami_config`](lib/steami_config/README.md) || Persistent board configuration |
3232

lib/bme280/README.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# BME280 MicroPython Driver
2+
3+
MicroPython driver for the **Bosch BME280** combined pressure, humidity, and temperature sensor.
4+
5+
This driver provides a simple API to read **pressure**, **humidity**, and **temperature** over **I2C**.
6+
7+
The BME280 is a high-precision environmental sensor suitable for applications such as:
8+
9+
* weather monitoring
10+
* indoor air quality
11+
* altimetry support
12+
* environmental sensing
13+
14+
---
15+
16+
# Features
17+
18+
* I2C communication
19+
* device identification
20+
* pressure measurement (hPa)
21+
* temperature measurement (C)
22+
* relative humidity measurement (%RH)
23+
* one-shot acquisition (forced mode)
24+
* continuous measurement mode (normal mode)
25+
* configurable oversampling (temperature, pressure, humidity)
26+
* configurable IIR filter
27+
* configurable standby time
28+
* data-ready status helpers
29+
* sleep mode (power off)
30+
* soft reset and full reset with recalibration
31+
32+
---
33+
34+
# Sensor Overview
35+
36+
| Feature | Value |
37+
| ---------------------- | ----------------------- |
38+
| Pressure range | 300 hPa - 1100 hPa |
39+
| Pressure resolution | 0.18 Pa (20-bit ADC) |
40+
| Temperature range | -40 C to +85 C |
41+
| Temperature resolution | 0.01 C |
42+
| Humidity range | 0 - 100 %RH |
43+
| Humidity resolution | 0.008 %RH (16-bit ADC) |
44+
| Interface | I2C / SPI |
45+
| Chip ID | 0x60 |
46+
47+
---
48+
49+
# I2C Address
50+
51+
The sensor can use two I2C addresses depending on the **SDO pin**:
52+
53+
| SDO | Address |
54+
| ------ | ------- |
55+
| GND | `0x76` |
56+
| VDDIO | `0x77` |
57+
58+
The default address used by the driver is **0x76**.
59+
60+
---
61+
62+
# Basic Usage
63+
64+
```python
65+
from machine import I2C
66+
from time import sleep
67+
from bme280 import BME280
68+
69+
i2c = I2C(1)
70+
71+
sensor = BME280(i2c)
72+
73+
while True:
74+
temperature, pressure, humidity = sensor.read()
75+
76+
print("T:", temperature, "C")
77+
print("P:", pressure, "hPa")
78+
print("H:", humidity, "%RH")
79+
print()
80+
81+
sleep(1)
82+
```
83+
84+
---
85+
86+
# API Reference
87+
88+
## Initialization
89+
90+
```python
91+
sensor = BME280(i2c)
92+
```
93+
94+
Optional custom address:
95+
96+
```python
97+
sensor = BME280(i2c, address=0x77)
98+
```
99+
100+
The constructor verifies the chip ID, waits for NVM calibration data to be ready, reads factory trimming parameters, and applies a default configuration (1x oversampling, sleep mode).
101+
102+
---
103+
104+
## Measurements
105+
106+
### Read all channels
107+
108+
```python
109+
temperature, pressure, humidity = sensor.read()
110+
```
111+
112+
Returns a tuple of `(temperature_c, pressure_hpa, humidity_rh)`.
113+
114+
---
115+
116+
### Temperature
117+
118+
```python
119+
sensor.temperature()
120+
```
121+
122+
Returns the temperature in **degrees Celsius**.
123+
124+
---
125+
126+
### Pressure
127+
128+
```python
129+
sensor.pressure_hpa()
130+
```
131+
132+
Returns the pressure in **hPa**.
133+
134+
---
135+
136+
### Humidity
137+
138+
```python
139+
sensor.humidity()
140+
```
141+
142+
Returns the relative humidity in **%RH**.
143+
144+
---
145+
146+
### One-shot measurement
147+
148+
```python
149+
temperature, pressure, humidity = sensor.read_one_shot()
150+
```
151+
152+
Triggers a forced measurement, waits for completion, and returns all three channels.
153+
154+
---
155+
156+
### Trigger forced measurement
157+
158+
```python
159+
sensor.trigger_one_shot()
160+
```
161+
162+
Triggers a single forced measurement. Poll `data_ready()` for completion, then read values with `temperature()`, `pressure_hpa()`, `humidity()`, or `read()`.
163+
164+
---
165+
166+
## Data-Ready Status
167+
168+
```python
169+
sensor.data_ready() # True when all channels are ready
170+
sensor.temperature_ready() # True when temperature is ready
171+
sensor.pressure_ready() # True when pressure is ready
172+
sensor.humidity_ready() # True when humidity is ready
173+
```
174+
175+
---
176+
177+
## Configuration
178+
179+
### Oversampling
180+
181+
```python
182+
from bme280.const import OSRS_X2, OSRS_X4, OSRS_X16
183+
184+
sensor.set_oversampling(temperature=OSRS_X2, pressure=OSRS_X16, humidity=OSRS_X4)
185+
```
186+
187+
Available constants: `OSRS_SKIP`, `OSRS_X1`, `OSRS_X2`, `OSRS_X4`, `OSRS_X8`, `OSRS_X16`.
188+
189+
Pass `None` to keep the current setting for a channel.
190+
191+
---
192+
193+
### IIR Filter
194+
195+
```python
196+
from bme280.const import FILTER_4
197+
198+
sensor.set_iir_filter(FILTER_4)
199+
```
200+
201+
Available constants: `FILTER_OFF`, `FILTER_2`, `FILTER_4`, `FILTER_8`, `FILTER_16`.
202+
203+
---
204+
205+
### Standby Time
206+
207+
```python
208+
from bme280.const import STANDBY_500_MS
209+
210+
sensor.set_standby(STANDBY_500_MS)
211+
```
212+
213+
Available constants: `STANDBY_0_5_MS`, `STANDBY_62_5_MS`, `STANDBY_125_MS`, `STANDBY_250_MS`, `STANDBY_500_MS`, `STANDBY_1000_MS`.
214+
215+
---
216+
217+
## Modes
218+
219+
### Sleep mode
220+
221+
```python
222+
sensor.power_off() # enter sleep mode, stop measurements
223+
sensor.power_on() # enter normal mode, continuous measurements
224+
```
225+
226+
---
227+
228+
### Continuous mode
229+
230+
```python
231+
from bme280.const import STANDBY_125_MS
232+
233+
sensor.set_continuous(standby=STANDBY_125_MS)
234+
```
235+
236+
Enters normal mode with the specified standby time between measurements. If `standby` is `None`, the current standby setting is kept.
237+
238+
---
239+
240+
## Device Management
241+
242+
### Device ID
243+
244+
```python
245+
sensor.device_id() # returns 0x60
246+
```
247+
248+
---
249+
250+
### Soft Reset
251+
252+
```python
253+
sensor.soft_reset()
254+
```
255+
256+
Sends the reset command and waits for NVM reload.
257+
258+
---
259+
260+
### Full Reset
261+
262+
```python
263+
sensor.reset()
264+
```
265+
266+
Performs a soft reset, re-reads calibration data, and re-applies default configuration.
267+
268+
---
269+
270+
# Examples
271+
272+
| Example | Description |
273+
| -------------------- | -------------------------------------------------- |
274+
| `basic_reader.py` | Read temperature, pressure, and humidity |
275+
| `weather_station.py` | Continuous logging with altitude computation |
276+
277+
---
278+
279+
# References
280+
281+
* [BME280 Datasheet](https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from time import sleep
2+
3+
from bme280 import BME280
4+
from machine import I2C
5+
6+
# Update the I2C bus number and pins to match your board
7+
i2c = I2C(1)
8+
9+
# Create the sensor object
10+
sensor = BME280(i2c)
11+
12+
print("BME280 found")
13+
print("Device ID:", hex(sensor.device_id()))
14+
15+
for _ in range(10):
16+
temperature, pressure, humidity = sensor.read()
17+
18+
print(
19+
"T: {:.1f} C P: {:.1f} hPa H: {:.1f} %RH".format(
20+
temperature, pressure, humidity
21+
)
22+
)
23+
24+
sleep(1)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Continuous weather monitoring with altitude estimation.
2+
3+
Reads temperature, pressure, and humidity every 5 seconds in normal mode,
4+
computes approximate altitude from pressure using the barometric formula,
5+
and logs each measurement to the DAPLink flash as CSV.
6+
"""
7+
8+
from time import sleep, sleep_ms
9+
10+
from bme280 import BME280
11+
from bme280.const import FILTER_16, OSRS_X2, OSRS_X16, STANDBY_1000_MS
12+
from daplink_bridge import DaplinkBridge
13+
from daplink_flash import DaplinkFlash
14+
from machine import I2C
15+
16+
# Sea-level reference pressure in hPa (adjust to local conditions)
17+
SEA_LEVEL_PRESSURE = 1013.25
18+
19+
i2c = I2C(1)
20+
21+
sensor = BME280(i2c)
22+
bridge = DaplinkBridge(i2c)
23+
flash = DaplinkFlash(bridge)
24+
25+
# Configure for weather monitoring: high pressure resolution, moderate temp/hum
26+
sensor.set_oversampling(temperature=OSRS_X2, pressure=OSRS_X16, humidity=OSRS_X2)
27+
sensor.set_iir_filter(FILTER_16)
28+
sensor.set_continuous(standby=STANDBY_1000_MS)
29+
30+
# Prepare flash logging
31+
flash.set_filename("WEATHER", "CSV")
32+
flash.clear_flash()
33+
sleep_ms(500)
34+
print("Flash erased.")
35+
36+
flash.write_line("temperature;pressure;humidity;altitude")
37+
38+
39+
def altitude_m(pressure_hpa):
40+
"""Estimate altitude in meters from pressure using the barometric formula."""
41+
return 44330.0 * (1.0 - (pressure_hpa / SEA_LEVEL_PRESSURE) ** 0.1903)
42+
43+
44+
while True:
45+
temperature, pressure, humidity = sensor.read()
46+
alt = altitude_m(pressure)
47+
48+
print(
49+
"T: {:.1f} C P: {:.1f} hPa H: {:.1f} %RH Alt: {:.0f} m".format(
50+
temperature, pressure, humidity, alt
51+
)
52+
)
53+
flash.write_line(
54+
"{:.1f};{:.1f};{:.1f};{:.0f}".format(temperature, pressure, humidity, alt)
55+
)
56+
57+
sleep(5)

0 commit comments

Comments
 (0)