Python is a very useful language thanks to its simplicity, powerful capabilities, and cross-platform nature. MicroPython, however, takes the best bits of Python and puts them on a microcontroller. In this How-To, we learn how to get MicroPython running on an Adafruit Circuit Playground Express (CPE), how to put programs onto the CPE, and how to send Python commands to the CPE over a serial connection.
Normally in these articles I start off by describing the product being used and then get straight into the actual how-to. However, this article will instead give a nod to the designers of the Adafruit Circuit Playground Express because of a really fantastic feature that has been built into the board. In its default state (before being flashed with MicroPython firmware), when the CPE is plugged into a USB slot, it appears as a generic USB to serial COM port. This means that you can communicate with it via a serial port application which is nice and easy but is something that is included in most other products. However, when you press the RESET button the device goes into development mode which allows for flashing new firmware. When this happens the device is seen as a USB flash disk, allowing for new firmware to be dragged in without having to do complex programming measures over a command line—absolutely fantastic!
Before we go plugging the board into our machine we first need to download some files. The first file you need to download is the Adafruit version of MicroPython (a port) so visit the link below and then select the latest version. The file that you will want will be a .uf2 file such as adafruit-circuitpython-circuitplayground_express-2.2.3.uf2
The second file you need to download is the library file. Make sure to download the latest release.
The next step involves flashing the CPE which is incredibly easy to do. Now, how you do this will vary ever so slightly depending on whether this is the first time you have connected your CPE to a computer or not. Start by connecting your CPE to the USB port of your computer and if all goes to plan there should be a strange sounding noise and then the lights should light up one by one in a rainbow fashion. At this point, press the RESET button once and all the lights should turn green. If they do not, try pressing the RESET button a second time.
Once you get all the lights to turn green you have gotten your CPE to enter developer mode. This will allow you to change the firmware that is running on the system. Changing the firmware is done by opening the CPE in windows as you would with any USB flash drive. If the USB drive does not show up, simply navigate to This PC > CPLAYBOOT.
With the flash drive loaded, drag in the CPE MicroPython file (shown as the extension .uf2) that we downloaded earlier. As soon as the file has transferred the file explorer will close, the onboard LED will glow bright yellow, and a new window will open up.
This window should open as soon as the firmware has been transferred
With the firmware flashed it’s now time to create a new folder in the USB drive that shows up (called CIRCUITPY) and the folder needs to be named “lib”. Open this lib folder, open the downloaded library zip file, and then copy the adafruit_circuitplayground folder to lib. DO NOT EXTRACT ALL THE FILES IN THE LIB FILE! If you do, you will run out of memory!
With that done you are ready to use MicroPython on the CPE!
Not only did Adafruit do a fantastic job of getting the CPE to work with computers and enabling easy firmware updates, they also made both the flash disk AND serial USB COM port work at the same time! This means that you can open a serial terminal window to communicate with MicroPython WHILE having the USB disk open.
So for now, we will open RealTerm, set our display to be either ANSI or ANSI-VT100, open the COM port that CPE is on, set the baud rate to 115200, and then open that port! If you do not know what COM port your CPE is on, the easiest way to find out in RealTerm is to look for the larger COM port name. Most systems use COM0 (or COM1) and cannot be accessed, so any other COM ports will be devices that you have inserted. In my case, I have COM0 and COM6 so the CPE is going to be on COM6. Plus, when you open the COM port to the CPE it automatically displays a message, letting you know that you have opened the right port.
Press the “any key” (Simpson reference for those who do not know), to start the REPL which is the Python command line. At this point, we can enter python commands and test them out! For now, let’s print the good old classic “Hello World”.
The last thing we will learn to do is how to put code onto the CPE that the system will run when it boots. When the CPE boots, it will look for a file called code.py and execute any code found in that file. This means that all we have to do is write a python program, save it to a file called code.py, and drag that file to the USB flash drive that is the CPE. In this case I will make a simple python program that will print out “Hello World” ten times. However, instead of putting my code into code.py I will instead put my code into a file called script.py so I can run it at will from the serial prompt. So, start by copying the code below into a text file and save it as script.py TO YOUR DESKTOP!
Then DRAG this file into the CPE flash drive.
In the RealTerm window enter the Python prompt and then execute the command import script.
And that’s all there is to setting up MicroPython on the Circuit Playground Express!