Example Code for Arduino-Reading Data via I2C
This tutorial demonstrates how to initialize the BMI323, BMM350, and BMP581 sensors, and directly read accelerometer, gyroscope, magnetometer, and barometric pressure data via polling.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0697 Fermion: BMI323+BMM350+BMP581 10 DOF IMU Sensor ×1
Software Preparation
-
Download and install Arduino IDE: Download Arduino IDE
-
Download and install the DFRobot_BMI323 library: Download the DFRobot_BMI323 library
-
Download and install the DFRobot_BMM350 library: Download the DFRobot_BMM350 library
-
Download and install the DFRobot_BMP58X library: Download the DFRobot_BMP58X library
-
Library Installation Guide: View Installation Guide
Wiring Diagram

Connect the 10-axis IMU sensor to the ESP32-C5 as shown in the diagram. Core connections:
- Sensor pin “3V3” → ESP32-C5 3.3V
- Sensor pin “GND” → ESP32-C5 GND
- Sensor I2C pin “SCL” → ESP32-C5 SCL (default GPIO10)
- Sensor I2C pin “SDA” → ESP32-C5 SDA (default GPIO9)
- I2C address pad configuration: Leave the I2C address pads of BMI323, BMM350, and BMP581 open. In this state, the I2C address of BMI323 is 0x69, BMM350 is 0x15, and BMP581 is 0x47 (factory default mode).
Sample Code
#include "DFRobot_BMI323.h"
#include "DFRobot_BMM350.h"
#include "DFRobot_BMP58X.h"
#define BMP5_COMM_I2C
#define CALIBRATE_ABSOLUTE_DIFFERENCE
#define BMI323_I2C_ADDR 0x69
#define BMM350_I2C_ADDR 0x15
#define BMP58X_I2C_ADDR 0x47
DFRobot_BMI323 bmi323(&Wire, BMI323_I2C_ADDR);
DFRobot_BMM350_I2C bmm350(&Wire, BMM350_I2C_ADDR);
DFRobot_BMP58X_I2C bmp58x(&Wire, BMP58X_I2C_ADDR);
void setup()
{
Serial.begin(115200);
while(!Serial) {
delay(10);
}
Serial.println("Combined Sensor Demo: BMI323 (6-axis) + BMM350 (Magnetometer) + BMP58X (Pressure)");
Serial.println("==================================================================================");
// Initialize BMI323 (6-axis)
while (!bmi323.begin()) {
Serial.println("BMI323 init failed, retry in 1s");
delay(1000);
}
Serial.println("BMI323 init success!");
// Configure accelerometer: 50Hz sampling, ±8g range, normal mode
if (!bmi323.configAccel(bmi323.eAccelODR50Hz, bmi323.eAccelRange8G, bmi323.eAccelModeNormal)) {
Serial.println("Accel config failed!");
while(1) delay(1000);
}
// Configure gyroscope: 800Hz sampling, ±2000dps range, normal mode
if (!bmi323.configGyro(bmi323.eGyroODR800Hz, bmi323.eGyroRange2000DPS, bmi323.eGyroModeNormal)) {
Serial.println("Gyro config failed!");
while(1) delay(1000);
}
// Initialize BMM350 (magnetometer)
while (bmm350.begin()) { // begin() returns 0 for success, non-zero for failure
Serial.println("BMM350 init failed, retry in 1s");
delay(1000);
}
Serial.println("BMM350 init success!");
// Set operation mode to normal mode
bmm350.setOperationMode(eBmm350NormalMode);
// Set preset mode to high accuracy, data rate 25Hz
bmm350.setPresetMode(BMM350_PRESETMODE_HIGHACCURACY, BMM350_DATA_RATE_25HZ);
// Enable X, Y, Z axis measurement (enabled by default, but explicit call ensures)
bmm350.setMeasurementXYZ();
// Initialize BMP58X (pressure sensor)
while(!bmp58x.begin()){
Serial.println("BMP58X init fail!");
delay(1000);
}
Serial.println("BMP58X init success!");
// Calibrate absolute difference (modify parameter according to actual altitude)
#if defined(CALIBRATE_ABSOLUTE_DIFFERENCE)
/* Example uses an altitude of 540 meters in Wenjiang District, Chengdu. Replace with local altitude when using. */
bmp58x.calibratedAbsoluteDifference(540.0);
#endif
// Set measurement mode to normal mode
bmp58x.setMeasureMode(bmp58x.eNormal);
Serial.println("Setup complete!\n");
}
void loop()
{
// Read BMI323 data
DFRobot_BMI323::sSensorData accel, gyro;
if (bmi323.getAccelGyroData(&accel, &gyro)) {
Serial.print("Accel (g) : ");
Serial.print(accel.x, 3);
Serial.print(", ");
Serial.print(accel.y, 3);
Serial.print(", ");
Serial.println(accel.z, 3);
Serial.print("Gyro (dps) : ");
Serial.print(gyro.x, 2);
Serial.print(", ");
Serial.print(gyro.y, 2);
Serial.print(", ");
Serial.println(gyro.z, 2);
} else {
Serial.println("Failed to read BMI323 data.");
}
// Read BMM350 data
sBmm350MagData_t magData = bmm350.getGeomagneticData();
Serial.print("Mag (uT) : ");
Serial.print(magData.float_x, 2);
Serial.print(", ");
Serial.print(magData.float_y, 2);
Serial.print(", ");
Serial.println(magData.float_z, 2);
// Read BMP58X data
Serial.print("Pressure : ");
Serial.print(bmp58x.readPressPa());
Serial.println(" Pa");
Serial.print("Altitude : ");
Serial.print(bmp58x.readAltitudeM());
Serial.println(" m");
Serial.println("--------------------------------");
delay(200); // Approximately 5Hz refresh rate
}
Result

Was this article helpful?
