Skip to main content

Basic readings

What this example shows​

Reading the acceleration on all three axes, converted to g, about 50 times a second. This is the library's simplest example and the one the Quick Start uses.

The example code​

Select Board
basicReadings.ino
/**
**************************************************
*
* @file basicReadings.ino
*
* @brief Example showing how to read X, Y, Z acceleration data
* from the Soldered BMA400 Accelerometer breakout over I2C
*
* @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;

void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMA400 Example - Basic Readings");

// 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!");
}

void loop()
{
// Get new measurements from the sensor. This must be called before
// accessing the acceleration data, otherwise it will never update
accelerometer.getSensorData();

// Print acceleration data in g's
Serial.print("Acceleration in g's");
Serial.print("\t");
Serial.print("X: ");
Serial.print(accelerometer.data.accelX, 3);
Serial.print("\t");
Serial.print("Y: ");
Serial.print(accelerometer.data.accelY, 3);
Serial.print("\t");
Serial.print("Z: ");
Serial.println(accelerometer.data.accelZ, 3);

// Print 50x per second
delay(20);
}

Expected result, with the breakout wired to a NULA DeepSleep (IO8 to SDA, IO9 to SCL) and lying flat:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
BMA400 Example - Basic Readings
BMA400 connected!
Acceleration in g's X: 0.007 Y: -0.022 Z: 0.996
Acceleration in g's X: 0.011 Y: -0.025 Z: 1.000
Acceleration in g's X: 0.014 Y: -0.026 Z: 1.004
Acceleration in g's X: 0.016 Y: -0.013 Z: 0.994
Acceleration in g's X: 0.012 Y: -0.015 Z: 1.001

After the two opening lines comes one line per reading, about 50 a second: Acceleration in g's, then X:, Y: and Z:, each axis in g with three decimals, separated by tabs. While the sensor does not answer, the sketch prints Error: BMA400 not connected, check wiring and I2C address! once a second instead of BMA400 connected!.

Functions used​

Soldered_BMA400()returns none

Constructor. 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_t

Starts the sensor over I2C: soft-resets the BMA400, checks its chip ID and puts it in normal power mode. Call Wire.begin() first, as the example does.

ReturnsBMA400_OK (0) on success, a negative error code otherwise. An address other than 0x14 or 0x15 returns BMA400_E_INVALID_CONFIG.

Parameters

TypeNameDescription
uint8_taddressI2C address. BMA400_I2C_ADDRESS_DEFAULT (0x14, the default) or BMA400_I2C_ADDRESS_SECONDARY (0x15).
TwoWire &wirePortI2C bus to use. Defaults to Wire.
int8_t getSensorData(bool sensorTime = false)returns int8_t

Reads a new measurement and converts it to g using the current range. The results land in accelerometer.data: accelX, accelY and accelZ (float, in g). Call it before reading data, otherwise the values never update.

ReturnsBMA400_OK (0) on success, a negative error code otherwise.

Parameters

TypeNameDescription
boolsensorTimeWhen true, also reads the sensor's own time counter into data.sensorTimeMillis, in milliseconds. Defaults to false.

Putting it together​

begin() sits in a while loop, so the sketch keeps retrying once a second until the sensor answers. The library does not change the range or data rate at start-up, so after its reset the BMA400 measures at ±4 g and 200 Hz. Each pass of loop() calls getSensorData() and prints the three fields of accelerometer.data with three decimals.

To use the second address, change the jumper (see Jumpers) and call accelerometer.begin(BMA400_I2C_ADDRESS_SECONDARY).