Rust BME280 Crate
====





A Rust crate to query temperature, pressure and humidity from sensor [BME280]
<https://gitlab.com/claudiomattera/bme280-rs/>
[BME280]: https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/
This crate supports both [`embedded-hal`][embedded-hal] and [`embedded-hal-async`][embedded-hal-async].
[embedded-hal]: https://crates.io/crates/embedded-hal
[embedded-hal-async]: https://crates.io/crates/embedded-hal-async
See the [changelog](./CHANGELOG.md) for this project.
Usage
----
Add the dependency to `Cargo.toml`.
~~~~toml
[dependencies.bme280-rs]
version = "0.4.1"
~~~~
Optionally enable the desired features.
| `blocking` (default) | Enable the blocking sensor `Bme280` |
| `async` (default) | Enable the async sensor `AsyncBme280` |
| `uom` | Use `uom` for measurement types |
| `log` | Use `log` for logging |
| `defmt` | Use `defmt` for logging |
A `Bme280` structure can be created from an I²C interface and a delay function.
The initial sampling configuration disables all measurements, so it is necessary to reconfigure the chip with the desired settings before read samples.
~~~~rust
use bme280_rs::{Bme280, Configuration, Oversampling, SensorMode};
let i2c = ...
let delay = ...
let mut bme280 = Bme280::new(i2c, delay);
bme280.init()?;
bme280.set_sampling_configuration(
Configuration::default()
.with_temperature_oversampling(Oversampling::Oversample1)
.with_pressure_oversampling(Oversampling::Oversample1)
.with_humidity_oversampling(Oversampling::Oversample1)
.with_sensor_mode(SensorMode::Normal)
)?;
delay.delay_ms(10);
if let Some(temperature) = bme280.read_temperature()? {
println!("Temperature: {} C", temperature);
} else {
println!("Temperature reading was disabled");
}
~~~~
An `AsyncBme280` structure can be used with asynchronous HALs.
Its API is completely identical to `Bme280`, just with `.await` at the end of function calls.
~~~~rust
use bme280_rs::{AsyncBme280, Configuration, Oversampling, SensorMode};
let i2c = ...
let delay = ...
let mut bme280 = AsyncBme280::new(i2c, delay);
bme280.init()?;
bme280.set_sampling_configuration(
Configuration::default()
.with_temperature_oversampling(Oversampling::Oversample1)
.with_pressure_oversampling(Oversampling::Oversample1)
.with_humidity_oversampling(Oversampling::Oversample1)
.with_sensor_mode(SensorMode::Normal)
).await?;
delay.delay_ms(10).await;
if let Some(temperature) = bme280.read_temperature().await? {
println!("Temperature: {} C", temperature);
} else {
println!("Temperature reading was disabled");
}
~~~~
Examples
----
The examples in directory [`examples`](./examples) show how to use this crate with an [Adafruit FT232H] board.
Connect the board to the sensor as shown (yes, D1 and D2 must be shorted for I²C to work).
| 3v | VIN |
| Gnd | GND |
| D0 | SCL |
| D1 and D2 | SDA |

And run the examples:
~~~~shell
just run-example single
~~~~
[Adafruit FT232H]: https://www.adafruit.com/product/2264
Unit of Measurements
----
By default, this crate uses `f32` values for all the measurements temperature, pressure and humidity.
When instead enabling the Cargo feature `uom`, it uses quantities from crate [uom].
Temperature measurements have type `uom::si::f32::ThermodynamicTemperature`, pressure measurements have type `uom::si::f32::Pressure`, and humidity measurements have type `uom::si::f32::Ratio`.
[uom]: https://crates.io/crates/uom
Logging
----
When enabling Cargo feature `log`, internal logging uses the crate [log].
When enabling feature `defmt`, internal logging uses the crate [defmt].
When disabling both features, internal logging is disabled.
When enabling both features, internal logging uses both crates (this is probably undesired, but it makes features additive).
[log]: https://crates.io/crates/log
[defmt]: https://crates.io/crates/defmt
License
----
Copyright Claudio Mattera 2022-2026
You are free to copy, modify, and distribute this application with attribution under the terms of either
* Apache License, Version 2.0
(file [`LICENSE-APACHE-2.0.txt`](./LICENSE-APACHE-2.0.txt) or <https://opensource.org/licenses/Apache-2.0>)
* MIT license
(file [`LICENSE-MIT.txt`](./LICENSE-MIT.txt) or <https://opensource.org/licenses/MIT>)
at your option.
This project is entirely original work, and it is not affiliated with nor endorsed in any way by Bosch Sensortec.