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
- 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 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 (A0=0,A1=0,factory default address)
Sample Code
#include "DFRobot_Multi_DOF_IMU.h"
#include <Wire.h>
// Only keep I2C communication mode
#define IMU_COMM_I2C
const uint8_t ADDR = 0x4A;
// ESP32-C5 I2C mapping: SDA=GPIO9, SCL=GPIO10, using 6DOF sensor
DFRobot_Multi_DOF_IMU_I2C imu(DFRobot_Multi_DOF_IMU::eSensorModel6DOF, &Wire, ADDR);
volatile bool stepDetected = false;
uint32_t lastStepCount = 0;
// Step counter interrupt service routine
void IRAM_ATTR int1ISR() {
stepDetected = true;
}
void setup() {
// Initialize I2C (ESP32-C5 specified SDA=GPIO9, SCL=GPIO10)
Wire.begin(9, 10);
// 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] Configure INT1 step counter interrupt
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(500);
// [5] Configure ESP32-C5 external interrupt (INT1→GPIO27, rising edge trigger)
Serial.print("[5] Configuring Arduino interrupt pin... ");
attachInterrupt(digitalPinToInterrupt(27), int1ISR, RISING);
Serial.println("Success");
Serial.println("Trigger mode: Rising edge");
delay(100);
// Configuration complete prompt (exactly the same format as the image)
Serial.println("\nConfiguration complete, starting step counting");
Serial.println("Tip: Start walking to trigger step counting\n");
}
void loop() {
if (stepDetected) {
stepDetected = false;
uint32_t currentStepCount = imu.getStepCount();
if (currentStepCount != lastStepCount) {
// Print format strictly matches the image: "Steps: X"
Serial.print("Steps: ");
Serial.println(currentStepCount);
lastStepCount = currentStepCount;
}
}
delay(200);
}
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?
