Skip to content

Commit c709aca

Browse files
NandoBongersNando Bongers
andauthored
Added this i2c example. It reads data from the Bosch BME680 sensor using i2c. (#41)
Co-authored-by: Nando Bongers <[email protected]>
1 parent acd3247 commit c709aca

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cfg-if = "0.1.10"
6161
rtt-target = { version = "0.3.0", features = ["cortex-m"] }
6262
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }
6363
mpu6050 = "0.1.4"
64+
bme680 = "0.6.0"
6465

6566
#TODO: Separate feature sets
6667
[features]

examples/i2c-bme680.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//This example reads data from the Bosch BME680 (gas, humidity, temperature, pressure) sensor using I2C.
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
use bme680::*;
7+
use core::time::Duration;
8+
use embedded_hal::blocking::delay::DelayMs;
9+
use hal::delay::DelayFromCountDownTimer;
10+
use hal::i2c::Config;
11+
use hal::prelude::*;
12+
use hal::stm32;
13+
use hal::timer::Timer;
14+
use stm32g4xx_hal as hal;
15+
16+
use cortex_m_rt::entry;
17+
use log::info;
18+
19+
#[macro_use]
20+
mod utils;
21+
22+
#[entry]
23+
fn main() -> ! {
24+
utils::logger::init();
25+
26+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
27+
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
28+
29+
let mut rcc = dp.RCC.constrain();
30+
let gpiob = dp.GPIOB.split(&mut rcc);
31+
32+
let sda = gpiob.pb9.into_alternate_open_drain();
33+
let scl = gpiob.pb8.into_alternate_open_drain();
34+
35+
let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.khz()), &mut rcc);
36+
37+
let mut delayer = cp.SYST.delay(&rcc.clocks);
38+
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
39+
let mut delay = DelayFromCountDownTimer::new(timer2.start_count_down(100.ms()));
40+
41+
let mut dev =
42+
Bme680::init(i2c, &mut delayer, I2CAddress::Secondary).expect("Init function failed");
43+
44+
let settings = SettingsBuilder::new()
45+
.with_humidity_oversampling(OversamplingSetting::OS2x)
46+
.with_pressure_oversampling(OversamplingSetting::OS4x)
47+
.with_temperature_oversampling(OversamplingSetting::OS8x)
48+
.with_temperature_filter(IIRFilterSize::Size3)
49+
.with_gas_measurement(Duration::from_millis(1500), 320, 25)
50+
.with_temperature_offset(-2.2)
51+
.with_run_gas(true)
52+
.build();
53+
54+
let profile_dur = dev.get_profile_dur(&settings.0).unwrap();
55+
dev.set_sensor_settings(&mut delayer, settings).unwrap();
56+
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
57+
.unwrap();
58+
let sensor_settings = dev.get_sensor_settings(settings.1);
59+
60+
loop {
61+
delay.delay_ms(500u32);
62+
let power_mode = dev.get_sensor_mode();
63+
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
64+
.unwrap();
65+
let (data, _state) = dev.get_sensor_data(&mut delayer).unwrap();
66+
info!("Temperature {}°C", data.temperature_celsius());
67+
info!("Pressure {}hPa", data.pressure_hpa());
68+
info!("Humidity {}%", data.humidity_percent());
69+
info!("Gas Resistence {}Ω", data.gas_resistance_ohm());
70+
}
71+
}

0 commit comments

Comments
 (0)