Example Code for Arduino-Reading Accumulated Step Count via I2C
This tutorial demonstrates how to configure the built-in hardware pedometer of the BMI323 six-axis sensor using the ESP32-C5 microcontroller, and stably read the accumulated step count by polling once per second.
Hardware Preparation
- DFR1222 FireBeetle 2 ESP32-C5 ×1
- SEN0693 Fermion: BMI323 6 DOF IMU Sensor ×1
Software Preparation
-
Download and install Arduino IDE: Download Arduino IDE
-
Download and install the DFRobot_BMI323 library: Download the DFRobot_BMI323 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 connections are:
- Sensor pin “3V3” → ESP32-C5 3.3V
- Sensor pin “GND” → ESP32-C5 GND
- Sensor pin “INT1” → ESP32-C5 GPIO27
- Sensor I2C pin “SCL” → ESP32-C5 SCL (default GPIO10)
- Sensor I2C pin “SDA” → ESP32-C5 SDA (default GPIO9)
- I2C address pad configuration: Leave the pad open, the I2C address will be 0x69 (factory default mode)
Sample Code
#include "DFRobot_BMI323.h"
#define BMI323_I2C_ADDR 0x69
DFRobot_BMI323 bmi323(&Wire, BMI323_I2C_ADDR);
volatile bool gStepReady = false;
// ESP32 interrupt function must be placed in IRAM
void IRAM_ATTR interrupt() {
gStepReady = true;
}
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10); // Wait for serial initialization
}
Serial.println("BMI323 Step Counter Demo");
Serial.println("========================");
// Initialize BMI323, retry on failure
while (!bmi323.begin()) {
Serial.println("I2C init failed, retry in 1s");
delay(1000);
}
// Configure accelerometer: 50Hz sampling rate, ±8g range, normal mode
bmi323.configAccel(bmi323.eAccelODR50Hz, bmi323.eAccelRange8G, bmi323.eAccelModeNormal);
// Enable step counter interrupt on INT1 pin
if (!bmi323.enableStepCounterInt(bmi323.eINT1)) {
Serial.println("Enable step counter interrupt failed!");
while (1) delay(1000);
}
// Connect BMI323 INT1 to ESP32 GPIO 27 (can be modified as needed)
attachInterrupt(digitalPinToInterrupt(27), interrupt, RISING);
/* Correspondence table of AVR series Arduino interrupt pins and terminal numbers
* ---------------------------------------------------------------------------------------
* | | Digital Pin | 2 | 3 | |
* | Uno, Nano, Mini, and other 328-based boards |--------------------------------|
* | | Interrupt No| 0 | 1 | |
* |-------------------------------------------------------------------------------------|
* | | Pin | 2 | 3 | 21 | 20 | 19 | 18 |
* | Mega2560 |--------------------------------------------|
* | | Interrupt No| 0 | 1 | 2 | 3 | 4 | 5 |
* |-------------------------------------------------------------------------------------|
* | | Pin | 3 | 2 | 0 | 1 | 7 | |
* | Leonardo and other 32u4-based boards |--------------------------------------------|
* | | Interrupt No| 0 | 1 | 2 | 3 | 4 | |
* |--------------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------------------------------------------------------------------
* Correspondence table of micro:bit interrupt pins and terminal numbers
* ---------------------------------------------------------------------------------------------------------------------------------------------
* | micro:bit | Digital Pin | P0-P20 can be used as external interrupt |
* | (When used as an external interrupt, |---------------------------------------------------------------------------------------------|
* | no need to set it to input mode with pinMode)| Interrupt No| Interrupt number is the pin's digital value, e.g., P0 interrupt number 0, P1 is 1 |
* |-------------------------------------------------------------------------------------------------------------------------------------------|
*/
Serial.println("Connect BMI323 INT1 to the defined MCU pin.");
Serial.println("Every step will trigger an interrupt and update the counter.\n");
}
void loop() {
if (gStepReady) {
gStepReady = false;
uint16_t status = bmi323.getIntStatus();
if (status & BMI3_INT_STATUS_STEP_DETECTOR) {
uint16_t steps = 0;
if (bmi323.readStepCounter(&steps) == BMI3_OK) {
Serial.print("Steps: ");
Serial.println(steps);
}
}
}
}
Result

Was this article helpful?
