Using CircuitPython with Swan
CircuitPython is an open-source derivative of the MicroPython programming language and is primarily supported by Adafruit.
Prerequisites
-
Download the latest version of the Swan's UF2 bootloader from the adafruit/tinyuf2 repository releases page. Expand the full list of assets under the current release and search for
"swan"to find the appropriate file.
-
Download the latest version of CircuitPython from Adafruit's CircuitPython page for Swan. (You only need the
.uf2file.)
-
If you haven't already, install the device firmware upgrade utility: dfu-util.
Installation
-
Connect the Swan's Micro USB port to your computer with a USB cable.
-
Put your Swan into "boot" mode by holding down the
BOOTbutton, pressing and releasing theRESETbutton, then releasing theBOOTbutton.
-
Unzip your
tiny-uf2-swan_r5-[version].zipfile. You'll need the extracted.binfile for the next step. -
In a terminal window, run the following command to flash the bootloader files to your Swan. Note that you may to need to navigate directories first, and you will likely need to change the file name at the end of the command to include the appropriate version number.
dfu-util -s 0x8000000:leave -a 0 -D tinyuf2-swan_r5.bin -
In a few seconds, a new drive called
SWANBOOTwill appear.note
If you have previously installed the bootloader but don't see the
SWANBOOTdrive appear, press theRESETbutton twice (fairly quickly) to mount the drive. -
Drag-and-drop the previously downloaded
adafruit-circuitpython-swan_r5.uf2file onto theSWANBOOTdrive. Your Swan will quickly reset and theCIRCUITPYdrive will appear.
Choosing an IDE
The beauty of working with CircuitPython on the Swan is that you interact
directly with the source Python files on the CIRCUITPY drive. Virtually any
IDE or text editor can be used to add, edit, and delete these files.
However, if you want to interact with the on-device REPL or view terminal output, you'll want to use a tool specific to CircuitPython development. Traditional Python editors such as Mu and Thonny are great for programming CircuitPython on the Swan. Likewise, VS Code with the CircuitPython extension is another popular option.
Blink the Onboard LED
-
Open the
code.pyfile in theCIRCUITPYdrive using your preferred editor. -
Paste in the following code and save the updated
code.pyfile.import board import digitalio import time led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5) -
Depending on your IDE or editor, you may have to reset the device to see the changes. Enjoy your blinking LED!