Example Code for Arduino-Reading Data via I2C
This tutorial demonstrates how to initialize the BMI323 and BMM350 sensors, and directly read accelerometer, gyroscope, and magnetometer data via polling.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0695 Fermion: BMI323+BMM350 9 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
-
Library Installation Guide: View Installation Guide
Wiring Diagram

Connect the 9-axis IMU sensor to the ESP32-C5 as shown in the diagram. The core connections are:
- 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 and BMM350 unmodified (open). In this state, the I2C address of BMI323 is 0x69, and the I2C address of BMM350 is 0x15 (factory default mode).
Sample Code
#include "DFRobot_BMI323.h"
#include "DFRobot_BMM350.h"
#define BMI323_I2C_ADDR 0x69
#define BMM350_I2C_ADDR 0x15
DFRobot_BMI323 bmi323(&Wire, BMI323_I2C_ADDR);
DFRobot_BMM350_I2C bmm350(&Wire, BMM350_I2C_ADDR);
void setup()
{
Serial.begin(115200);
while(!Serial) {
delay(10);
}
Serial.println("BMI323 and BMM350 Combined Sensor Demo");
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();
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);
Serial.println("--------------------------------");
delay(200); // Approximately 5Hz refresh rate
}
Result

Was this article helpful?
