Read on the data-ready interrupt
What this example shows
Instead of asking the sensor over and over, the controller waits for the
BMP585's INT pin. The sensor pulses INT high each time a new reading is
ready, and only then does the sketch read and print it. With the 50 Hz rate
begin() sets, that is a new line about every 20 ms.
Connect the breakout over I2C as in the Quick Start, and wire the breakout's
INT pin to pin 2 of your controller. To use another pin, change
interruptPin.
The example code
#include <BMP585-SOLDERED.h>
// Create an instance of the BMP585 sensor
Soldered_BMP585 pressureSensor;
// Pin connected to the sensor's INT pin, change to match your wiring
int interruptPin = 2;
// Flag to know when an interrupt occurs
volatile bool interruptOccurred = false;
void bmp585InterruptHandler()
{
interruptOccurred = true;
}
void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMP585 Example - Data Ready Interrupt");
// Initialize I2C bus
Wire.begin();
// Check if sensor is connected and initialize it.
// Address is optional, defaults to 0x47
while (pressureSensor.begin() != BMP5_OK)
{
Serial.println("Error: BMP585 not connected, check wiring and I2C address!");
delay(1000);
}
Serial.println("BMP585 connected!");
// Configure the interrupt pin as push/pull, active high, pulsed
pressureSensor.configureInterrupt(BMP5_PULSED, BMP5_ACTIVE_HIGH, BMP5_INTR_PUSH_PULL, true);
// Only assert the pin on a new data-ready reading
pressureSensor.setInterruptSource(true);
// Attach the interrupt handler to the microcontroller pin wired to INT
attachInterrupt(digitalPinToInterrupt(interruptPin), bmp585InterruptHandler, RISING);
}
void loop()
{
if (interruptOccurred)
{
interruptOccurred = false;
if (pressureSensor.getSensorData() == BMP5_OK)
{
Serial.print("Pressure: ");
Serial.print(pressureSensor.pressure, 2);
Serial.print(" Pa\t");
Serial.print("Temperature: ");
Serial.print(pressureSensor.temperature, 2);
Serial.println(" degC");
}
}
}
Expected result:
BMP585 Example - Data Ready Interrupt
BMP585 connected!
Pressure: 101318.94 Pa Temperature: 23.44 degC
Pressure: 101314.99 Pa Temperature: 23.44 degC
Pressure: 101317.14 Pa Temperature: 23.45 degC
Pressure: 101319.16 Pa Temperature: 23.44 degC
One line arrives per interrupt: the pressure in pascals and the temperature in
°C, each with two decimals. If BMP585 connected! appears but no readings follow,
the controller is not seeing INT: check that wire and interruptPin.
Functions used
Soldered_BMP585()returns Soldered_BMP585Creates the sensor object. Nothing is sent to the sensor until you call begin().
ReturnsThe sensor object.
Wire.begin()returns voidStarts the Arduino Wire (I2C) library and joins the I2C bus as a controller. Call it once, before begin(). Wire comes with the Arduino core and is not part of the BMP585 library.
ReturnsNothing.
pressureSensor.begin(uint8_t address = BMP5_I2C_ADDR_SEC, TwoWire &wirePort = Wire)returns int8_tStarts the sensor over I2C: a soft reset, a check that the BMP585 is there, then temperature oversampling 64x, pressure oversampling 4x, a 50 Hz output data rate with pressure enabled, and NORMAL power mode. The library supports I2C only.
ReturnsBMP5_OK (0) on success, a negative error code otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | address | I2C address. BMP5_I2C_ADDR_SEC (0x47) by default; pass BMP5_I2C_ADDR_PRIM (0x46) when the ADDRESS jumper (JP3) is moved. |
TwoWire & | wirePort | The I2C bus to use. Wire by default. |
pressureSensor.configureInterrupt(enum bmp5_intr_mode mode, enum bmp5_intr_polarity pol, enum bmp5_intr_drive drive, bool enable)returns int8_tSets how the INT pin behaves and turns it on or off. It does not choose which event drives the pin; setInterruptSource() does that.
ReturnsBMP5_OK (0) on success, a negative error code otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bmp5_intr_mode | mode | BMP5_PULSED or BMP5_LATCHED. The example uses pulsed. |
bmp5_intr_polarity | pol | BMP5_ACTIVE_LOW or BMP5_ACTIVE_HIGH. The example uses active high, so the pin rises on an event. |
bmp5_intr_drive | drive | BMP5_INTR_PUSH_PULL or BMP5_INTR_OPEN_DRAIN. |
bool | enable | true to enable the INT pin, false to disable it. |
pressureSensor.setInterruptSource(bool dataReady, bool fifoFull = false, bool fifoThreshold = false, bool pressureOOR = false)returns int8_tChooses which events drive the INT pin. Call configureInterrupt() first.
ReturnsBMP5_OK (0) on success, a negative error code otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bool | dataReady | A new pressure and temperature reading is ready. The example passes true. |
bool | fifoFull | The FIFO buffer is full. false by default. |
bool | fifoThreshold | The FIFO watermark is reached. false by default. |
bool | pressureOOR | Pressure is out of range. false by default. |
pressureSensor.getSensorData()returns int8_tReads the latest pressure and temperature from the sensor and stores them in the public members pressure (float, Pa) and temperature (float, °C).
ReturnsBMP5_OK (0) on success, a negative error code otherwise.
digitalPinToInterrupt(pin)returns intChecks that a controller pin can be used for an interrupt, for passing to attachInterrupt(). It is part of the Arduino core, not the BMP585 library.
ReturnsThe pin when it can be used for an interrupt, -1 when it cannot.
Parameters
| Type | Name | Description |
|---|---|---|
int | pin | The controller pin wired to INT. The example passes interruptPin (2). |
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)returns voidRuns the function ISR whenever the pin changes the way mode says. The ISR takes no parameters, returns nothing and should be short. It is part of the Arduino core, not the BMP585 library.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | digitalPinToInterrupt(pin) | The pin wired to INT, passed through digitalPinToInterrupt(). |
| function | ISR | The function to call. The example passes bmp585InterruptHandler. |
| constant | mode | LOW, CHANGE, RISING or FALLING. The example uses RISING, since INT is set to active high. |
Putting it together
configureInterrupt() makes INT a push-pull, active-high pulse, and
setInterruptSource(true) lets only the data-ready event drive it. The Arduino
core's attachInterrupt() then runs bmp585InterruptHandler() on each rising
edge of the controller's pin. The handler only sets a flag; loop() sees the
flag, clears it and calls getSensorData(), so the I2C traffic stays out of
the interrupt routine.