CircuitPython

Setup the ItsyBitsy nRF52840 with CircuitPython

We'll need to get our board setup so we can run the CircuitPython code. Let's walk through these steps to get the latest version of CircuitPython onto your board. 

The Mu Python Editor

Mu is a simple Python editor that works with Adafruit CircuitPython hardware. It's written in Python and works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in, so you get immediate feedback from your board's serial output! While you can use any text editor with your code, Mu makes it super simple. Instructions for Mu are available here.

Installing or upgrading CircuitPython

You should ensure you have CircuitPython 4.0 or greater on your board. Plug your board in with a known good data + power cable (not the cheesy USB cable that comes with USB power packs, they are power only). You should see a new flash drive pop up.

If the drive is CIRCUITPY, then open the boot_out.txt file to ensure the version number is 4.0 or greater. 

Download: file
Adafruit CircuitPython 5.0.0-beta.3 on 2020-01-08; Adafruit ItsyBitsy nRF52840 Express with nRF52840

If you do not have CircuitPython on your board, you can add it or upgrade via this page on the ItsyBitsy guide.

Download the Adafruit CircuitPython Library Bundle

In order to run the code, we'll need to download a few libraries. Libraries contain code to help interface with hardware a lot easier for us.

Use the ItsyBitsy nRF52840 page on Installing Libraries to get the library that matches the major version of CircuitPython you are using noted above, i.e. 4.x for the versions starting with 4, 5.x for the versions starting with 5, etc.

To run the code for this project, we need the three libraries in the Required Libraries list below. Unzip the library bundle and search for the libraries. Drag and drop the files into a folder named lib on the CIRCUITPY drive (which appears when your board is plugged into your computer via a known good USB cable) if the directory is not already on the ItsyBitsy nRF52840).

Required Libraries 

  • neopixel.mpy
  • adafruit_bluefruit_connect
  • adafruit_ble

Once we have all the files we need, a directory listing will look similar to above as far as files and directories.

Upload Code

Click on the download link below to grab the main code directly from GitHub. Ensure the file is named code.py and drop it onto the CIRCUITPY drive main (root) directory that appears when your ItsyBitsy nRF52840 is plugged into your computer via a known good USB data cable. The code will run properly when all of the files have been uploaded including libraries.

Use any text editor or favorite IDE to modify the code. We suggest using Mu as noted above.

import time
import board
import neopixel
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_bluefruit_connect.color_packet import ColorPacket

# NeoPixel strip data pin
pixel_pin = board.D5

# The number of NeoPixels
num_pixels = 46

# Increase or decrease between 0 and 1 to increase or decrease the brightness of the LEDs
brightness = 0.1

# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
ORDER = neopixel.GRB

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=brightness, auto_write=False,
                           pixel_order=ORDER)
# BLE Setup
ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)

#Wheel function for rainbows
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        r = g = b = 0
    elif pos < 85:
        r = int(pos * 3)
        g = int(255 - pos * 3)
        b = 0
    elif pos < 170:
        pos -= 85
        r = int(255 - pos * 3)
        g = 0
        b = int(pos * 3)
    else:
        pos -= 170
        r = 0
        g = int(pos * 3)
        b = int(255 - pos * 3)
    return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)

#Rainbow Swirl Animation
def rainbow_swirl(wait):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = (i * 256 // num_pixels) + j
            pixels[i] = wheel(pixel_index & 255)
        pixels.show()
        time.sleep(wait)

#Rainbow Fill Animation
def rainbow_fill(wait):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = int(i + j)
            pixels[i] = wheel(pixel_index & 255)
        pixels.show()
        time.sleep(wait)

# List of colors
RED = (255, 0, 0)
ORANGE = (255, 50, 0)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
PURPLE = (100, 0, 255)
YELLOW = (255,230, 0)
BLUE = (0, 0, 255)

while True:
    while ble.connected:
        if uart_service.in_waiting:
            if uart_service.in_waiting:
                packet = Packet.from_stream(uart_service)
            if isinstance(packet, ColorPacket):
                # Set all the pixels to one color and stay there.
                pixels.fill(packet.color)
                pixels.show()
            elif isinstance(packet, ButtonPacket):
                if packet.pressed:
                    if packet.button == ButtonPacket.BUTTON_1:
                        pixels.fill(BLUE)
                        pixels.show()
                    elif packet.button == ButtonPacket.BUTTON_2:
                        pixels.fill(RED)
                        pixels.show()
                    elif packet.button == ButtonPacket.BUTTON_3:
                        rainbow_swirl(0.01)
                        pixels.show()
                    elif packet.button == ButtonPacket.BUTTON_4:
                        rainbow_fill(0.001)
                    elif packet.button == ButtonPacket.DOWN:
                        pixels.fill(BLACK)
                        pixels.show()

Double Check

See the directory listing above and double check that you have all the files listed to make this project function. If any are missing or in an incorrect directory, move them so they're in the right places.

This guide was first published on Feb 12, 2020. It was last updated on Feb 12, 2020.
This page (CircuitPython) was last updated on Jul 17, 2020.