bmp180: Driver for the BMP180 digital pressure sensor
The BMP180 is a digital barometric pressure and temperature sensor with a factory calibration in E2PROM and up to 19 bits of pressure resolution in its slowest mode.
Hardware
The sensor answers on 0x77; the address is fixed, so there can be only one
BMP180 per bus. Supply is 1.8–3.6 V, and the breakout boards usually add a
regulator, so check the markings before feeding one from 5 V. The driver sets
the port to 1 MHz, the fastest rate ESP-IDF supports; the chip itself would go
up to 3.4 MHz.
Two pins are enough: SCL and SDA with pull-ups, plus the supply.
The chip id lives in the version register and is checked on every
bmp180_init(): a different value returns ESP_ERR_NOT_FOUND, so a wrong
address or a dead bus is reported instead of producing plausible numbers.
Usage
#include <bmp180.h>
bmp180_dev_t dev = { 0 };
ESP_ERROR_CHECK(bmp180_init_desc(&dev, 0, CONFIG_EXAMPLE_I2C_MASTER_SDA,
CONFIG_EXAMPLE_I2C_MASTER_SCL));
ESP_ERROR_CHECK(bmp180_init(&dev));
float temperature;
uint32_t pressure;
ESP_ERROR_CHECK(bmp180_measure(&dev, &temperature, &pressure,
BMP180_MODE_STANDARD));
i2cdev_init() has to run before the first transfer, which happens inside
bmp180_init(); bmp180_init_desc() only fills the descriptor and creates
its mutex.
Measuring
bmp180_measure() takes the temperature first and the pressure second: the
pressure compensation needs the temperature of the same cycle, so both values
always belong together. Either output may be NULL, and a NULL pressure
skips its conversion entirely and returns sooner.
The oss argument trades resolution for time, as in table 3 of the
datasheet:
Mode |
Samples |
Pressure conversion |
|---|---|---|
|
1 |
4.5 ms |
|
2 |
7.5 ms |
|
4 |
13.5 ms |
|
8 |
25.5 ms |
The temperature conversion takes 4.5 ms in every mode. The driver does not
sleep for these times: it writes the command and polls the Sco bit until
the conversion reports completion, with a 10 ms margin over the datasheet
time. A conversion that does not finish returns ESP_ERR_TIMEOUT.
Temperature is returned in degrees Celsius, pressure in pascals. The
compensation follows the datasheet formulas, so the accuracy figures of the
datasheet apply: ±0.5 °C and ±1 hPa in the standard mode at room temperature.
Values that come out non-physical mean corrupted calibration data and are
reported as ESP_ERR_INVALID_RESPONSE instead of being returned: the driver
checks the temperature against the operating range of the chip (−40…+85 °C) and
rejects a pressure that is not positive.
bmp180_reset() sends the reset command and waits at least 2 ms (the delay is
quantised to the FreeRTOS tick); bmp180_init()
calls it before reading the identification and the eleven calibration
coefficients, so a sensor that stayed powered through an MCU restart starts
from a known state.
Limitations
The address is fixed at
0x77.Pressure is returned in pascals only; altitude needs the sea-level pressure of the moment and is left to the application.
A measurement is several I2C transactions, each with its own mutex, so another device on the same bus can interleave between them. The conversion itself is not interrupted.
bmp180_free_desc()deletes the mutex, it does not release the port; calli2cdev_done()when nothing else uses the bus.
Examples
default— measures every 500 ms in the standard mode and prints the values.
Support
For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Contributing
Please read CONTRIBUTING.md at esp-idf-lib/core.
Defines
-
BMP180_DEVICE_ADDRESS
I2C address.
Enums
-
enum bmp180_mode_t
Hardware accuracy mode.
See Table 3 of the datasheet
Values:
-
enumerator BMP180_MODE_ULTRA_LOW_POWER
1 sample, 4.5 ms
-
enumerator BMP180_MODE_STANDARD
2 samples, 7.5 ms
-
enumerator BMP180_MODE_HIGH_RESOLUTION
4 samples, 13.5 ms
-
enumerator BMP180_MODE_ULTRA_HIGH_RESOLUTION
8 samples, 25.5 ms
-
enumerator BMP180_MODE_ULTRA_LOW_POWER
Functions
-
esp_err_t bmp180_init_desc(bmp180_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
Initialize device descriptor.
- Parameters:
dev – Device descriptor
port – I2C port number
sda_gpio – GPIO pin number for SDA
scl_gpio – GPIO pin number for SCL
- Returns:
ESP_OKon successESP_ERR_INVALID_ARGifdevis NULLESP_ERR_NO_MEMif the descriptor mutex cannot be created
-
esp_err_t bmp180_free_desc(bmp180_dev_t *dev)
Free device descriptor.
- Parameters:
dev – Pointer to BMP180 device descriptor
- Returns:
ESP_OKon successESP_ERR_INVALID_ARGifdevis NULL
-
esp_err_t bmp180_reset(bmp180_dev_t *dev)
Reset the sensor.
Runs the power-on sequence: the control and the data registers return to their default values and an ongoing conversion is aborted. The calibration coefficients are stored in the E2PROM and survive the reset.
- Parameters:
dev – Pointer to BMP180 device descriptor
- Returns:
ESP_OKon successESP_ERR_INVALID_ARGifdevis NULLanother error code from the I2C layer
-
esp_err_t bmp180_init(bmp180_dev_t *dev)
Initialize device.
Calls bmp180_reset, verifies the chip ID and reads the calibration coefficients. The reset puts the sensor into the known state after an MCU restart while the sensor stays powered.
- Parameters:
dev – Pointer to BMP180 device descriptor
- Returns:
ESP_OKon successESP_ERR_INVALID_ARGifdevis NULLESP_ERR_NOT_FOUNDif the chip ID is not the BMP180 oneanother error code from the I2C layer
-
esp_err_t bmp180_measure(bmp180_dev_t *dev, float *temperature, uint32_t *pressure, bmp180_mode_t oss)
Measure temperature and pressure.
The temperature is measured before the pressure, the pressure measurement needs the temperature one. At least one of the output parameters has to be given; when the pressure is not requested it is not measured, which saves the conversion time of its mode.
- Parameters:
dev – Pointer to BMP180 device descriptor
temperature – [out] Temperature in degrees Celsius, may be NULL
pressure – [out] Pressure in Pa, may be NULL
oss – Measurement mode, one of bmp180_mode_t
- Returns:
ESP_OKon successESP_ERR_INVALID_ARGifdevis NULL, both outputs are NULL orossis out of rangeESP_ERR_TIMEOUTif a conversion does not finish in timeESP_ERR_INVALID_RESPONSEif the measured value is not physical, which means that the calibration data is corruptedanother error code from the I2C layer
-
struct bmp180_dev_t
- #include <bmp180.h>
BMP180 device descriptor.