In this recipe, we will show you how to load your first program on the Circuit Playground Express and how to modify the program and reload it. The program will then light one of the ten NeoPixels that come available on the board.
Executing your first program
Getting ready
Once the Circuit Playground Express has had the CircuitPython firmware flashed, you may load Python scripts onto the board and run them.
How to do it...
Let's have a look at how to do this:
- Make sure that the board is connected to your computer with a USB cable and that the CIRCUITPY drive appears.
- Save a text file on the drive with the following contents and name it main.py:
from adafruit_circuitplayground.express import cpx
import time
cpx.pixels[0] = (255, 0, 0) # set first NeoPixel to the color red
time.sleep(60)
- Once the file has been saved, eject the drive, and remove and reconnect the USB cable from the computer.
- The first NeoPixel on the drive should light up with a red color.
- Open the main.py file in your text editor of choice and change the cpx.pixels[0] line to cpx.pixels[1]. Save the file. This change will make the second NeoPixel light up instead of the first.
- Eject the drive, remove, and then reconnect the USB cable to see the change take effect.
How it works...
When the device is turned on it looks for certain files, such as code.py or main.py, that, if found, will be executed as part of the startup process. In this way, you can specify the code you want run when the device is powered on. The script first imports the adafruit_circuitplayground.express library so that it can control the NeoPixels. The first NeoPixel is set to the color red by giving it a set of appropriate RGB values.
Finally, the script will sleep for 60 seconds so that the LED remains lit for one minute before the script ends execution.
There's more...
Now that the board has been loaded with a Python script, it can be disconnected from the computer and have the battery pack attached to it. Once the battery pack is powered on by the script, it should run and light up the selected NeoPixel.
This is a simple way to create portable and inexpensive projects that can have a code running directly from the board with no need for a connected PC and can be powered simply by three AAA batteries.
See also
There are a number of files that CircuitPython looks for when it boots up, which are described at https://learn.adafruit.com/welcome-to-circuitpython?view=all#naming-your-program-file-7-30.