> For the complete documentation index, see [llms.txt](https://alfredo-reyes-montero.gitbook.io/iot-bosch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alfredo-reyes-montero.gitbook.io/iot-bosch/devices/bosch-xdk-110/hardware/sensors/accelerometer/c.md).

# C

The next code would help to understand more about this sensor, and how we can retrieve information.

First we need to import the libraries to use this feature on the XDK.

XdkSensorHandle.h

```
/************************************************************
*   AccelerationSensor.c
************************************************************/

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

#include "XdkSensorHandle.h"
```

We are going to use two functions, that are going to help us to read the value of the accelerometer.

Accelerometer\_readXyzGValue()

Accelerometer\_readXyzLsValue()

Both functions will help us to provide data but with different unit. The first function provides data that is measured in earth acceleration g. Additionally the physical data is stored in 32 bit integers and therefore measured in mili g.

The second functions provides the analog sensor data converted into its raw digital.

```
/* own header files */

// Function that read and print the sensor data of the BMA280 to the console of the XDK-Workbench
static void readAccelerometer(xTimerHandle xTimer)
{
    (void) xTimer;

    Retcode_T returnValue = RETCODE_FAILURE;

    /* read and print BMA280 accelerometer data */

    Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};
    memset(&bma280, 0, sizeof(CalibratedAccel_XyzMps2Data_T));

    returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

    if (RETCODE_OK == returnValue) {
        printf("BMA280 Acceleration Data - M/S2\t: %f m/s2\t %f m/s2\t %f m/s2\n\r",
            (float) bma280.xAxisData, (float) bma280.yAxisData, (float) bma280.zAxisData);
    }
}
```

We need to initialized the sensor

```
// Function that initializes the Accelerometer with the BMA280 handler and with additional presettings
static void initAccelerometer(void)
{
    Retcode_T returnValue = RETCODE_FAILURE;
    Retcode_T returnBandwidthValue = RETCODE_FAILURE;
    Retcode_T returnRangeValue = RETCODE_FAILURE;

    /* initialize accelerometer */

    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

    if ( RETCODE_OK != returnValue) {
        printf("BMA280 Accelerometer initialization failed\n\r");
    }

    returnBandwidthValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

    if (RETCODE_OK != returnBandwidthValue) {
        printf("Configuring bandwidth failed \n\r");
    }
    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_2G);

    if (RETCODE_OK != returnRangeValue) {
        printf("Configuring range failed \n\r");
    }
}
```

Finally we declared the main part of the program where we need to call all the function that we previously defined. Also define the timers.

```
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);

    uint32_t timerBlockTime = UINT32_MAX;
    uint32_t oneSecondDelay = UINT32_C(1000);
    uint32_t timerAutoReloadOn = UINT32_C(1);

    xTimerHandle accelerometerHandle = NULL;

    initAccelerometer();

    /* Creation and start of the timer task */
    accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", oneSecondDelay,timerAutoReloadOn, NULL, readAccelerometer);

    xTimerStart(accelerometerHandle,timerBlockTime);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alfredo-reyes-montero.gitbook.io/iot-bosch/devices/bosch-xdk-110/hardware/sensors/accelerometer/c.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
