Skip to content

heronet/stm32-bme280-hal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BME280 STM32 Library

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.

Features

  • 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

Hardware Requirements

  • STM32 microcontroller with I2C peripheral
  • BME280 sensor module
  • Pull-up resistors on I2C lines (typically 4.7kΩ)

Wiring

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

Installation

  1. Copy bme280.h and bme280.c to your STM32 project
  2. Include the header file in your main application:
    #include "bme280.h"
  3. Ensure your project includes STM32 HAL I2C libraries

Quick Start

Basic Initialization

#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
    }
}

Advanced Configuration

// 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 16

API Reference

Initialization Functions

int 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 handle
  • humidity_oversampling: Humidity oversampling setting (see constants below)
  • temp_oversampling: Temperature oversampling setting
  • pressure_oversampling: Pressure oversampling setting
  • sensor_mode: Operating mode (sleep, forced, normal)
  • standby_time: Standby time between measurements in normal mode
  • filter_coeff: IIR filter coefficient

Returns: 0 on success, -1 on failure

Data Reading Functions

float bme280_get_temperature(void)

Returns temperature in degrees Celsius.

float bme280_get_pressure(void)

Returns pressure in Pascals (Pa).

float bme280_get_humidity(void)

Returns relative humidity as percentage (%RH).

Configuration Constants

Oversampling Settings

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 oversampling

Operating Modes

BME280_SLEEP_MODE         // Sleep mode (lowest power)
BME280_FORCED_MODE        // Forced mode (single measurement)
BME280_NORMAL_MODE        // Normal mode (continuous measurement)

Standby Time (Normal Mode)

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     // 1000ms

IIR Filter Coefficients

BME280_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 16

Unit Conversions

Pressure

float 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;       // PSI

Temperature

float 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

Troubleshooting

Common Issues

  1. Initialization fails (-1 return)

    • Check I2C wiring and pull-up resistors
    • Verify BME280 I2C address (default: 0x76)
    • Ensure proper power supply (3.3V)
  2. Incorrect readings

    • Allow sensor to stabilize after power-on
    • Check for proper calibration data reading
    • Verify oversampling settings match your requirements
  3. I2C communication errors

    • Check I2C clock speed (typically 100kHz or 400kHz)
    • Verify SDA/SCL connections
    • Ensure adequate pull-up resistors (4.7kΩ recommended)

Debug Tips

  • 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

Performance Considerations

  • 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

Documentation and References

This library is implemented according to the official Bosch BME280 Environmental Sensor Datasheet:

The compensation algorithms and register configurations are directly sourced from the official datasheet to ensure accuracy and compliance with manufacturer specifications.

License

This library is provided as-is for educational and commercial use. Based on official Bosch BME280 compensation algorithms.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly on hardware
  5. Submit a pull request

Changelog

v1.0.0

  • Initial release
  • Support for temperature, pressure, and humidity measurements
  • STM32 HAL I2C integration
  • Configurable oversampling and filtering

About

BME280 library for STM32 HAL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published