Circuit Python

CPX Circuit Python: Super-quick start (with “Blink” demo code)

Based on the Adafruit tutorial.

  • Connect the CPX (Circuit Playground Express) and make sure it’s ready to be programmed (green lights). Single- or double-click RESET; whichever works. CPLAYBOOT should appear on the desktop.



  • Download Circuit Python for CPX to CPLAYBOOT. If successful, new drive CIRCUITPY will appear.

     .  

  • Now download this sample Python program to CIRCUITPY as code.py.

Here’s what the code looks like:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

The red LED should be blinking.

To go back to MakeCode, double-click RESET and load new program as usual into CPLAYBOOT.

 

Using the Mu editor

  • Download Portamu to Desktop (it’s kinda big).



  • Double-click the downloaded zip file to unpack the editor.



  • Start Portamu.
  • Be sure “Mode” is set to “Adafruit”.
 
  • Click “New”, then cut and paste the following:

    import board
    import digitalio
    import time

    led = digitalio.DigitalInOut(board.D13)
    led.direction = digitalio.Direction.OUTPUT

    while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
  • “Check” before you try to save. Editor is finicky, but that’s good!



  • “Save”, always as “code.pyto CIRCUITPY. (Once you select a file to save to, you can’t change it except by starting a new program.)

You should see the red LED blinking again.

Serial Interface

Now try this code:

import time
from adafruit_circuitplayground.express import cpx

while True:
x, y, z = cpx.acceleration
print((x, y, z))
time.sleep(0.5)

Be sure to Save as code.py into CPLAYBOOT.

Hmm, not much is happening. Click the “Serial” button in the editor.

A window labelledAdafruit CircuitPython REPL” opens at the bottom and you can see the CPX reporting its orientation every half-second.

More sample code

Using interactive Python

  • Click “Serial” in the mu editor if you haven’t already..
  • Click in the “Adafruit CircuitPython REPL” window, type Control-C (yes, “Control”, even on Macs). Then press any key. You will see the prompt for interactive Python.



  • You can use it as a calculator. Try typing in some simple arithmetic.



  • Type Control-D to exit to Console.



Now check out the Circuit Python for CPX guide, with lots of cut and paste code

Online Python for Beginners