Author: Siratul Islam
Version: 1.0.0
License: MIT License
A comprehensive C library for interfacing with the Bosch BME280 temperature, pressure, and humidity sensor using STM32 HAL I2C.
- Complete sensor support: Temperature, pressure, and humidity measurements
- STM32 HAL integration: Built specifically for STM32 microcontrollers using HAL libraries
- Configurable settings: Customizable oversampling, filtering, and operating modes
- Calibration handling: Automatic reading and application of sensor calibration data
- Error handling: Robust I2C communication with error checking
- Compensation algorithms: Uses official Bosch compensation formulas from datasheet
- Datasheet compliance: Implementation based on official BME280 datasheet specifications
- STM32 microcontroller with I2C peripheral
- BME280 sensor module
- Pull-up resistors on I2C lines (typically 4.7kΩ)
| BME280 Pin | STM32 Pin | Description |
|---|---|---|
| VCC | 3.3V | Power supply |
| GND | GND | Ground |
| SDA | I2C SDA | I2C Data line |
| SCL | I2C SCL | I2C Clock line |
- Copy
bme280.handbme280.cto your STM32 project - Include the header file in your main application:
#include "bme280.h"
- Ensure your project includes STM32 HAL I2C libraries
#include "bme280.h"
// Assuming hi2c1 is your I2C handle
extern I2C_HandleTypeDef hi2c1;
int main(void) {
// Initialize BME280 with default settings
int result = bme280_init(&hi2c1,
BME280_OVERSAMPLE_X1, // humidity
BME280_OVERSAMPLE_X1, // temperature
BME280_OVERSAMPLE_X1, // pressure
BME280_NORMAL_MODE, // operating mode
BME280_STANDBY_1000MS, // standby time
BME280_FILTER_OFF); // filter coefficient
if (result != 0) {
// Initialization failed
Error_Handler();
}
while (1) {
// Read sensor values
float temperature = bme280_get_temperature(); // °C
float pressure = bme280_get_pressure(); // Pa
float humidity = bme280_get_humidity(); // %RH
// Convert pressure to more common units
float pressure_hPa = pressure / 100.0f; // hPa
float pressure_mmHg = pressure / 133.322f; // mmHg
HAL_Delay(2000); // Wait 2 seconds between readings
}
}// High precision configuration
int result = bme280_init(&hi2c1,
BME280_OVERSAMPLE_X16, // humidity oversampling
BME280_OVERSAMPLE_X2, // temperature oversampling
BME280_OVERSAMPLE_X16, // pressure oversampling
BME280_NORMAL_MODE, // continuous measurement
BME280_STANDBY_500MS, // 500ms between measurements
BME280_FILTER_COEFF_16); // IIR filter coefficient 16int bme280_init(I2C_HandleTypeDef *hi2c, uint8_t humidity_oversampling, uint8_t temp_oversampling, uint8_t pressure_oversampling, uint8_t sensor_mode, uint8_t standby_time, uint8_t filter_coeff)
Initializes the BME280 sensor with specified configuration.
Parameters:
hi2c: Pointer to I2C handlehumidity_oversampling: Humidity oversampling setting (see constants below)temp_oversampling: Temperature oversampling settingpressure_oversampling: Pressure oversampling settingsensor_mode: Operating mode (sleep, forced, normal)standby_time: Standby time between measurements in normal modefilter_coeff: IIR filter coefficient
Returns: 0 on success, -1 on failure
Returns temperature in degrees Celsius.
Returns pressure in Pascals (Pa).
Returns relative humidity as percentage (%RH).
BME280_OVERSAMPLE_SKIP // Skip measurement
BME280_OVERSAMPLE_X1 // 1x oversampling
BME280_OVERSAMPLE_X2 // 2x oversampling
BME280_OVERSAMPLE_X4 // 4x oversampling
BME280_OVERSAMPLE_X8 // 8x oversampling
BME280_OVERSAMPLE_X16 // 16x oversamplingBME280_SLEEP_MODE // Sleep mode (lowest power)
BME280_FORCED_MODE // Forced mode (single measurement)
BME280_NORMAL_MODE // Normal mode (continuous measurement)BME280_STANDBY_0_5MS // 0.5ms
BME280_STANDBY_10MS // 10ms
BME280_STANDBY_20MS // 20ms
BME280_STANDBY_62_5MS // 62.5ms
BME280_STANDBY_125MS // 125ms
BME280_STANDBY_250MS // 250ms
BME280_STANDBY_500MS // 500ms
BME280_STANDBY_1000MS // 1000msBME280_FILTER_OFF // Filter off
BME280_FILTER_COEFF_2 // Filter coefficient 2
BME280_FILTER_COEFF_4 // Filter coefficient 4
BME280_FILTER_COEFF_8 // Filter coefficient 8
BME280_FILTER_COEFF_16 // Filter coefficient 16float pressure_Pa = bme280_get_pressure(); // Pascals
float pressure_hPa = pressure_Pa / 100.0f; // hectoPascals (millibar)
float pressure_kPa = pressure_Pa / 1000.0f; // kiloPascals
float pressure_mmHg = pressure_Pa / 133.322f; // mmHg
float pressure_inHg = pressure_Pa / 3386.389f; // inches of mercury
float pressure_psi = pressure_Pa / 6894.757f; // PSIfloat temp_C = bme280_get_temperature(); // Celsius
float temp_F = (temp_C * 9.0f / 5.0f) + 32.0f; // Fahrenheit
float temp_K = temp_C + 273.15f; // Kelvin-
Initialization fails (-1 return)
- Check I2C wiring and pull-up resistors
- Verify BME280 I2C address (default: 0x76)
- Ensure proper power supply (3.3V)
-
Incorrect readings
- Allow sensor to stabilize after power-on
- Check for proper calibration data reading
- Verify oversampling settings match your requirements
-
I2C communication errors
- Check I2C clock speed (typically 100kHz or 400kHz)
- Verify SDA/SCL connections
- Ensure adequate pull-up resistors (4.7kΩ recommended)
- Use an oscilloscope or logic analyzer to verify I2C communication
- Check the sensor ID register (should read 0x60 for BME280)
- Monitor I2C bus for proper start/stop conditions
- Power consumption: Use sleep or forced mode for battery applications
- Measurement time: Higher oversampling increases accuracy but also measurement time
- Noise reduction: Use IIR filtering for applications with vibration or rapid changes
- Update rate: Balance between data freshness and power consumption
This library is implemented according to the official Bosch BME280 Environmental Sensor Datasheet:
- Datasheet: BME280 Combined humidity and pressure sensor
- Manufacturer: Bosch Sensortec
The compensation algorithms and register configurations are directly sourced from the official datasheet to ensure accuracy and compliance with manufacturer specifications.
This library is provided as-is for educational and commercial use. Based on official Bosch BME280 compensation algorithms.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly on hardware
- Submit a pull request
- Initial release
- Support for temperature, pressure, and humidity measurements
- STM32 HAL I2C integration
- Configurable oversampling and filtering