CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards. Here is a guide which covers the basics:
Be sure you have the latest CircuitPython for the Feather nRF52840 Express loaded onto your board, as described here.
CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.
Libraries
Plug your FeathernRF52840 Express board into your computer via a USB cable. Please be sure the cable is a good power+data cable so the computer can talk to the Feather board.
A new disk should appear in your computer's file explorer/finder called CIRCUITPY. This is the place we'll copy the code and code library. If you can only get a drive named FTHR840BOOT, load CircuitPython per the Feather guide above.
Create a new directory on the CIRCUITPY drive named lib.
Download the latest CircuitPython driver package to your computer using the green button below. Match the library you get to the version of CircuitPython you are using. Save to your computer's hard drive where you can find it.
With your file explorer/finder, browse to the bundle and open it up. Copy the following folders and files from the library bundle to your CIRCUITPY lib directory you made earlier:
- adafruit_ble
- adafruit_bluefruit_connect
- neopixel.mpy
All of the other necessary libraries are baked into CircuitPython!
Your CIRCUITPY/lib directory should look like the snapshot below.
The code
It starts by setting up the NeoPixel and Bluetooth drivers. The main loop waits for a connection (typically to the bluefruit app). Once it has a connection, it repeatedly listens for a color command. When it receives such a command, the NeoPixels are updated. If the connection is lost, the inner loop exits and the outer one tries to re-establish a connection.
# SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries # # SPDX-License-Identifier: MIT """ Lightbox driver program. Adafruit invests time and resources providing this open source code. Please support Adafruit and open source hardware by purchasing products from Adafruit! Written by Dave Astels for Adafruit Industries Copyright (c) 2018 Adafruit Industries Licensed under the MIT license. All text above must be included in any redistribution. """ 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.color_packet import ColorPacket pixel_pin = board.A0 num_pixels = 20 pixels = neopixel.NeoPixel(pixel_pin, num_pixels) ble = BLERadio() uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) while True: ble.start_advertising(advertisement) while not ble.connected: pass # Now we're connected while ble.connected: if uart.in_waiting: packet = Packet.from_stream(uart) if isinstance(packet, ColorPacket): # Change the NeoPixel color. pixels.fill(packet.color)
Text editor powered by tinymce.