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-axis 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

Sample Code

#include "DFRobot_Multi_DOF_IMU.h"

// Use ESP32 UART communication
#define IMU_COMM_UART

// Altitude calibration (optional, commented out for simplicity)
// #define CALIBRATE_ABSOLUTE_DIFFERENCE
#ifdef CALIBRATE_ABSOLUTE_DIFFERENCE
const bool CALCULATE_ALTITUDE = true;
#else
const bool CALCULATE_ALTITUDE = false;
#endif

const uint8_t ADDR = 0x4A;

DFRobot_Multi_DOF_IMU_UART imu(DFRobot_Multi_DOF_IMU::eSensorModel10DOF, &Serial1, 9600, ADDR, /*rx*/8, /*tx*/26);

// Interrupt flags
volatile bool int1DataReady = false;
volatile bool int3DataReady = false;
volatile bool int4DataReady = false;

// Interrupt service routines
void IRAM_ATTR int1ISR() { int1DataReady = true; }
void IRAM_ATTR int3ISR() { int3DataReady = true; }
void IRAM_ATTR int4ISR() { int4DataReady = true; }

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);
  Serial.println("\n10DOF IMU Data Ready Interrupt Example");

  Serial.print("\n[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(1000);

#if defined(CALIBRATE_ABSOLUTE_DIFFERENCE)
  // Calibrate altitude (reference 540m as example)
  Serial.print("[5] Calibrating altitude (reference 540m)... ");
  imu.calibrateAltitude(540.0);
  delay(1000);
#endif

  Serial.print("[6] 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(1000);

  Serial.print("[7] 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(1000);

  Serial.print("[8] 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(1000);

  Serial.print("[9] Configuring ESP32 external interrupt pins... ");
  // INT1
  attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
  // INT3
  attachInterrupt(digitalPinToInterrupt(28), int3ISR, RISING);
  // INT4
  attachInterrupt(digitalPinToInterrupt(25), int4ISR, RISING);
  Serial.println("Success, trigger mode: rising edge");

  Serial.println("\nConfiguration complete, starting data reading");
  Serial.println("Each sensor read will be printed with labels:");
  delay(100);
}

void loop() {
  if (int1DataReady || int3DataReady || int4DataReady) {
    uint16_t int1Status = imu.getIntStatus(DFRobot_Multi_DOF_IMU::eImuIntPin1);
    uint16_t int3Status = imu.getIntStatus(DFRobot_Multi_DOF_IMU::eImuIntPin3);
    uint16_t int4Status = imu.getIntStatus(DFRobot_Multi_DOF_IMU::eImuIntPin4);

    bool int1IsDRDY = (int1Status & INT1_2_INT_STATUS_DRDY) != 0;
    bool int3IsDRDY = (int3Status & INT3_INT_STATUS_DRDY) != 0;
    bool int4IsDRDY = (int4Status & INT4_INT_STATUS_DRDY) != 0;

    if (int1IsDRDY && int3IsDRDY && int4IsDRDY) {
      int1DataReady = false;
      int3DataReady = false;
      int4DataReady = false;

      DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro, mag;
      float pressureValue;   // Pressure (Pa) or altitude (m), depends on CALCULATE_ALTITUDE

      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);
        if (CALCULATE_ALTITUDE) {
          Serial.print("Altitude (m): "); Serial.println(pressureValue, 2);
        } else {
          Serial.print("Pressure (Pa): "); Serial.println(pressureValue, 2);
        }
        Serial.println(); 
      } else {
        Serial.println("Failed to read 10DOF data!");
      }
    } else {
      if (int1DataReady && !int1IsDRDY) int1DataReady = false;
      if (int3DataReady && !int3IsDRDY) int3DataReady = false;
      if (int4DataReady && !int4IsDRDY) int4DataReady = false;
    }
  }
  delay(200);
}

Result

SEN0696-UART Result

Was this article helpful?

TOP