Bluetooth: Peripheral LBS

The peripheral LBS sample demonstrates how to use the LED Button Service (LBS).

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

Thingy:53

PCA20053

thingy53

thingy53/nrf5340/cpuapp/ns thingy53/nrf5340/cpuapp

nRF54L15 PDK

PCA10156

nrf54l15pdk

nrf54l15pdk/nrf54l15/cpuapp

nRF54L15 DK

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l15/cpuapp

nRF54H20 DK

PCA10175

nrf54h20dk

nrf54h20dk/nrf54h20/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp/ns nrf5340dk/nrf5340/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52 DK (emulating nRF52810)

PCA10040

nrf52dk

nrf52dk/nrf52810

nRF52 DK (emulating nRF52805)

PCA10040

nrf52dk

nrf52dk/nrf52805

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

nRF52840 DK (emulating nRF52811)

PCA10056

nrf52840dk

nrf52840dk/nrf52811

nRF52833 DK (emulating nRF52820)

PCA10100

nrf52833dk

nrf52833dk/nrf52820

When built for a board target with the */ns variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample also requires a smartphone or tablet running a compatible mobile application. The Testing instructions refer to nRF Connect for Mobile, but you can also use other similar applications (for example, nRF Blinky or nRF Toolbox).

Note

If you build this application for Thingy:53, it enables additional features. See Application guide for Thingy:53 for details.

Overview

You can use the sample to transmit the button state from your development kit to another device.

When connected, the sample sends the state of Button 1 on the development kit to the connected device, such as a phone or tablet. The mobile application on the device can display the received button state and control the state of LED 3 on the development kit.

You can also use this sample to control the color of the RGB LED on the nRF52840 Dongle or Thingy:53.

User interface

The user interface of the sample depends on the hardware platform you are using.

LED 1:

Blinks when the main loop is running (that is, the device is advertising) with a period of two seconds, duty cycle 50%.

LED 2:

Lit when the development kit is connected.

LED 3:

Lit when the development kit is controlled remotely from the connected device.

Button 1:

Send a notification with the button state: “pressed” or “released”.

Building and running

This sample can be found under samples/bluetooth/peripheral_lbs in the nRF Connect SDK folder structure.

When built as firmware image for a board target with the */ns variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Note

Programming the nRF54H20 SoC can sometimes fail due to conflicts in the resource configuration. This can happen if, for example, an application programmed to the nRF54H20 SoC configured the UICRs for one or more cores in a way that is incompatible with the configuration required by the application you are trying to program on the SoC.

To fix this error and erase the UICR for the application core, run the following command:

nrfutil device recover --core Application

If your sample also uses the radio core, you must also erase the UICRs for the radio core. To erase the UICR for the radio core, run the following command:

nrfutil device recover --core Network

For more information on the command, run:

nrfutil device recover --help

You can then run west flash to program your application.

Minimal build

You can build the sample with a minimum configuration as a demonstration of how to reduce code size and RAM usage, using the -DFILE_SUFFIX=minimal flag in your build.

See Providing CMake options for instructions on how to add this option to your build. For example, when building on the command line, you can add the option as follows:

west build samples/bluetooth/peripheral_lbs -- -DFILE_SUFFIX=minimal

Testing

After programming the sample to your dongle or development kit, one of the LEDs starts blinking to indicate that the advertising loop is active (see User interface for details).

To test the sample using the nRF Connect for Mobile mobile application, complete the following steps:

  1. Install and start the nRF Connect for Mobile application on your smartphone or tablet.

  2. Power on the development kit or insert your dongle into the USB port.

  3. Connect to the device from the application. The device is advertising as Nordic_LBS. The services of the connected device are shown.

  4. In Nordic LED Button Service, enable notifications for the Button characteristic.

  5. Press Button 1 on the device.

  6. Observe that notifications with the following values are displayed:

    • Button released when Button 1 is released.

    • Button pressed when Button 1 is pressed.

  7. Write the following values to the LED characteristic in the Nordic LED Button Service. Depending on the hardware platform, this produces results described in the table.

    Hardware platform

    Value

    Effect

    nRF52 and nRF53 DKs

    OFF

    Switch the LED 3 off.

    ON

    Switch the LED 3 on.

    nRF52840 Dongle

    OFF

    Switch the green channel of the RGB LED off.

    ON

    Switch the green channel of the RGB LED on.

    Thingy:53

    OFF

    Switch the blue channel of the RGB LED off.

    ON

    Switch the blue channel of the RGB LED on.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr/types.h

  • lib/libc/minimal/include/errno.h

  • include/sys/printk.h

  • include/sys/byteorder.h

  • GPIO Interface

  • API:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/hci.h

    • include/bluetooth/conn.h

    • include/bluetooth/uuid.h

    • include/bluetooth/gatt.h

The sample also uses the following secure firmware component: