# esp-idf-lib/bme680
[](https://github.com/esp-idf-lib/bme680/actions/workflows/build.yml)
[](https://github.com/esp-idf-lib/bme680/actions/workflows/build-docs.yml)
[](https://github.com/esp-idf-lib/bme680/actions/workflows/validate-component.yml)
Driver for the Bosch BME680 environmental sensor: temperature, barometric
pressure, relative humidity and a gas resistance that changes with the mix of
volatile organic compounds in the air.
* [Documentation](https://esp-idf-lib.github.io/bme680/)
* [Repository](https://github.com/esp-idf-lib/bme680)
* [Issues](https://github.com/esp-idf-lib/bme680/issues)
* [Discussions and questions](https://github.com/esp-idf-lib/core/discussions)
* [Component page at the ESP Component Registry](https://components.espressif.com/components/esp-idf-lib/bme680)
## Installation
```sh
idf.py add-dependency esp-idf-lib/bme680
```
## What the sensor gives you
The chip measures four things in one cycle:
| Quantity | Fixed point | Floating point |
| --- | --- | --- |
| Temperature | `int16_t`, °C × 100 | `float`, °C |
| Pressure | `uint32_t`, Pa | `float`, hPa |
| Humidity | `uint32_t`, %RH × 1000 | `float`, %RH |
| Gas resistance | `uint32_t`, Ω | `float`, Ω |
`bme680_values_fixed_t` keeps everything in integers, `bme680_values_float_t`
is the same data divided by 100, 100 and 1000. An axis that was not measured
keeps its "invalid" marker: `INT16_MIN` for the temperature and `0` for the
other three in the fixed structure, `-327.68f`, `0.0f`, `0.0f`, `0.0f` in the
floating one. Ask for temperature, pressure and humidity through the
oversampling settings — the sensor skips whatever is set to
`BME680_OSR_NONE`.
The gas resistance is not a calibrated quantity: it is a resistance in ohms
that changes with the heater temperature, the humidity and the age of the
sensor. Treat it as a relative signal, not as a concentration.
## Wiring
The driver speaks I2C, the chip also has an SPI mode that is not implemented
here. It answers on `0x76` (`SDO` to ground) or `0x77` (`SDO` to VDDIO), and
`bme680_init_desc()` accepts exactly these two addresses and rejects anything
else with `ESP_ERR_INVALID_ARG`. The datasheet allows 1.2–3.6 V on both VDD and
VDDIO; the breakout boards usually add a regulator and level shifters, so check
whether yours tolerates 5 V on VCC before connecting it to a 5 V rail.
The driver sets the bus to 1 MHz (`I2C_FREQ_HZ` in `bme680.c`). The chip itself
supports up to 3.4 MHz, but that is above what the ESP-IDF I2C master can do, so
1 MHz is the practical limit — and on a long or poorly pulled-up bus it may
still fail, in which case lower the constant and rebuild.
## Measuring
A measurement must be started explicitly. `bme680_measure_fixed()` and
`bme680_measure_float()` do the whole sequence: compute the duration of the
cycle for the current settings, switch the chip to forced mode, sleep, then read
the results. The duration is an estimate from the datasheet: 1.25 ms to wake
up, about 2.3 ms per oversampled conversion for each of T, P and H, plus the
heater time of the gas profile; it is rounded up to whole milliseconds, has
5 ms of slack and is rounded up to whole RTOS ticks with one extra tick, so the
call blocks a little longer than the chip needs.
The lower-level sequence is also available: `bme680_force_measurement()` starts
a cycle, `bme680_is_measuring()` polls the status register, and
`bme680_get_results_fixed()`/`bme680_get_results_float()` read and compensate
the raw data. A second `bme680_force_measurement()` before the results are read
returns `ESP_ERR_INVALID_STATE`, and reading before a measurement was started
returns the same error; if the chip has no new data yet, the read returns
`ESP_ERR_INVALID_STATE` while the conversion is running and
`ESP_ERR_INVALID_RESPONSE` when the status does not report new data at all.
After `bme680_init_sensor()` the sensor runs with oversampling 1× for all three
axes, an IIR filter of 3 and heater profile 0 set to 320 °C for 150 ms.
## Gas measurement
The gas sensor needs a heater, and the heater is described by profiles 0…9.
`bme680_set_heater_profile(dev, profile, temperature, duration)` stores a
profile with a target of 200…400 °C and a duration in milliseconds, and
`bme680_use_heater_profile(dev, profile)` selects the one used by the next
cycle, or disables gas measurement with `BME680_HEATER_NOT_USED`. The values
are written to the chip immediately, so changing a profile that is currently in
use changes the next measurement.
The chip reports two flags with every gas reading: whether the raw value is
valid and whether the heater was stable during the measurement. The driver
warns and leaves `gas_resistance` at zero when either flag is missing, which is
normal for the first cycles after power-on and for a heater that is still
settling.
## Ambient temperature
Pressure compensation uses the measured temperature of the same cycle, not this
value. The ambient temperature is an input of the heater resistance calculator:
the heater needs to know how warm the air around it is to reach its target.
The default is 25 °C; `bme680_set_ambient_temperature()` stores a new value and
rewrites the resistance of every configured heater profile on the chip. The
driver never updates it from the measured temperature by itself, so with large
temperature swings the heater runs a little off its target.
## Example
```c
#include <bme680.h>
bme680_t dev = { 0 };
ESP_ERROR_CHECK(bme680_init_desc(&dev, BME680_I2C_ADDR_0, 0,
CONFIG_EXAMPLE_I2C_MASTER_SDA,
CONFIG_EXAMPLE_I2C_MASTER_SCL));
ESP_ERROR_CHECK(bme680_init_sensor(&dev));
bme680_values_float_t values;
while (true)
{
ESP_ERROR_CHECK(bme680_measure_float(&dev, &values));
printf("T: %.2f deg.C, P: %.2f hPa, H: %.2f %%, G: %.0f Ohm\n",
values.temperature, values.pressure, values.humidity,
values.gas_resistance);
vTaskDelay(pdMS_TO_TICKS(1000));
}
```
The full example is in `examples/default`.
## Limitations
- Only I2C is supported; the SPI mode of the chip is not implemented.
- The bus speed is a compile-time constant, not an argument of
`bme680_init_desc()`.
- The driver does not run a gas sensor calibration or a baseline, and it does
not compute an indoor air quality index — it returns the resistance.
- Humidity is not an input of the gas compensation: the driver converts the raw
resistance and its range only. Humidity still changes the physical signal, so
comparing readings taken at very different humidity is misleading.
- The descriptor mutex serialises single register accesses, not a whole
measurement cycle: two tasks calling `bme680_measure_float()` on the same
descriptor can interleave and get `ESP_ERR_INVALID_STATE`.
- `bme680_free_desc()` only deletes the mutex; the bus itself is owned by
i2cdev and released by `i2cdev_done()`.
## Support
For questions and discussions about the component, please use
[Discussions](https://github.com/esp-idf-lib/core/discussions)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).
## Contributing
Please read [CONTRIBUTING.md](https://github.com/esp-idf-lib/core/blob/main/CONTRIBUTING.md)
at [esp-idf-lib/core](https://github.com/esp-idf-lib/core).
6a47abed56a75bbda7419b51088111d1b0047a23
idf.py add-dependency "esp-idf-lib/bme680^1.0.9"