Basic readings
What this example shows
Reading the acceleration on all three axes, converted to g, in a loop. This is the module's simplest example and the one the Quick Start uses.
The example creates I2C bus 0 with SCL on GPIO 9 and SDA on GPIO 8. Change these pins to the ones you wired.
The example code
# FILE: bma400-basicReadings.py
# AUTHOR: Soldered Electronics
# BRIEF: Reads X, Y, Z acceleration from the BMA400 sensor over I2C
# WORKS WITH: BMA400 Accelerometer breakout: www.solde.red/333419
# LAST UPDATED: 2026-09-17
from bma400 import BMA400, BMA400_OK
from machine import I2C, Pin
import time
# Change these to match how your board wires I2C
i2c = I2C(0, scl=Pin(9), sda=Pin(8))
sensor = BMA400(i2c)
if sensor.status != BMA400_OK:
raise Exception("Failed to initialize BMA400: " + sensor.status_string())
print("BMA400 connected!")
while True:
if sensor.get_sensor_data() == BMA400_OK:
print(
"X: {:.3f} g\tY: {:.3f} g\tZ: {:.3f} g".format(
sensor.data.accel_x, sensor.data.accel_y, sensor.data.accel_z
)
)
else:
print("Failed to read acceleration:", sensor.status_string())
time.sleep_ms(20)
🔗Open the complete example
bma400-basicReadings.py at commit 484a5be
This example also uses machine.I2C
(MicroPython reference)
and machine.Pin
(MicroPython reference),
which come with the MicroPython firmware and are not part of the bma400
module.
Expected result, with the breakout wired to a NULA DeepSleep (IO8 to SDA, IO9
to SCL) and lying flat:
BMA400 connected!
X: 0.008 g Y: -0.028 g Z: 1.003 g
X: 0.020 g Y: -0.018 g Z: 1.006 g
X: 0.018 g Y: -0.009 g Z: 1.003 g
X: 0.014 g Y: -0.018 g Z: 1.009 g
X: 0.008 g Y: -0.021 g Z: 1.001 g
After the first line comes one line per reading, about every 20 ms: X:, Y:
and Z:, each in g with three decimals and followed by g, separated by tabs.
If a read fails, the line reads Failed to read acceleration: and the reason
instead.
Functions used
BMA400(i2c=None, address=BMA400_I2C_ADDRESS_SDO_LOW)returns BMA400Constructor. Soft-resets the sensor, checks its chip ID (0x90) and puts it in normal power mode. Raises an exception when the sensor does not answer or another chip answers. Without an i2c object it uses I2C(0, scl=Pin(22), sda=Pin(21)) when the firmware reports itself as esp32, esp8266 or one older Soldered board firmware, and raises an exception on any other board.
ReturnsThe sensor object.
Parameters
| Type | Name | Description |
|---|---|---|
I2C | i2c | An initialized machine.I2C object. Optional only on the boards the library detects (see the constructor above). |
int | address | I2C address. BMA400_I2C_ADDRESS_SDO_LOW (0x14, the default) or BMA400_I2C_ADDRESS_SDO_HIGH (0x15). |
get_sensor_data(sensor_time=False)returns intReads a new measurement, converts it to g using the current range and stores it in sensor.data.accel_x, sensor.data.accel_y and sensor.data.accel_z.
ReturnsBMA400_OK (0) on success, an error code otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bool | sensor_time | When True, also reads the sensor's time counter into sensor.data.sensor_time_ms. Defaults to False. |
status_string()returns strDescribes the current sensor.status code, for example Communication failure or Sensor not found.
ReturnsThe description, or an empty string when the status is BMA400_OK.
Putting it together
The constructor already raises an exception when the sensor cannot be reached,
so the sensor.status check after it is a second guard. The module 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 the loop calls get_sensor_data()
and prints the three axes from sensor.data when it returns BMA400_OK.
For a board set to 0x15, create the sensor with
BMA400(i2c, address=BMA400_I2C_ADDRESS_SDO_HIGH) and import that constant
from bma400.