Bluetooth: Peripheral UART

The Peripheral UART sample demonstrates how to use the Nordic UART Service (NUS). It uses the NUS service to send data back and forth between a UART connection and a Bluetooth® LE connection, emulating a serial port over Bluetooth LE.

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/cpurad 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

PCA10100

nrf52833dk

nrf52833dk/nrf52833

nRF52833 DK (emulating nRF52820)

PCA10100

nrf52833dk

nrf52833dk/nrf52820

nRF21540 DK

PCA10112

nrf21540dk

nrf21540dk/nrf52840

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.

Note

  • The boards nrf52dk/nrf52810, nrf52840dk/nrf52811, and nrf52833dk/nrf52820 only support the Minimal sample variant.

  • When used with Thingy:53, the sample supports the MCUboot bootloader with serial recovery and SMP DFU over Bluetooth. Thingy:53 has no built-in SEGGER chip, so the UART 0 peripheral is not gated to a USB CDC virtual serial port.

  • When used with nRF5340 DK, the sample might support the MCUboot bootloader with serial recovery of the networking core image.

The sample also requires using a compatible application for Testing. You can use the nRF Connect Bluetooth Low Energy or nRF Connect for Mobile applications (or other similar applications, such as nRF Blinky or nRF Toolbox). Using mobile applications for testing requires a smartphone or tablet.

You can also test the application with the Bluetooth: Central UART sample. See the documentation for that sample for detailed instructions.

Note

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

The sample also enables an additional USB CDC ACM class serial port that is used instead of UART 0. Because of that, it uses a separate USB Vendor and Product ID.

Overview

When connected, the sample forwards any data received on the RX pin of the UART 0 peripheral to the Bluetooth LE unit. On Nordic Semiconductor’s development kits, the UART 0 peripheral is typically gated through the SEGGER chip to a USB CDC virtual serial port.

Any data sent from the Bluetooth LE unit is sent out of the UART 0 peripheral’s TX pin.

Note

Thingy:53 uses the second instance of USB CDC ACM class instead of UART 0, because it has no built-in SEGGER chip that could be used to gate UART 0.

Debugging

In this sample, the UART console is used to send and read data over the NUS service. Debug messages are not displayed in this UART console. Instead, they are printed by the RTT logger.

If you want to view the debug messages, follow the procedure in Testing and optimization.

Note

On the Thingy:53, debug logs are provided over the USB CDC ACM class serial port instead of RTT or other logging backends.

For more information about debugging in the nRF Connect SDK, see Debugging an application.

FEM support

You can add support for the nRF21540 front-end module to this sample by using one of the following options, depending on your hardware:

  • Build the sample for one board that contains the nRF21540 FEM, such as nrf21540dk/nrf52840.

  • Manually create a devicetree overlay file that describes how the nRF21540 FEM is connected to the SoC. See Configuring devicetree for different ways of adding the overlay file.

  • Provide nRF21540 FEM capabilities by using a shield, for example the nRF21540 EK shield that is available in the nRF Connect SDK. In this case, build the project for a board connected to the shield you are using with an appropriate variable included in the build command, for example -DSHIELD=nrf21540ek. This variable instructs the build system to append the appropriate devicetree overlay file.

    To build the sample in the nRF Connect for VS Code extension for an nRF52840 DK with the nRF21540 EK attached, add the shield variable in the build configuration’s Extra CMake arguments and rebuild the build configuration. For example: -DSHIELD=nrf21540ek.

    See How to work with build configurations in the nRF Connect for VS Code extension documentation for more information.

    See Programming nRF21540 EK for information about how to program when you are using a board with a network core, for example the nRF5340 DK.

Each of these options adds the description of the nRF21540 FEM to the devicetree. See Developing with Front-End Modules for more information about FEM in the nRF Connect SDK.

To add support for other front-end modules, add the respective devicetree file entries to the board devicetree file or the devicetree overlay file.

Minimal sample variant

You can build the sample with a minimum configuration as a demonstration of how to reduce code size and RAM usage. This variant is available for resource-constrained boards.

See Activating sample extensions for details.

USB CDC ACM extension

For the boards with the USB device peripheral, you can build the sample with support for the USB CDC ACM class serial port instead of the physical UART. This build uses the sample-specific UART async adapter module that acts as a bridge between USB CDC ACM and Zephyr’s UART asynchronous API used by the sample. See Activating sample extensions for details about how to build the sample with this extension using the prj_cdc.conf file.

Async adapter experimental module

The default sample configuration uses the UART async API. The sample uses the UART async adapter library to communicate with the USB CDC ACM driver. This is needed because the USB CDC ACM implementation provides only the interrupt interface.

To use the library, set the CONFIG_UART_ASYNC_ADAPTER Kconfig option to y.

MCUboot with serial recovery of the networking core image

For the nrf5340dk/nrf5340/cpuapp, it is possible to enable serial recovery of the network core while multi-image update is not enabled in the MCUboot. See Activating sample extensions for details on how to build the sample with this feature using the nrf5340dk_app_sr_net.conf and nrf5340dk_mcuboot_sr_net.conf files.

User interface

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

Development kits

LED 1:

Blinks, toggling on/off every second, when the main loop is running and the device is advertising.

LED 2:

Lit when connected.

Button 1:

Confirm the passkey value that is printed in the debug logs to pair/bond with the other device.

Button 2:

Reject the passkey value that is printed in the debug logs to prevent pairing/bonding with the other device.

Configuration

See Configuring and building for information about how to permanently or temporarily change the configuration.

Configuration options

Check and configure the following configuration options for the sample:

CONFIG_UART_ASYNC_ADAPTER - Enable UART async adapter

Enables asynchronous adapter for UART drives that supports only IRQ interface.

