Bosch Sensortec Community

    Showing results for 
    Search instead for 
    Did you mean: 

    BMA456 Fails after initialization

    BMA456 Fails after initialization


    Hi, First of all let me admit that I am a newbie in embedded firmware. I am trying to write a code for BMA456 using ESP32 and Arduino platform(PlatformIO). I am using latest version of BMA456 Sensor API from the official repo. I am able to read the chip ID (0x16) correctly. But when I try to execute the further steps it's failing. The BMA456 seems to get hang at 



     while (Wire.available() < len) 


    Also I tried to read the status register at 0x2A  and got a value 0b11111 which is not even defined in the datasheet.
    I will attach my code below.
    P.S: I also tried arduino portred library from Seed studio. The link is here. I tried their step counter example and the steps never incremented. 



    #include <Arduino.h>
    #include "bma456.h"
    #include "bma4_common.h"
    //  Function Declaration
    uint16_t init_BMA();
    uint16_t config_acc();
    // Global Variables
    struct bma4_dev bma_global; // Declare an instance of the BMA456 device
    uint16_t rslt = BMA4_OK;
    uint8_t init_seq_status;
    uint16_t int_status;         // Variable to store accel data ready interrupt status
    struct bma4_accel_config accel_conf = {0};
    void setup()
      Serial.println("\n BMA456");
    void loop()
      // read_acc();
    uint16_t init_BMA()
      // Modify Params
      // bma.dev_addr = BMA4_I2C_ADDR_PRIMARY;
      struct bma4_dev bma; // Declare an instance of the BMA456 device
      bma.intf = BMA4_I2C_INTF;
      bma.bus_read = usr_i2c_r;
      bma.bus_write = usr_i2c_w;
      bma.delay_us = usr_delay;
      bma.read_write_len = 8;
      bma.resolution = 12;
      bma.feature_len = BMA456_FEATURE_SIZE;
      /* a. Reading the chip id. */
      uint16_t rslt = BMA4_OK;
      rslt |= bma456_init(&bma);
      Serial.println(rslt, HEX);
      bma4_error_codes_print_result("bma456_init status", rslt);
      bma4_set_command_register(0xB6, &bma); // reset device  
      /* b. Performing initialization sequence. 
           c. Checking the correct status of the initialization sequence.
      rslt |= bma456_write_config_file(&bma);
      bma4_error_codes_print_result("bma456_write_config status", rslt);
      bma_global = bma;
      return rslt;
    uint16_t config_acc()
      struct bma4_dev bma; // Declare an instance of the BMA456 device
      bma = bma_global;
      bma4_set_accel_enable(BMA4_ENABLE, &bma);
      bma4_error_codes_print_result("bma4_set_accel_enable status", rslt);
      /* Assign the desired settings */
      accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
      accel_conf.range = BMA4_ACCEL_RANGE_2G;
      accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
      /* Set the configuration */
      rslt |= bma4_set_accel_config(&accel_conf, &bma);
      bma_global = bma;
      return rslt;




    i2c_read functipon



    int8_t usr_i2c_r(uint8_t reg_addr, uint8_t *read_data, uint32_t len, void *intf_ptr)
      Wire.requestFrom(dev_addr, len);
      while (Wire.available() < len) 
      for (int i = 0; i < len; i++)
        read_data[i++] =;
      return 0;




    P.S 2: I am using the Serial.print() for debugging

    8 REPLIES 8

    Community Moderator
    Community Moderator

    Hello nafihahmd,

    1.The address should be shifted like this in I2C read or write function.
    uint8_t dev_addr = *(uint8_t*)intf_ptr;
    uint8_t DevAddress = dev_addr << 1;

    BMA456 I2C read.png

    2. Could you find any error return init_BMA()?

    3.  You should enable step counter before you read step value.

    rslt = bma456_feature_enable(BMA456_STEP_CNTR, BMA4_ENABLE, &bma);
    bma4_error_codes_print_result("bma456_feature_enable status", rslt);


    BSTRobin, Thank you for the fast response.

    1. I am able to read the chip ID correctly using the aforementioned i2c read function. Doesn't that mean the communication is carried out properly ? I have successfully operated other i2c sensors with this same programming logic, that's why I am asking.

    2. The code is not executing to the point where it will return a value (It is getting stuck in between). So I am unable to get the return value init_BMA().


    3. Regarding step counter I was using Seeed-Studio/Seeed_BMA456 library example. So everything is done automatically by the library including enabling the step counter. But stil there was no output


    Also I carried out some more investigations into the issue. The Seeed_BMA456 working fine with AtMega328 uC (Arduino Uno). I looked into the Status registrer at 0x2A and the value is '1' meaning successfully initialized. So why is it not working on an ESP32 and working fine on an AVR ATMega328.


    Thanks again for the reply.

    Now I am getting the following error:




    bma456_init status      Error [65535] : Null pointer
    bma456_write_config status      Error [65535] : Null pointer




    What does this mean? 



    I solved the above error by locally defining  instance of the BMA456 device (struct bma4_dev bma;). But now the Init_BMA() is returning 0xFFFC

    Community Moderator
    Community Moderator

    Hello nafihahmd,

    In init_BMA(), your code called bma456_write_config_file() function. You could add printf log to see which code fail in bma456_write_config_file() function.