STM32 driver code to read pressure and temperature data from the BMP180 sensor. Tested on STM32F1
BMP180 is a low-cost highly accurate barometric pressure/temperature sensor. It offers a pressure data with 4 levels of accuracy you can choose from and a temperature data.
This driver gives full control of the sensor eg.(read temp(Celcius), read pressure(Pascals), read elevation(Meters). The rest of this documentation goes over the installation process and the usage of the driver with STM32.
- Copy
BMP180.hto./Core/IncandBMP180.cto./Core/Src. - In
BMP180.h, Change the#include "stm32f1xx_hal.h"to your STM32 model. (Disclaimer: Only tested on STM32F1) - Include
BMP180.hinmain.c.
- Create a
BMP180struct. - Run
BMP180_Init(). - Run anything else.
Example code is also available here
// Create struct
BMP180 bmp180;
// Run intialization function
BMP180_Init(&bmp180, &hi2c1);Simply call BMP180_ReadTemp() with the address to the BMP180 struct as its argument.
Then you can access temp_C as a member of the BMP180 struct.
while (1) {
// Read data
BMP180_ReadPressure(&bmp180, CTRL_REG_PRESSURE_OSS_1);
doSomething(bmp180->elevation_M);
HAL_Delay(250);
}Call BMP180_ReadPressure() with the address to the BMP180 struct as its first argument. For its second argument choose the accuracy of the result using CTRL_REG_PRESSURE_OSS_X.
Then you can access pressure_Pa and elevation_M as a member of the BMP180 struct.
while (1) {
// Read data
BMP180_ReadPressure(&bmp180, CTRL_REG_PRESSURE_OSS_0); // ultra low power
BMP180_ReadPressure(&bmp180, CTRL_REG_PRESSURE_OSS_1); // standard
BMP180_ReadPressure(&bmp180, CTRL_REG_PRESSURE_OSS_2); // high res
BMP180_ReadPressure(&bmp180, CTRL_REG_PRESSURE_OSS_3); // ultra high res
doSomething(bmp180->pressure_Pa);
doSomething(bmp180->elevation_M);
HAL_Delay(250);
}typedef struct {
// I2c handle
I2C_HandleTypeDef *hi2c;
// param
HAL_StatusTypeDef device_id_status;
int OSS;
int32_t B5;
// Raw data
uint32_t raw_pressure, raw_temperature;
// Processed data;
uint32_t pressure_Pa;
float temp_C;
float elevation_M;
// Calibration data struct
CALIBRATION_DATA calibration_data;
} BMP180;typedef struct {
int16_t AC1;
int16_t AC2;
int16_t AC3;
uint16_t AC4;
uint16_t AC5;
uint16_t AC6;
int16_t B1;
int16_t B2;
int16_t MB;
int16_t MC;
int16_t MD;
} CALIBRATION_DATA;This is used to store the calibration data of the BMP180 in the Initialization function. It can be accessed from BMP180->calibration_data.
typedef enum {
CTRL_REG_TEMPERATURE = 0x2E,
CTRL_REG_PRESSURE_OSS_0 = 0x34, // ultra low power
CTRL_REG_PRESSURE_OSS_1 = 0x74, // standard
CTRL_REG_PRESSURE_OSS_2 = 0xB4, // high res
CTRL_REG_PRESSURE_OSS_3 = 0xF4, // ultra high res
} CONTROL_REGISTER;Used to set the resolution of the pressure sensor. Use CTRL_REG_PRESSURE_OSS_X when calling BMP180_ReadPressure() or BMP180_ReadRawPressure().
uint8_t BMP180_Init(BMP180 *device, I2C_HandleTypeDef *hi2c);Initialization function. Takes in BMP180 struct and I2C_HandleTypeDef. Returns an error code.
ERROR CODES
1: OK
2: Failed to read device ID reg.
3: Device Id does not match.
4: Failed to read calibration data.
HAL_StatusTypeDef BMP180_ReadTemp(BMP180 *device);Reads the raw temperature and saves it to BMP180->raw_temperature.
Reads the temperature in Celcius and saves it to BMP180->temp_C.
HAL_StatusTypeDef BMP180_ReadPressure(BMP180 *device, CONTROL_REGISTER control_register);Reads the raw pressure and saves it to BMP180->raw_pressure.
Reads the pressure in Pascals and saves it to BMP180->pressure_Pa. Also saves elevation to BMP180->elevation_M in meters.
Due to the conversion of raw pressures to pascals requiring temperature data, the raw temperature and temperature in Celcius gets saved into the BMP180 struct again.
This means that if you need to both read temperature and pressure, you will be fine with just calling BMP180_ReadPressure() since it updates all temperature, pressure, and elevation.
HAL_StatusTypeDef BMP180_ReadRawTemp(BMP180 *device);Reads the raw temperature and saves it to BMP180->raw_temperature.
HAL_StatusTypeDef BMP180_ReadRawPressure(BMP180 *device, CONTROL_REGISTER control_register);Reads the raw pressure and saves it to BMP180->raw_pressure.
HAL_StatusTypeDef BMP180_Read_Calibration_Data(BMP180 *device);HAL_StatusTypeDef BMP180_SET_CTRL_MEAS(BMP180 *device, CONTROL_REGISTER control_register);HAL_StatusTypeDef BMP180_ReadReg(BMP180 *device, uint8_t reg_addr, uint8_t *data);Reads a single register.
HAL_StatusTypeDef BMP180_ReadRegs(BMP180 *device, uint8_t reg_addr, uint8_t *data, uint8_t size);HAL_StatusTypeDef BMP180_WriteReg(BMP180 *device, uint8_t reg_addr, uint8_t *data);If some of the function descriptions weren't too clear, please take a look at the comments in BMP180.h.
If there are any questoins, bug reports, or suggestions, please contact me at hyunwoo6@illinois.edu