Bluetooth Low Energy with CircuitPython on a nRF52840 USB Dongle

Eric Pietrowicz
4 min readAug 14, 2019

--

This tutorial outlines how to configure the nRF52840 USB dongle as a circuit python device.

Unfortunately, the dongle’s default bootloader used with the nRF Connect desktop app will not allow you to flash the required bootloader for CircuitPython. Instead, we’ll have to use the nrfjprog command line tool and an external JLink programmer.

This tutorial will focus on using the nRF52840 Development Kit as a JLink programmer for an external board.

Using the P20 header on the development kit, make the connections shown below:

In this case the external board is the nRF52840 USB Dongle.
The power to our target board (USB Dongle) is provided by the VDD pin on connector P20 of the development kit.

Before we begin, we must disable MSD Support on the dev kit’s built in JLink. This causes a conflict with the CDC serial communication. To do this, run J-Link Commander:

Execute the command:

MSDDisable
This can be reverted later by running MSDEnable

Once your hardware connections are made, go to the page below to install the nrfjprog tool developed by Nordic.

This allows you to access the JLink programmer in your command line. Once installed, open up a command prompt and confirm a successful installation by running this command:

nrfjprog --version
The system should respond with the tool and driver version.

You may need to add the nrfjprog tool to system PATH. Tutorials can easily be found on how to do this. This wasn’t necessary with my setup, though.

Erase the contents of your USB Dongle by running the following command in your command prompt:

nrfjprog --family nrf52 --eraseall

You should see this:

If you receive an error, double check your hardware connections. I had my nRF Dev. Kit power selection switch set to VDD.

Once the dongle has been successfully erased, download Adafruit’s PCA10059 nRF52840 Bootloader hex file to a convenient directory. I just stashed it on my desktop. You can find the latest released build here:

In your command line, cd into the directory where your hex file is stored. Run the command shown below:

nrfjprog --program <name of your hex file> --sectoranduicrerase -f nrf52 --reset

You should see something like this:

If the bootloader’s installation was successful, the USB dongle should now come up as the removable disk NRF52BOOT when plugged in.

To configure with CircuitPython, download the latest UF2 file for the PCA10059 here:

Copy/drag the UF2 file to the removable disk. The NRF52BOOT should disconnect, then reconnect after a few seconds with the new name of CIRCUITPY:

Adafruit has built a bunch of libraries for use with CircuitPython. In order to use Bluetooth LE, a library is required from the bundle (make sure to download the 4.x release):

The Mu IDE makes it super easy to upload a test program to the CircuitPython device.

https://codewith.mu/en/download

The IDE should recognize your CircuitPython device. (Check the lower left hand corner of the window when you open Mu, it should say “Detected New Adafruit CircuitPython Device.”) Upload some test code by clicking the Save icon and naming your script “code.py” so it runs at boot. Make sure the save location is set as the root of your removable CircuitPython disk.

(Check out my latest project here!)

Here’s some test code written by Rototron in his helpful tutorial here:

https://www.rototron.info/circuitpython-nrf52840-pi-tutorial/

import board
from pulseio import PWMOut
from adafruit_ble.uart import UARTServer
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket
r = PWMOut(board.LED2_R, duty_cycle=0)
g = PWMOut(board.LED2_G, duty_cycle=0)
b = PWMOut(board.LED2_B, duty_cycle=0)
uart_server = UARTServer()while True:
uart_server.start_advertising()
while not uart_server.connected:
pass
while uart_server.connected:
packet = Packet.from_stream(uart_server)
if isinstance(packet, ColorPacket):
print(packet.color)
dc = [-257*c+65535 for c in packet.color]
r.duty_cycle, g.duty_cycle, b.duty_cycle = dc

Try this script with your new CircuitPython nRF52840 USB Dongle and the color picker tool (connect to CIRCUITPY -> controller -> color picker) in the Adafruit Bluefuit app for iOS or Android.

--

--

Eric Pietrowicz
Eric Pietrowicz

Responses (2)