Building and running

This sample can be found under samples/bluetooth/peripheral_uart 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.

Experimental nRF54H20 SoC radio core only build

The sample can also be built in a configuration that allows it to run on the nRF54H20 SoC radio core only. To build the sample with this configuration, use the following command:

west build -b nrf54h20dk/nrf54h20/cpurad

Experimental Bluetooth Low Energy Remote Procedure Call interface

To build the sample with a Bluetooth Low Energy Remote Procedure Call interface, use the following command:

west build samples/bluetooth/peripheral_uart -b board_name --sysbuild -S nordic-bt-rpc -- -DFILE_SUFFIX=bt_rpc

Activating sample extensions

To activate the optional extensions supported by this sample, set EXTRA_CONF_FILE using the respective CMake option in the following manner:

  • For the minimal build variant, set it to prj_minimal.conf.

  • For the USB CDC ACM extension, set it to prj_cdc.conf. Additionally, you need to set DTC_OVERLAY_FILE to the usb.overlay file.

  • For the MCUboot with serial recovery of the networking core image feature, set it to nrf5340dk_app_sr_net.conf. You also need to set the mcuboot_EXTRA_CONF_FILE variant to the nrf5340dk_mcuboot_sr_net.conf file.

For more information about configuration files in the nRF Connect SDK, see Build and configuration system.

Testing

After programming the sample to your development kit, complete the following steps to test the basic functionality:

  1. Connect the device to the computer to access UART 0. If you use a development kit, UART 0 is forwarded as a COM port (Windows) or ttyACM device (Linux) after you connect the development kit over USB. If you use Thingy:53, you must attach the debug board and connect an external USB to UART converter to it.

  2. Connect to the kit with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  3. Reset the kit.

  4. Observe that LED 1 is blinking and the device is advertising under the default name Nordic_UART_Service. You can configure this name using the CONFIG_BT_DEVICE_NAME Kconfig option.

  5. Observe that the text “Starting Nordic UART service example” is printed on the COM listener running on the computer.

Testing with nRF Connect for Mobile

You can test the sample pairing with a mobile device. For this purpose, use nRF Connect for Mobile (or other similar applications, such as nRF Blinky or nRF Toolbox).

To perform the test, complete the following steps:

  1. Connect the device to the computer to access UART 0. If you use a development kit, UART 0 is forwarded as a COM port (Windows) or ttyACM device (Linux) after you connect the development kit over USB. If you use Thingy:53, you must attach the debug board and connect an external USB to UART converter.

  2. Connect to the kit with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  3. Optionally, you can display debug messages. See Debugging for details.

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

  5. Connect to the device using nRF Connect for Mobile. Observe that LED 2 is lit.

  6. Optionally, pair or bond with the device with MITM protection. This requires using the passkey value displayed in debug messages. See Debugging for details on how to access debug messages. To confirm pairing or bonding, press Button 1 on the device and accept the passkey value on the smartphone.

  7. In the application, observe that the services are shown in the connected device.

  8. Select the UART RX characteristic value in nRF Connect for Mobile. You can write to the UART RX and get the text displayed on the COM listener.

  9. Type “0123456789” and tap SEND. Verify that the text “0123456789” is displayed on the COM listener.

  10. To send data from the device to your phone or tablet, enter any text, for example, “Hello”, and press Enter to see it on the COM listener. Observe that a notification is sent to the peer.

  11. Disconnect the device in nRF Connect for Mobile. Observe that LED 2 turns off.

Testing with Bluetooth Low Energy app

If you have an nRF52 Series DK with the Peripheral UART sample and either a dongle or second Nordic Semiconductor development kit that supports Bluetooth Low Energy, you can test the sample on your computer. Use the nRF Connect Bluetooth Low Energy app in nRF Connect for Desktop for testing.

To perform the test, complete the following steps:

  1. Install the nRF Connect Bluetooth Low Energy app in nRF Connect for Desktop.

  2. Connect to your nRF52 Series DK.

  3. Connect the dongle or second development kit to a USB port of your computer.

  4. Open the Bluetooth Low Energy app.

  5. Select the serial port that corresponds to the dongle or the second development kit. Do not select the kit you want to test just yet.

    Note

    If the dongle or the second development kit has not been used with the Bluetooth Low Energy app before, you may be asked to update the J-Link firmware and connectivity firmware on the nRF SoC to continue. When the nRF SoC has been updated with the correct firmware, the nRF Connect Bluetooth Low Energy app finishes connecting to your device over USB. When the connection is established, the device appears in the main view.

  6. Click Start scan.

  7. Find the DK you want to test and click the corresponding Connect button.

    The default name for the Peripheral UART sample is Nordic_UART_Service.

  8. Select the Universal Asynchronous Receiver/Transmitter (UART) RX characteristic value.

  9. Write 30 31 32 33 34 35 36 37 38 39 (the hexadecimal value for the string “0123456789”) and click Write.

    The data is transmitted over Bluetooth LE from the app to the DK that runs the Peripheral UART sample. The terminal emulator connected to the DK then displays "0123456789".

  10. In the terminal emulator, enter any text, for example Hello.

    The data is transmitted to the DK that runs the Peripheral UART sample. The UART TX characteristic displayed in the Bluetooth Low Energy app changes to the corresponding ASCII value. For example, the value for Hello is 48 65 6C 6C 6F.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr/types.h

  • boards/arm/nrf*/board.h

  • Kernel Services:

    • include/kernel.h

  • Peripherals:

    • include/gpio.h

    • include/uart.h

  • API:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/gatt.h

    • include/bluetooth/hci.h

    • include/bluetooth/uuid.h

The sample also uses the following secure firmware component: