Example Code for Arduino-Reading Data via I2C
This tutorial demonstrates how to initialize a 9 DOF IMU sensor and directly read acceleration, angular velocity, and magnetometer data using the polling method.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0694 Gravity: 9 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 9 DOF 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"
const uint8_t ADDR = 0x4A;
// Create I2C interface IMU object
DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel9DOF, &Wire, ADDR);
void setup()
{
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("\n9DOF IMU Polling Example (I2C)");
// Initialize sensor
Serial.print("[1] Initializing sensor... ");
while (!imu.begin()) {
Serial.println("Failed, please check device address and connections!");
delay(1000);
}
Serial.println("Success");
delay(1000);
// Set to normal mode
Serial.print("[2] Setting sensor mode to normal... ");
while (!imu.setSensorMode(DFRobot_Multi_DOF_IMU::eNormalMode)) {
Serial.println("Failed, please check device communication!");
delay(1000);
}
Serial.println("Success");
delay(1000);
// Accelerometer range ±2g
Serial.print("[3] Setting accelerometer range to ±2G... ");
while (!imu.setAccelRange(DFRobot_Multi_DOF_IMU::eAccelRange2G)) {
Serial.println("Failed, please check device communication!");
delay(1000);
}
Serial.println("Success");
delay(1000);
// Gyroscope range ±250dps
Serial.print("[4] Setting gyroscope range to ±250dps... ");
while (!imu.setGyroRange(DFRobot_Multi_DOF_IMU::eGyroRange250DPS)) {
Serial.println("Failed, please check device communication!");
delay(1000);
}
Serial.println("Success");
delay(100);
Serial.println("\nConfiguration complete, starting data reading");
}
void loop()
{
DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro, mag;
if (imu.get9dofData(&accel, &gyro, &mag)) {
// Print accelerometer data (unit: g)
Serial.print("Accel (g): ");
Serial.print("X="); Serial.print(accel.x, 3);
Serial.print(", Y="); Serial.print(accel.y, 3);
Serial.print(", Z="); Serial.println(accel.z, 3);
// Print gyroscope data (unit: dps)
Serial.print("Gyro (dps): ");
Serial.print("X="); Serial.print(gyro.x, 2);
Serial.print(", Y="); Serial.print(gyro.y, 2);
Serial.print(", Z="); Serial.println(gyro.z, 2);
// Print magnetometer data (unit: uT)
Serial.print("Mag (uT): ");
Serial.print("X="); Serial.print(mag.x, 2);
Serial.print(", Y="); Serial.print(mag.y, 2);
Serial.print(", Z="); Serial.println(mag.z, 2);
Serial.println("---");
} else {
Serial.println("Failed to read data!");
}
delay(500);
}
Result

Was this article helpful?
