Example Code for Arduino-Reading Data via I2C
This tutorial demonstrates how to initialize a 10-axis IMU sensor and directly read acceleration, angular velocity, magnetometer, and barometric pressure data using the polling method.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0696 Gravity: 10 DOF IMU Sensor ×1
Software Preparation
- Download and install Arduino IDE: Download Arduino IDE
- Download and install the DFRobot_Multi_DOF_IMU library: Download DFRobot_Multi_DOF_IMU Library
- Download and install the DFRobot_RTU library: Download DFRobot_RTU Library
- Library Installation Guide: View Installation Guide
Wiring Diagram

Connect the 10-axis IMU sensor to the ESP32-C5 as shown in the diagram, with the core pin correspondences as follows:
- Sensor pin "+" → ESP32-C5 3.3V
- Sensor pin "-" → ESP32-C5 GND
- Sensor I2C pin "SCL" → ESP32-C5 SCL (GPIO10)
- Sensor I2C pin "SDA" → ESP32-C5 SDA (GPIO9)
- Sensor DIP switch configuration: Set the communication mode switch to the I2C side, and set the I2C address switch to 0x4A (factory default address)
Sample Code
#include "DFRobot_Multi_DOF_IMU.h"
#define IMU_COMM_I2C
// Whether to calculate altitude (true=altitude, false=pressure)
const bool CALCULATE_ALTITUDE = true;
const uint8_t IMU_ADDR = 0x4A;
// Initialize IMU object for I2C communication
DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel10DOF, &Wire, IMU_ADDR);
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("\n10DOF IMU Simplified Data Reading (ESP32 I2C)\n");
// Initialize sensor
Serial.print("Initializing IMU sensor... ");
while (!imu.begin()) {
Serial.println("Failed! Check address and wiring.");
delay(1000);
}
Serial.println("Success!");
// Basic configuration (sensor mode, accelerometer and gyroscope range)
imu.setSensorMode(DFRobot_Multi_DOF_IMU::eNormalMode);
imu.setAccelRange(DFRobot_Multi_DOF_IMU::eAccelRange2G);
imu.setGyroRange(DFRobot_Multi_DOF_IMU::eGyroRange250DPS);
// Altitude calibration (reference altitude 540 meters, modify according to actual situation)
if (CALCULATE_ALTITUDE) {
Serial.print("Calibrating altitude (reference: 540m)... ");
imu.calibrateAltitude(540.0);
Serial.println("Done");
}
Serial.println("\nConfiguration completed. Starting data reading...\n");
delay(500);
}
void loop() {
// Define variables to store sensor data
DFRobot_Multi_DOF_IMU::sSensorData_t accelData, gyroData, magData;
float pressureAltValue; // Store pressure (Pa) or altitude (m)
// Read 10DOF sensor data
if (imu.get10dofData(&accelData, &gyroData, &magData, &pressureAltValue, CALCULATE_ALTITUDE)) {
Serial.println("==================== IMU Data ====================");
// Accelerometer data
Serial.println("--- Accelerometer Data (g) ---");
Serial.print("X-axis: "); Serial.print(accelData.x, 3); Serial.println(" g");
Serial.print("Y-axis: "); Serial.print(accelData.y, 3); Serial.println(" g");
Serial.print("Z-axis: "); Serial.print(accelData.z, 3); Serial.println(" g");
// Gyroscope data
Serial.println("--- Gyroscope Data (dps) ---");
Serial.print("X-axis: "); Serial.print(gyroData.x, 2); Serial.println(" dps");
Serial.print("Y-axis: "); Serial.print(gyroData.y, 2); Serial.println(" dps");
Serial.print("Z-axis: "); Serial.print(gyroData.z, 2); Serial.println(" dps");
// Magnetometer data
Serial.println("--- Magnetometer Data (uT) ---");
Serial.print("X-axis: "); Serial.print(magData.x, 2); Serial.println(" uT");
Serial.print("Y-axis: "); Serial.print(magData.y, 2); Serial.println(" uT");
Serial.print("Z-axis: "); Serial.print(magData.z, 2); Serial.println(" uT");
// Pressure/Altitude data
if (CALCULATE_ALTITUDE) {
Serial.println("--- Altitude Data ---");
Serial.print("Altitude: "); Serial.print(pressureAltValue, 2); Serial.println(" meters");
} else {
Serial.println("--- Pressure Data ---");
Serial.print("Pressure: "); Serial.print(pressureAltValue, 2); Serial.println(" Pa");
Serial.print("Pressure (hPa): "); Serial.print(pressureAltValue / 100.0, 2); Serial.println(" hPa");
}
Serial.println("===================================================\n");
} else {
Serial.println("Error: Failed to read IMU sensor data!\n");
}
delay(500);
}
Result

Was this article helpful?
