Example Code for Arduino-Reading Data via UART and Interrupts

This tutorial demonstrates how to configure a 10-axis IMU sensor to trigger a hardware interrupt when data is ready, and read acceleration, angular velocity, and magnetometer data in the interrupt service routine for efficient data acquisition.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0696-UART Wiring Diagram

Connect the 10-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 pin “INT3” → ESP32-C5 GPIO28
  • Sensor pin “INT4” → ESP32-C5 GPIO25
  • 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"
#define IMU_COMM_UART

#undef CALIBRATE_ABSOLUTE_DIFFERENCE
const bool CALCULATE_ALTITUDE = false;

const uint8_t ADDR = 0x4A;

// ESP32-C5 UART1 mapping: RX = GPIO8(D2), TX = GPIO26(D3), for 10DOF sensor
DFRobot_Multi_DOF_IMU_UART imu(DFRobot_Multi_DOF_IMU::eSensorModel10DOF, &Serial1, 9600, ADDR, /*rx=*/8, /*tx=*/26);

volatile bool int1DataReady = false;
volatile bool int3DataReady = false;
volatile bool int4DataReady = false;

// INT1 data ready interrupt service routine
void IRAM_ATTR int1ISR() {
  int1DataReady = true;
}

// INT3 data ready interrupt service routine
void IRAM_ATTR int3ISR() {
  int3DataReady = true;
}

// INT4 data ready interrupt service routine
void IRAM_ATTR int4ISR() {
  int4DataReady = true;
}

void setup() {
  // Initialize serial monitor (baud rate 115200)
  Serial.begin(115200);
  while (!Serial) delay(10);

  // [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 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(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 INT3 data ready interrupt
  Serial.print("[6] Configuring INT3 data ready interrupt... ");
  while (!imu.setInt(DFRobot_Multi_DOF_IMU::eImuIntPin3, DFRobot_Multi_DOF_IMU::eInt3DataReady)) {
    Serial.println("Failed, please check pin and interrupt configuration!");
    delay(1000);
  }
  Serial.println("Success");
  delay(500);

  // [7] Configure INT4 data ready interrupt
  Serial.print("[7] Configuring INT4 data ready interrupt... ");
  while (!imu.setInt(DFRobot_Multi_DOF_IMU::eImuIntPin4, DFRobot_Multi_DOF_IMU::eInt4DataReady)) {
    Serial.println("Failed, please check pin and interrupt configuration!");
    delay(1000);
  }
  Serial.println("Success");
  delay(500);

  // [8] Configure ESP32-C5 external interrupt (INT1→GPIO27, INT3→GPIO28, INT4→GPIO25, rising edge trigger)
  Serial.print("[8] Configuring Arduino interrupt pins... ");
  attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
  attachInterrupt(digitalPinToInterrupt(28), int3ISR, RISING);
  attachInterrupt(digitalPinToInterrupt(25), int4ISR, RISING);
  Serial.println("Success");
  Serial.println("Trigger mode: Rising edge");

  // Configuration completion prompt
  Serial.println("\nConfiguration complete, starting data reading");
  delay(100);
}

void loop() {
  if (int1DataReady || int3DataReady || int4DataReady) {
    if (int1DataReady && int3DataReady && int4DataReady) {
      int1DataReady = false;
      int3DataReady = false;
      int4DataReady = false;

      DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro, mag;
      float pressureValue; // Fixed to pressure (Pa)

      if (imu.get10dofData(&accel, &gyro, &mag, &pressureValue, CALCULATE_ALTITUDE)) {
        
        Serial.println("--- New Data ---");
        Serial.print("AccX (g): "); Serial.println(accel.x, 3);
        Serial.print("AccY (g): "); Serial.println(accel.y, 3);
        Serial.print("AccZ (g): "); Serial.println(accel.z, 3);
        Serial.print("GyrX (dps): "); Serial.println(gyro.x, 2);
        Serial.print("GyrY (dps): "); Serial.println(gyro.y, 2);
        Serial.print("GyrZ (dps): "); Serial.println(gyro.z, 2);
        Serial.print("MagX (uT): "); Serial.println(mag.x, 2);
        Serial.print("MagY (uT): "); Serial.println(mag.y, 2);
        Serial.print("MagZ (uT): "); Serial.println(mag.z, 2);
  
        Serial.print("Pressure (Pa): "); 
        Serial.println(pressureValue, 2);
        Serial.println(" ");
      } else {
        Serial.println("Failed to read 10DOF data!");
      }
    }
  }
  delay(200);
}

Result

SEN0696-UART Result

Was this article helpful?

TOP