Skip to content

Accelerometer

Description

BMA253 is a digital, triaxial acceleration sensor. Communication is carried over I2C interface. Two independently programmed interrupt lines provide various motion triggers and asynchronous data readout capability.

Part details

Manufacturer: Bosch Sensortec GmbH

Part number: BMA253

Datasheet: link

Code example

Compiling example

This example is based on Mbed OS and requires a simple setup. For full description check Code Setup page.

BMA253 driver

Mbed OS driver for SensorIO can be found here: BMA253 on Github.

Code

1. Latest data

In this example IRQ line is used to provide callback with most recent acceleration data. Please mind that sensor's callback is executed in the context of sensor's own thread.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "I2C.h"
#include "Serial.h"
#include "InterruptIn.h"
#include "DigitalOut.h"
#include "bma253.h"
#include "mbed_wait_api.h"

mbed::Serial console(USBTX, USBRX, 115200);
events::EventQueue scheduler;

constexpr uint8_t BMA253_I2C_ADDRESS = 0b0011000 << 1;

mbed::InterruptIn bma253Irq1(ACCEL_INT1);
mbed::InterruptIn bma253Irq2(ACCEL_INT2);
mbed::I2C iic(SENSORS_SDA, SENSORS_SCL);

Bma253 sensor(iic,
              BMA253_I2C_ADDRESS,
              {&bma253Irq1, &bma253Irq2});

// executor in this context
void measDone(Bma253::Output& data)
{
    console.printf("Accelerometer output:\r\n"
                   "x = %f mg [new: %d]\r\n"
                   "y = %f mg [new: %d]\r\n"
                   "z = %f mg [new: %d]\r\n"
                   "temp = %f C\r\n",
                   data.x,
                   data.newData[0],
                   data.y,
                   data.newData[1],
                   data.z,
                   data.newData[2],
                   data.temp);
}

// callback from driver in separate execution context
void dataCallback(Bma253::Output data)
{
    // not necessary if moving to scheduler execution context is not needed
    scheduler.call(measDone, data);
}

int main(int argc, char **argv)
{
    console.printf("This is SensorIO\r\n");

    // enable sensor and configure interrupt to trigger on a new data event
    Bma253::Config accConfig;
    accConfig.attachCallback(Bma253::IrqLine::LINE_1, &dataCallback)
             .enableEvent(Bma253::IrqLine::LINE_1, Bma253::Event::NEW_DATA);

    const bool configOk = sensor.setConfig(accConfig);

    if(configOk)
    {
        console.printf("Sensor configured correctly\n");
        sensor.enable();
    }
    else
    {
        console.printf("Sensor configuration failed\n");
    }

    // running event loop
    scheduler.dispatch_forever();
}

Example main.cpp file can be downloaded from here.

2. Tap detection

ESD or short-circuit damage possible!

This example requires touching the board. To avoid damage it has to be done with keeping in mind ESD protection rules. Avoid touching any conductive parts or elements. It is advised to 'tap' only plastic and safe parts of the board, like mikroBUS black plastic pin port.

In this example IRQ line is used to provide data when tap is detected.

Please change sensor config code lines from main function in previous example to:

1
2
3
4
Bma253::Config accConfig;
accConfig.bw = Bma253::FilterBandwidth::BW_500_Hz;
accConfig.attachCallback(Bma253::IrqLine::LINE_1, &dataCallback)
         .enableEvent(Bma253::IrqLine::LINE_1, Bma253::Event::SINGLE_TAP);

SINGLE_TAP config enables tap detection event.
FilterBandwidth::BW_500_Hz is a non-default setting for filtering acceleration data used for tap detection. Higher bandwidth is desirable for detecting dynamic events

Try taping gently safe parts on the board and observe output.