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

Software Preparation

Wiring Diagram

SEN0692-UART 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

Sample Code

#include "DFRobot_Multi_DOF_IMU.h"

const uint8_t ADDR = 0x4A;  // Device address (may be used to distinguish multiple devices in UART mode)

// Create IMU object using Serial1, RX=8, TX=26
DFRobot_Multi_DOF_IMU_UART imu(DFRobot_Multi_DOF_IMU::eSensorModel6DOF, &Serial1, 9600, ADDR, /*rx*/ 8, /*tx*/ 26);

volatile bool dataReady = false;  // Interrupt flag

// Interrupt service routine
void int1ISR()
{
  dataReady = true;
}

void setup()
{
  Serial.begin(115200);  // Initialize serial for output
  while (!Serial) {
    delay(10);
  }

  Serial.println("\n6DOF IMU Data Ready Interrupt Example (UART, ESP32)");

  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);

  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(1000);

  Serial.print("[6] Configuring Arduino interrupt pin... ");
  // Use GPIO27 (D6) as interrupt input, rising edge trigger
  attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
  Serial.println("Success");
  Serial.println("Trigger mode: Rising edge");

  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;

    // Read interrupt status (only check INT1 pin)
    uint16_t intStatus = imu.getIntStatus(DFRobot_Multi_DOF_IMU::eImuIntPin1);

    if (intStatus & INT1_2_INT_STATUS_DRDY) {  // Data ready flag
      DFRobot_Multi_DOF_IMU::sSensorData_t accel, gyro;
      if (imu.get6dofData(&accel, &gyro)) {    // Read accelerometer and gyroscope simultaneously
        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!");
      }
    }
  }

}

Result

SEN0692-UART Result

Was this article helpful?

TOP