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 (A0=0,A1=0,factory default address)
Sample Code
#include "DFRobot_Multi_DOF_IMU.h"
#include <Wire.h>
#define IMU_COMM_I2C
const uint8_t ADDR = 0x4A;
// Initialize IMU in I2C mode
DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel9DOF, &Wire, ADDR);
void setup() {
// Initialize serial port
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("9DOF IMU Polling Example (I2C)");
// [1] Initialize the 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);
// [2] Set the sensor 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);
// [3] Set accelerometer range to ±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);
// [4] Set gyroscope range to ±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);
// Configuration completion prompt
Serial.println("\nConfiguration complete, starting data reading");
}
void loop() {
DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro, mag;
// Read 9-axis data
if (imu.get9dofData(&accel, &gyro, &mag)) {
// Print accelerometer data
Serial.print("Accel (g): 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
Serial.print("Gyro (dps): 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
Serial.print("Mag (uT): 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?
