Example Code for Arduino-Reading Data via I2C
This tutorial guides you through initializing a 6 DOF IMU sensor and reading accelerometer and gyroscope data using I2C polling. Ideal for tech enthusiasts looking to enhance their projects with precise motion sensing capabilities.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0692 Gravity: 6 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 6 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"
// Sensor I2C address
const uint8_t ADDR = 0x4A;
DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel6DOF, &Wire, ADDR);
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("\n6DOF IMU Polling Example");
Serial.print("[1] Initializing sensor... ");
while (!imu.begin()) {
Serial.println("Failed, please check device address and connections!");
delay(1000);
}
Serial.println("Success");
delay(1000);
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);
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);
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;
// Read 6-axis data and output it
if (imu.get6dofData(&accel, &gyro)) {
// Accelerometer data
Serial.println("--- Accelerometer Data (g) ---");
Serial.print("X: "); Serial.println(accel.x, 3);
Serial.print("Y: "); Serial.println(accel.y, 3);
Serial.print("Z: "); Serial.println(accel.z, 3);
// // Gyroscope data
Serial.println("--- Gyroscope Data (dps) ---");
Serial.print("X: "); Serial.println(gyro.x, 2);
Serial.print("Y: "); Serial.println(gyro.y, 2);
Serial.print("Z: "); Serial.println(gyro.z, 2);
Serial.println();
} else {
Serial.println("Failed to read data!");
}
delay(500);
}
Result

Was this article helpful?
