Example Code for Arduino-FIFO & Interrupt Data Reading

Last revision 2026/02/05

This guide focuses on using FIFO and interrupt read techniques in Arduino to optimize sensor data handling, covering setup, wiring, and sample code for the BMP581 barometric sensor.

Introduction to FIFO

FIFO (First In, First Out) is an integrated data buffering mechanism within the sensor. Its core feature is that data is temporarily stored here and retrieved in batches when needed, ensuring the temporal consistency of data.

In barometric pressure sensors, FIFO serves as a temporary data storage unit, responsible for data buffering and batch processing: Temperature and pressure data collected by the sensor at a set frequency are first written to the FIFO in an orderly manner instead of being transmitted to the external host in real time; when data acquisition is required, the host can read all cached data in the FIFO with a single operation, enabling batch retrieval.

Compared with systems without a buffering mechanism, FIFO offers distinct advantages:

  • Eliminates bus resource occupancy caused by high-frequency real-time communication, reducing system interaction overhead
  • Prevents transient data loss due to host processing delays, safeguarding data integrity
  • Supports on-demand batch reading, adapting to data analysis rhythms in various scenarios

In essence, FIFO acts as a data coordination unit between the sensor and the host, optimizing the efficiency and reliability of data interaction through orderly buffering and batch transmission.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0665-FIFO Wiring Diagram

  • Connect the Gravity: BMP581 Barometric Pressure Sensor to the ESP32-E as illustrated, with the key wiring correspondences as follows:
    • Sensor Pin "VCC" → ESP32-E 3.3V
    • Sensor Pin "GND" → ESP32-E GND
    • Sensor I2C Pin "SCL" → ESP32-E SCL (default GPIO22)
    • Sensor I2C Pin "SDA" → ESP32-E SDA (default GPIO21)
    • Sensor Pin "INT" → ESP32-E D6 (GPIO14)
    • Sensor DIP Switch Configuration: Set the communication mode to I2C and the I2C address to 0x47

Note: The setting of the communication mode DIP switch persists after power-off, and the new mode takes effect upon device restart.

Sample Code

#include "DFRobot_BMP58X.h"
#include "DFRobot_RTU.h"

#define BMP5_COMM_I2C

/* Uncomment this line if eliminating absolute measurement error is not required */
#define CALIBRATE_ABSOLUTE_DIFFERENCE

/**
 * Configure interrupt mode: Uncomment the macro below to use latched interrupt;
 * otherwise, pulsed interrupt will be used
 */
// #define BMP5_INT_MODE_LATCHED

const uint8_t ADDR = 0x47;

DFRobot_BMP58X_I2C bmp58x(&Wire, ADDR);

volatile uint8_t flag = 0;
/* Interrupt Service Routine: Set flag when triggered */
void interrupt() {
  if (flag == 0) {
    flag = 1;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("Initializing..");
  
  while (!bmp58x.begin()) {
    Serial.println("Sensor initialization failed!");
    delay(1000);
  }

  /**
   * Configure FIFO parameters:
   * - Data Type: Pressure + Temperature
   * - Downsampling: None
   * - Mode: Overwrite old data when full
   * - Threshold: 2 frames (triggers interrupt when reached)
   */
  bmp58x.configFIFO(
    bmp58x.eFIFOPressAndTempData,  // Store pressure and temperature data
    bmp58x.eNoDownSampling,        // No downsampling
    bmp58x.eFIFOOverwriteMode,     // Overwrite mode
    0x02                           // Threshold set to 2 frames
  );

  /**
   * Configure interrupt parameters:
   * - Trigger Mode: Pulsed/Latched (depends on macro definition)
   * - Polarity: Active high
   * - Output Type: Push-pull
   */
#if defined(BMP5_INT_MODE_LATCHED)
  bmp58x.configInterrupt(bmp58x.eIntModeLatched, bmp58x.eIntHighActive, bmp58x.eIntPushPull);
#else
  bmp58x.configInterrupt(bmp58x.eIntModePulsed, bmp58x.eIntHighActive, bmp58x.eIntPushPull);
#endif

  /* Set interrupt source: Trigger when FIFO reaches threshold */
  bmp58x.setIntSource(bmp58x.eIntFIFOThres);

  #if defined(CALIBRATE_ABSOLUTE_DIFFERENCE)
  /**
   * Calibrate absolute difference with 540 meters as the reference altitude
   * (Modify to local altitude in practical use)
   */
  bmp58x.calibratedAbsoluteDifference(540.0);
  #endif

  /* Set measurement mode: Normal mode (continuous measurement) */
  bmp58x.setMeasureMode(bmp58x.eNormal);

  /* Attach interrupt to ESP32's D6 pin (rising edge trigger) */
  attachInterrupt(digitalPinToInterrupt(D6), interrupt, RISING);
}

void loop() {
  /* Latched interrupt requires reading status register to clear flag */
#if defined(BMP5_INT_MODE_LATCHED)
  bmp58x.getIntStatus();
#endif

  /* When interrupt flag is detected, read FIFO data and print */
  if (flag == 1) {
    DFRobot_BMP58X::sFIFOData_t data = bmp58x.getFIFOData();
    Serial.print("FIFO len: ");
    Serial.println(data.len);
    
    /* Loop through and print all data in FIFO */
    for(int i = 0; i < data.len; ++i){
      Serial.print("temp: ");
      Serial.print(data.fifoTempC[i]);
      Serial.print(" (°C)  pressure: ");
      Serial.print(data.fifoPressPa[i]);
      Serial.println(" (Pa)");
    }
    
    flag = 0;  // Clear flag to wait for next interrupt
  }
}

Result

SEN0665-FIFO Result

Was this article helpful?

TOP