Using the Accelerometer of the WSTK Expansion Board#

Description#

Introduction#

EFR32BGXX devices are capable of running many different types of external sensors. This example shows how to use an accelerometer found in the WSTK expansion board. Communicating between the sensor and the radio board is done via SPI. Accelerometer data is sent over UART and Bluetooth notifications. Because not all the EFR32BGXX devices are pin-to-pin compatible, this example may need some modifications to work with your radio board. The example has been created for the BGM121 but it can be ported to other devices easily.

Implementation#

The software has the following tasks:

  • Initialize SPI after the Bluetooth stack.

    This is implemented in spi_init()

    This is the only function that requires changes according to the used device. Pin rerouting locations can be found from EFR32BGxx data sheets. The default settings are the following:

      // USART signal routing: (see your EFR32 data sheet and reference manual USART sections)
      // For example BGM121 module (EFR32BG1)
      // US0/1_CLK: location 11 -> PC8
      // US0/1_CS: location 11 -> PC9
      // US0/1_RX: location 11 -> PC7
      // US0/1_TX: location 11 -> PC6
  • set up a soft timer with a measurement interval

    gecko_cmd_hardware_set_soft_timer(32768, 0, 0);

  • read the sensor register values to get measurements from all three axes.

    This is implemented in BMA280_read_register()

    To learn more about the configurations of the accelerometer, see the Github page and consult the sensor reference manual.

  • send the acceleration measurements by Bluetooth notifications to all subscribed clients and also prints them to UART.

    static void measure_acceleration()
    {
      // For simplicity, we are reading only the 8 most significant bits here.
      uint8_t acceleration_x, acceleration_y, acceleration_z;
    
      // X-Axis in 0x02-0x03
      acceleration_x = BMA280_read_register(USART1, 0x03);
      gecko_cmd_gatt_server_send_characteristic_notification(0xFF, gattdb_x_axis_measured_value, 1, &acceleration_x);
      // Y-Axis in 0x04-0x05
      acceleration_y = BMA280_read_register(USART1, 0x05);
      gecko_cmd_gatt_server_send_characteristic_notification(0xFF, gattdb_y_axis_measured_value, 1, &acceleration_y);
      // Z-Axis in 0x06-0x07
      acceleration_z = BMA280_read_register(USART1, 0x07);
      gecko_cmd_gatt_server_send_characteristic_notification(0xFF, gattdb_z_axis_measured_value, 1, &acceleration_z);
    
      printLog("\nACC X%d, Y%d, Z%d\n", (int8_t)acceleration_x, (int8_t)acceleration_y, (int8_t)acceleration_z);
    }

Setting up#

This code example requires the following hardware:

  • Wireless Starter Kit (WSTK)

  • WSTK Expansion Board

  • Bluetooth Radio Board

To try the attached example, following the steps below:

  1. Create a new SoC - Empty project for your device.

  2. Import the attached gatt.xml file into the GATT configurator. The imported GATT database will include a custom Accelerometer Service (with random 128bit UUID) containing 3 custom characteristics.

Accelerometer CharacteristicsAccelerometer Characteristics

  1. Press Save and then Generate in the GATT Configurator.

  2. Enable printing to UART by changing DEBUG_LEVEL to 1 in app.h.

  3. Disable sleep by setting DISABLE_SLEEP to 1 in app.h.

    EM2 sleep mode causes the UART connection to not work properly and to avoid that we disable sleep mode by setting the sleep flag to zero via this macro.

  4. Replace existing app.c with the one attached here.

  5. Build and flash your project.

Usage#

  1. Attach the expansion board to your WSTK and reset it.

  2. Open a terminal program (such as TeraTerm) and connect to the virtual COM port (JLink CDC UART).

  3. You should see the values measured on the accelerometers. Rotate the board, to see the changes:

    Accelerometer values on serial portAccelerometer values on serial port

  4. Open EFR Connect app on your smart phone.

  5. Connect to the device named Accelero.

  6. Find the very last service (Unknown service) in the GATT database and open it.

  7. Open any of the characteristics and tap Notify to enable receiving notifications.

  8. You should see the values changing.

    Reading accelerometer value in EFR Connect AppReading accelerometer value in EFR Connect App

    Note: the value is displayed as unsigned here so you have to convert it to a signed integer to get a sensible value.

Source#