Skip to content

BLE Nordic UART Service

Overview

The BLE Nordic UART Service sample demonstrates using Nordic UART Service to control the RGB LED on nRF52840 Connect Kit over Bluetooth Low Energy.

Tip

adafruit_ble is pre-built into CircuitPython as a frozen module, so that it can be imported in the code directly.

Requirements

Before you start, check that you have the required hardware and software:

Running the code

To run the code, complete the following steps:

  1. Connect nRF52840 Connect Kit to your computer using the USB-C Cable.
  2. Start Mu Editor, click Load to open code.py in the CIRCUITPY drive.
  3. Copy and paste the following code into code.py and click Save:

    CIRCUITPY/code.py
    import digitalio
    import board
    from adafruit_ble import BLERadio
    from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
    from adafruit_ble.services.nordic import UARTService
    
    red_led = digitalio.DigitalInOut(board.RED_LED)
    red_led.direction = digitalio.Direction.OUTPUT
    red_led.value = True
    green_led = digitalio.DigitalInOut(board.GREEN_LED)
    green_led.direction = digitalio.Direction.OUTPUT
    green_led.value = True
    blue_led = digitalio.DigitalInOut(board.BLUE_LED)
    blue_led.direction = digitalio.Direction.OUTPUT
    blue_led.value = True
    
    ble = BLERadio()
    ble.name = "CIRCUITPY NUS"
    uart = UARTService()
    advertisement = ProvideServicesAdvertisement(uart)
    
    while True:
        ble.start_advertising(advertisement)
        while not ble.connected:
            pass
        while ble.connected:
            # Returns b'' if nothing was read.
            one_byte = uart.read(1)
            if one_byte == b'1':
                red_led.value = not red_led.value
            elif one_byte == b'2':
                green_led.value = not green_led.value
            elif one_byte == b'3':
                blue_led.value = not blue_led.value
    
  4. Your code will run as soon as the file is done saving. Start the nRF Toolbox app, tap UART to open the UART application.

  5. Connect to the device with the name CIRCUITPY NUS discovered in the UART application.
  6. Tap the blank buttons to create new commands. The following command assignments are configured with EOL set to LF :

    • - Send 1 to toggle the RED LED.
    • - Send 2 to toggle the GREEN LED.
    • - Send 3 to toggle the BLUE LED.

  7. Tap to toggle the RGB LED. Observe that the RGB LED on the board turns on or off.