Step counter
What this example shows
Counting steps with the BMA400's built-in step counter, which also reports
whether you are walking, running or standing still. The chip does the step
detection and counting itself; it raises INT1 on a step, and your controller
reads the count.
Besides Qwiic or the I2C header pins, wire the breakout's INT1 pin to digital
pin 2 on your controller, or change interruptPin in the sketch to the pin
you use. The pin has to support interrupts.
The example code
/**
**************************************************
*
* @file stepCounter.ino
*
* @brief Example showing how to use the BMA400's built-in step
* counter and activity type detection (running/walking/still)
*
* @link solde.red/333419 : Soldered BMA400 Accelerometer breakout
*
* @authors SparkFun Electronics, modified by Soldered Electronics
***************************************************/
#include <BMA400-SOLDERED.h>
// Create an instance of the BMA400 sensor
Soldered_BMA400 accelerometer;
// Pin connected to the sensor's INT1 pin, change to match your wiring
int interruptPin = 2;
// Flag to know when an interrupt occurs
volatile bool interruptOccurred = false;
void bma400InterruptHandler()
{
interruptOccurred = true;
}
void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMA400 Example - Step Counter");
// Initialize I2C bus
Wire.begin();
// Check if sensor is connected and initialize it.
// Address is optional, defaults to 0x14
while (accelerometer.begin() != BMA400_OK)
{
Serial.println("Error: BMA400 not connected, check wiring and I2C address!");
delay(1000);
}
Serial.println("BMA400 connected!");
// Configure the step counter feature. Step detection and counting is
// handled entirely by the sensor; the count is a 24-bit value that can
// be read at any time
bma400_step_int_conf config = {
.int_chan = BMA400_INT_CHANNEL_1 // Route interrupt to INT1 pin
};
accelerometer.setStepCounterInterrupt(&config);
// Configure INT1 pin to push/pull mode, active high
accelerometer.setInterruptPinMode(BMA400_INT_CHANNEL_1, BMA400_INT_PUSH_PULL_ACTIVE_1);
// Enable the step counter interrupt condition. This must be enabled for
// step counting to work at all, even without using the interrupt pin
accelerometer.enableInterrupt(BMA400_STEP_COUNTER_INT_EN, true);
// Attach the interrupt handler to the microcontroller pin wired to INT1
attachInterrupt(digitalPinToInterrupt(interruptPin), bma400InterruptHandler, RISING);
}
void loop()
{
if (interruptOccurred)
{
interruptOccurred = false;
// Check which interrupt condition triggered
uint16_t interruptStatus = 0;
accelerometer.getInterruptStatus(&interruptStatus);
if (interruptStatus & BMA400_ASSERTED_STEP_INT)
{
// Get total step count and detected activity type
uint32_t stepCount = 0;
uint8_t activityType = 0;
accelerometer.getStepCount(&stepCount, &activityType);
Serial.print("Step detected! Step count: ");
Serial.print(stepCount);
Serial.print("\t");
Serial.print("Detected activity: ");
switch (activityType)
{
case BMA400_RUN_ACT:
Serial.println("Running");
break;
case BMA400_WALK_ACT:
Serial.println("Walking");
break;
case BMA400_STILL_ACT:
Serial.println("Standing still");
break;
default:
Serial.println("Unknown");
break;
}
}
}
}
This example also uses attachInterrupt() and digitalPinToInterrupt()
(Arduino reference),
which come with the Arduino core and are not part of the Soldered BMA400
Accelerometer library.
Expected result:
BMA400 Example - Step Counter
BMA400 connected!
After start-up, each step interrupt prints one line: Step detected! Step count: with the total count, a tab, then Detected activity: followed by
Running, Walking, Standing still or Unknown.
Functions used
Soldered_BMA400()returns noneConstructor. Creates the sensor object; it does not talk to the sensor until you call begin().
ReturnsConstructor; no return value.
int8_t begin(uint8_t address = BMA400_I2C_ADDRESS_DEFAULT, TwoWire &wirePort = Wire)returns int8_tStarts the sensor over I2C: soft-resets the BMA400, checks its chip ID and puts it in normal power mode.
ReturnsBMA400_OK (0) on success, a negative error code otherwise. An address other than 0x14 or 0x15 returns BMA400_E_INVALID_CONFIG.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | address | I2C address. BMA400_I2C_ADDRESS_DEFAULT (0x14, the default) or BMA400_I2C_ADDRESS_SECONDARY (0x15). |
TwoWire & | wirePort | I2C bus to use. Defaults to Wire. |
int8_t setStepCounterInterrupt(bma400_step_int_conf* config)returns int8_tSets which interrupt pin the step counter signals on.
Returns0 on success, negative on failure.
Parameters
| Type | Name | Description |
|---|---|---|
bma400_step_int_conf* | config | Pointer to the configuration. The example sets int_chan to BMA400_INT_CHANNEL_1 for INT1. |
int8_t setInterruptPinMode(bma400_int_chan channel, uint8_t mode)returns int8_tSets an interrupt pin as push-pull or open drain, active high or active low.
Returns0 on success, negative on failure.
Parameters
| Type | Name | Description |
|---|---|---|
bma400_int_chan | channel | The pin to configure, for example BMA400_INT_CHANNEL_1 for INT1. |
uint8_t | mode | BMA400_INT_PUSH_PULL_ACTIVE_0, BMA400_INT_PUSH_PULL_ACTIVE_1 (default), BMA400_INT_OPEN_DRIVE_ACTIVE_0 or BMA400_INT_OPEN_DRIVE_ACTIVE_1. |
int8_t enableInterrupt(bma400_int_type intType, bool enable)returns int8_tTurns one interrupt condition on or off. The step counter only counts while BMA400_STEP_COUNTER_INT_EN is enabled, even if you never use the interrupt pin.
Returns0 on success, negative on failure.
Parameters
| Type | Name | Description |
|---|---|---|
bma400_int_type | intType | The condition, for example BMA400_STEP_COUNTER_INT_EN for the step counter. |
bool | enable | true to enable, false to disable. |
int8_t getInterruptStatus(uint16_t* status)returns int8_tReads which interrupt conditions are asserted. Test the result against flags such as BMA400_ASSERTED_STEP_INT.
Returns0 on success, negative on failure.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t* | status | Receives the status flags, a combination of BMA400_ASSERTED_* values. |
int8_t getStepCount(uint32_t* count, uint8_t* activityType)returns int8_tReads the total step count and the activity the chip detected.
Returns0 on success, negative on failure.
Parameters
| Type | Name | Description |
|---|---|---|
uint32_t* | count | Receives the step count, a 24-bit value. |
uint8_t* | activityType | Receives BMA400_STILL_ACT, BMA400_WALK_ACT or BMA400_RUN_ACT. |
Putting it together
The step counter needs no tuning in this example: setStepCounterInterrupt()
only routes its interrupt to INT1, and enableInterrupt() switches it on.
INT1 is push-pull, active high, so the handler is attached on a RISING edge
and only sets a volatile flag. loop() then checks the status for
BMA400_ASSERTED_STEP_INT and reads the count and activity with
getStepCount().
Because the count lives in the chip, you can also call getStepCount() at any
time without waiting for the interrupt.