# esp-idf-lib/dps310
[](https://github.com/esp-idf-lib/dps310/actions/workflows/build.yml)
[](https://github.com/esp-idf-lib/dps310/actions/workflows/build-docs.yml)
[](https://github.com/esp-idf-lib/dps310/actions/workflows/validate-component.yml)
Driver for the Infineon DPS310 barometric pressure and temperature sensor.
The DPS310 measures pressure from 300 to 1200 hPa and temperature from -40 to
+85 °C. Both values are compensated inside the driver with the calibration
coefficients the sensor keeps in its OTP memory, so the application receives
pascals and degrees Celsius, not raw counts. A pressure reading is also the
basis of the altitude calculation the component provides.
* [Documentation](https://esp-idf-lib.github.io/dps310/)
* [Repository](https://github.com/esp-idf-lib/dps310)
* [Issues](https://github.com/esp-idf-lib/dps310/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/dps310)
## Features
- pressure in pascals and temperature in degrees Celsius, both compensated
- 1 to 128 measurements per second, 1 to 128 times oversampling per value
- command mode (one measurement on request) and background mode (continuous)
- internal or external temperature sensor for the compensation
- pressure and temperature ready flags, so polling can be avoided
- an 8-bit FIFO with interrupts on a full FIFO or a ready measurement
- altitude with a calibration against a known height
- 1 MHz I2C (the sensor itself allows 3.4 MHz, the ESP-IDF driver stops at
1 MHz)
## Wiring
The sensor uses I2C at address `0x76` when the SDO pin is tied low and `0x77`
when it is tied high (`DPS310_I2C_ADDRESS_0` and `DPS310_I2C_ADDRESS_1`);
the examples use `0x77`.
The supply is 1.7 to 3.6 V.
| DPS310 | ESP32 |
|--------|-------|
| VDD | 3.3 V |
| GND | GND |
| SDA | `CONFIG_EXAMPLE_I2C_MASTER_SDA` (18 on ESP32/S2/S3) |
| SCL | `CONFIG_EXAMPLE_I2C_MASTER_SCL` (19 on ESP32/S2/S3) |
| SDO | GND for `0x76`, VDD for `0x77` |
## Usage
```c
#include <dps310.h>
static dps310_t dev;
void app_main(void)
{
ESP_ERROR_CHECK(i2cdev_init());
ESP_ERROR_CHECK(dps310_init_desc(&dev, DPS310_I2C_ADDRESS_1, 0,
CONFIG_EXAMPLE_I2C_MASTER_SDA, CONFIG_EXAMPLE_I2C_MASTER_SCL));
dps310_config_t config = DPS310_CONFIG_DEFAULT();
ESP_ERROR_CHECK(dps310_init(&dev, &config));
// one measurement on request
ESP_ERROR_CHECK(dps310_set_mode(&dev, DPS310_MODE_COMMAND_PRESSURE));
float pressure;
if (dps310_read_pressure_wait(&dev, 10, 10, &pressure) == ESP_OK)
printf("Pressure: %.1f Pa\n", pressure);
dps310_free_desc(&dev);
i2cdev_done();
}
```
`dps310_init_desc()` only fills the descriptor and creates the mutex;
`dps310_init()` is what talks to the chip: it checks the product ID, resets the
sensor, reads the 18 calibration coefficients and applies the configuration.
`DPS310_CONFIG_DEFAULT()` sets 1 measurement per second with 16 times
oversampling for both pressure and temperature, takes the temperature from the
external sensor (the one inside the pressure MEMS element) and enables the
right shift of the results.
## Modes
| Mode | Meaning |
|------|---------|
| `DPS310_MODE_STANDBY` | no measurements |
| `DPS310_MODE_COMMAND_PRESSURE` | one pressure measurement, then standby |
| `DPS310_MODE_COMMAND_TEMPERATURE` | one temperature measurement, then standby |
| `DPS310_MODE_BACKGROUND_PRESSURE` | continuous pressure |
| `DPS310_MODE_BACKGROUND_TEMPERATURE` | continuous temperature |
| `DPS310_MODE_BACKGROUND_ALL` | continuous pressure and temperature |
`dps310_backgorund_start()` and `dps310_backgorund_stop()` are thin wrappers
around `dps310_set_mode()` with the name stressed.
In command mode the result is not ready immediately: either poll
`dps310_is_ready_for_pressure()` / `dps310_is_ready_for_temp()`, or use
the `..._wait()` variants, which retry for the given number of attempts with
the given delay in milliseconds. The delay is rounded up to at least one
FreeRTOS tick, so a ten-millisecond delay still sleeps with a 100 Hz tick.
`dps310_read_raw()` returns the uncompensated 24-bit value from either the
pressure or the temperature register block. Use it only for diagnostics; the
compensated functions are `dps310_read_pressure()` and `dps310_read_temp()`.
## Rates and oversampling
`dps310_set_rate_p()`/`dps310_set_rate_t()` take `DPS310_PM_RATE_1` …
`DPS310_PM_RATE_128` (`DPS310_TMP_RATE_*` for temperature), that is 1 to 128
measurements per second. `dps310_set_oversampling_p()`/`..._t()` take
`DPS310_PM_PRC_1` … `DPS310_PM_PRC_128` (`DPS310_TMP_PRC_*`), the number of
internal samples averaged into one result. More oversampling means less noise
and a longer measurement, and the driver has to know the ratio: the pressure
compensation divides the raw value by the scale factor that matches the
oversampling of that measurement, so a value read with one rate and compensated
with another is wrong. The driver keeps the last rates in `dev->p_rate` and
`dev->t_rate` for exactly this reason; change the hardware registers through the
setters, not behind the driver's back.
Changing the oversampling while the FIFO is enabled empties the FIFO: the entries
already in it were taken with the old resolution and cannot be compensated with
the new one. The compensated reads also need a descriptor that went through
`dps310_init()`, because they rely on the cached rates; before that they return
`ESP_ERR_INVALID_STATE` instead of guessing.
Oversampling above 8 times needs the result bit-shift: the hardware shifts the
result to a 24-bit value before the driver scales it. `dps310_init()` therefore
returns `ESP_ERR_INVALID_ARG` when the oversampling is above 8 and the shift is
disabled, and the setters reject values outside their enumerations instead of
writing stray bits into the registers.
`dps310_set_tmp_ext()` selects whether the temperature comes from the ASIC
(`DPS310_TMP_SRC_INTERNAL`) or from the MEMS element
(`DPS310_TMP_SRC_EXTERNAL`). `dps310_set_tmp_coef_ext()` selects the source of
the temperature *coefficients*. With the external source the coefficients are
used together with the external temperature; if the coefficients were measured
with the internal sensor, the result is less accurate. The default takes both
from the external source.
## Interrupts and FIFO
`dps310_set_int_hl()` sets the polarity of the interrupt pin,
`dps310_set_int_prs()`/`dps310_set_int_tmp()` enable the "a measurement is
ready" interrupts, `dps310_set_int_fifo()` enables the "FIFO is full"
interrupt. The driver configures the sensor only; the pin itself is a plain
GPIO that the application has to configure and read (or attach an interrupt
handler to) on its own.
The FIFO holds up to 8 measurements. `dps310_set_fifo_en()` enables it,
`dps310_is_fifo_empty()` tells whether there is something to read,
`dps310_read_fifo()` returns one entry with its type
(`DPS310_MEASUREMENT_TEMPERATURE`, `DPS310_MEASUREMENT_PRESSURE` or
`DPS310_MEASUREMENT_EMPTY`) and the already compensated value, and
`dps310_flush_fifo()` clears it.
A pressure entry is compensated with the cached temperature, and the cache is
only valid for one oversampling: the value read with one averaging ratio cannot
be used with another. `dps310_read_fifo()` returns
`ESP_ERR_INVALID_STATE` for a pressure entry when no temperature sample with the
current oversampling has been read yet, so keep the FIFO's temperature entries
enabled or call `dps310_read_temp()` first.
## Altitude
```c
ESP_ERROR_CHECK(dps310_calibrate_altitude(&dev, 120.0f)); // known height
float altitude;
ESP_ERROR_CHECK(dps310_read_altitude(&dev, &altitude));
```
By default the altitude is calculated from the average sea-level pressure
`DPS310_AVERAGE_SEA_LEVEL_PRESSURE_Pa`. `dps310_calibrate_altitude()` measures
the current pressure, compares it with the known height and keeps the
difference in `dev->offset`; `dps310_calc_altitude()` and
`dps310_read_altitude()` add that offset. Calibration is worth doing because
the local sea-level pressure differs from the average by more than the sensor
error.
The known altitude must be finite and lie between −1000 and 44330 metres, the
height where the formula divides by zero; anything else returns
`ESP_ERR_INVALID_ARG`. During the calibration the function raises the
oversampling to 64 times, enables both result shifts, switches the chip to the
command modes and then restores the previous oversampling, shifts and operation
mode, so a sensor running in the background mode keeps measuring. If a restore
step fails, the function returns an error: the remaining steps are still tried,
so the sensor is left with some of the calibration settings and some of its
own.
## A note on the quirk
`dps310_init()` calls `dps310_quirk()`, which writes undocumented values to
undocumented registers. The issue and the workaround come from Infineon's own
DPS310 library; the data sheet and the errata do not mention it. After the magic
writes the function re-reads the coefficients and performs one temperature
measurement. The coefficients are read again after the coefficient source is
selected, and the temperature taken by the quirk is dropped once `dps310_init()`
applies the real configuration: it belongs to the previous oversampling. The
function is public in case a sensor needs it again after a reset.
## Limitations
- only I2C is supported, although the chip also speaks SPI
- the driver does not touch the interrupt pin, see above
- the measurement rate and oversampling must be changed through the driver, see
above
- temperature accuracy is limited by the coefficient source, see above
- the FIFO depth is 8 measurements, and the driver reads one entry per call
## Examples
- `default` — one pressure and one temperature reading
- `altitude` — calibrated altitude
- `background` — continuous measurements in background mode
## Installation
```sh
idf.py add-dependency esp-idf-lib/dps310
```
## 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).
c917ff9ede38f6f0ffdd0dad8bc45e21e07308e9
idf.py add-dependency "esp-idf-lib/dps310^1.0.9"