Example Code for Arduino-I2C Continuous Data Reading

Last revision 2026/02/05

The article outlines the process of setting up and programming an Arduino board to continuously read atmospheric pressure, temperature, and altitude using a BMP581 sensor.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0665-I2C 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 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"
#define BMP5_COMM_I2C
#define CALIBRATE_ABSOLUTE_DIFFERENCE

const uint8_t ADDR = 0x47;
DFRobot_BMP58X_I2C bmp58x(&Wire, ADDR);

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

// Calibrate absolute error (modify parameters according to actual altitude)
  #if defined(CALIBRATE_ABSOLUTE_DIFFERENCE)
/* This example uses an elevation of 540 meters in Wenjiang District, Chengdu. Replace it with the local elevation when in use.*/
    bmp58x.calibratedAbsoluteDifference(540.0);
  #endif

  // Set the measurement mode to normal mode
  bmp58x.setMeasureMode(bmp58x.eNormal);
}

void loop() {
  delay(1000);

  Serial.print("Temperature: ");
  Serial.print(bmp58x.readTempC());
  Serial.println(" °C");
  
  Serial.print("Atmospheric Pressure: ");
  Serial.print(bmp58x.readPressPa());
  Serial.println(" Pa");
  
  Serial.print("Altitude: ");
  Serial.print(bmp58x.readAltitudeM());
  Serial.println(" m");
  Serial.println("=================================");
}

Result

SEN0665-I2C Result

Was this article helpful?

TOP