Example Code for Arduino-Reading Data via UART and Interrupts
Explore how to configure a 6 DOF IMU sensor to trigger a hardware interrupt for real-time data acquisition. Learn to read accelerometer and gyroscope data efficiently in the interrupt service routine.
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. The main connections are as follows:
- Sensor pin “+” → ESP32-C5 3.3V
- Sensor pin “-” → ESP32-C5 GND
- Sensor pin “INT1” → ESP32-C5 GPIO27
- Sensor UART pin “RX” → ESP32-C5 D3 (GPIO26)
- Sensor UART pin “TX” → ESP32-C5 D2 (GPIO8)
- Sensor DIP switch configuration: Set the communication mode to UART, and set the address to 0x4A(A0=0,A1=0)
Sample Code
#include "DFRobot_Multi_DOF_IMU.h"
// Only keep UART communication mode
#define IMU_COMM_UART
const uint8_t ADDR = 0x4A;
// ESP32-C5 UART1 mapping: RX=GPIO8(D2), TX=GPIO26(D3)
DFRobot_Multi_DOF_IMU_UART imu(DFRobot_Multi_DOF_IMU::eSensorModel6DOF, &Serial1, 9600, ADDR, /*rx=*/8, /*tx=*/26);
volatile bool dataReady = false;
// Data ready interrupt service routine
void IRAM_ATTR int1ISR() {
dataReady = true;
}
void setup() {
// Initialize serial monitor (baud rate consistent with the example)
Serial.begin(115200);
while (!Serial) delay(10);
// Print title
Serial.println("\n6DOF IMU Data Ready Interrupt Example (UART, ESP32)");
// 1. Initialize the sensor
Serial.print("\n[1] Initializing sensor... ");
while (!imu.begin()) {
Serial.println("Failed, please check device address and connections!");
delay(1000);
}
Serial.println("Success");
delay(500);
// 2. 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(500);
// 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(500);
// 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(500);
// 5. Configure INT1 data ready interrupt
Serial.print("[5] Configuring INT1 data ready interrupt... ");
while (!imu.setInt(DFRobot_Multi_DOF_IMU::eImuIntPin1, DFRobot_Multi_DOF_IMU::eInt1_2DataReady)) {
Serial.println("Failed, please check pin and interrupt configuration!");
delay(1000);
}
Serial.println("Success");
delay(500);
// 6. Configure ESP32-C5 external interrupt (INT1 → GPIO27)
Serial.print("[6] Configuring Arduino interrupt pin... ");
attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
Serial.println("Success");
Serial.println("Trigger mode: Rising edge");
// Configuration completion prompt
Serial.println("\nConfiguration complete, starting data reading");
Serial.println("AccX(g), AccY(g), AccZ(g), GyrX(dps), GyrY(dps), GyrZ(dps)");
delay(100);
}
void loop() {
if (dataReady) {
dataReady = false;
DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro;
if (imu.get6dofData(&accel, &gyro)) {
// Strictly align with the example print format: Acc retains 3 decimal places, Gyro retains 2 decimal places
Serial.print(accel.x, 3); Serial.print(", ");
Serial.print(accel.y, 3); Serial.print(", ");
Serial.print(accel.z, 3); Serial.print(", ");
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 data!");
}
}
delay(200);
}
Result

Was this article helpful?
