Example Code for Arduino-Reading Accumulated Step Count via I2C

Explore how to configure a 6 DOF IMU sensor's pedometer with an ESP32-C5 controller, using I2C to read accumulated step counts. This tutorial is ideal for developing projects in robotics or fitness applications. Follow our step-by-step guide to ensure stable readings by polling once per second.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0692-Accumulated Step Count wiring diagram

Connect the 6 DOF IMU sensor to the ESP32-C5 as shown in the diagram. The main pin connections are:

  • Sensor pin “+” → ESP32-C5 3.3V
  • Sensor pin “-” → ESP32-C5 GND
  • Sensor pin “INT1” → ESP32-C5 GPIO27
  • Sensor I2C pin “SCL” → ESP32-C5 SCL (GPIO10)
  • Sensor I2C pin “SDA” → ESP32-C5 SDA (GPIO9)
  • Sensor DIP switch configuration: Set the communication mode to I2C, and set the I2C address to 0x4A (factory default address)

Sample Code

#include "DFRobot_Multi_DOF_IMU.h"

const uint8_t ADDR = 0x4A;                    


DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel6DOF, &Wire, ADDR);

volatile bool stepDetected  = false;          // Flag set by ISR when interrupt occurs
uint32_t      lastStepCount = 0;               // Stores last step count to detect changes

// Interrupt Service Routine for INT1 pin
void IRAM_ATTR int1ISR() {
  stepDetected = true;
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  Serial.println("\nStep Counter Interrupt Example (ESP32 I2C)");

  // [1] Initialize 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(1000);

  // [2] Set sensor mode to normal
  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);

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

  // [4] Configure INT1 pin to generate interrupt on step counter
  Serial.print("[4] Configuring INT1 step counter interrupt... ");
  while (!imu.setInt(DFRobot_Multi_DOF_IMU::eImuIntPin1,
                     DFRobot_Multi_DOF_IMU::eInt1_2StepCounter)) {
    Serial.println("Failed, please check pin and interrupt configuration!");
    delay(1000);
  }
  Serial.println("Success");
  delay(1000);

  
  Serial.print("[5] Configuring Arduino interrupt pin... ");
  attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
  Serial.println("Success");
  Serial.println("Trigger mode: Rising edge");
  delay(100);

  Serial.println("\nConfiguration complete, starting step counting");
  Serial.println("Tip: Start walking to trigger step counting\n");
  delay(100);
}

void loop() {
  if (stepDetected) {
    stepDetected = false;

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

    // Check if the interrupt is due to step counter
    if (intStatus & INT1_2_INT_STATUS_STEP_COUNTER) {
      uint32_t currentStepCount = imu.getStepCount();

      if (currentStepCount != lastStepCount) {
        Serial.print("Steps: ");
        Serial.println(currentStepCount);
        lastStepCount = currentStepCount;
      }
    } else if (intStatus != 0) {
      Serial.print("Other interrupt: 0x");
      Serial.println(intStatus, HEX);
    }
  }
}

Result

SEN0692- Accumulated Step Count Result

Notes

  • When starting step counting or resuming after being stationary, the device requires a 3–5 step buffer period (approx. 1–3 seconds) to stabilize the signal and validate steps.It is normal for the step count to show 0 or remain unchanged during this period; it will automatically update to the actual step count afterward.
  • Avoid shaking the device frequently (e.g., casual swinging) during the initial startup stage, as this may prolong the buffer period or cause false counts.
  • The device uses an attitude estimation algorithm. Due to the characteristics of the algorithm output, occasional step jumps may appear in the serial print log, but the core step count result remains accurate.

Was this article helpful?

TOP