CircuitPython Language Reference Sheet:

Set up the system
File organization: Set up a file folder on your computer:

  1. Make a CircuitPython Programming folder
  2. Inside that folder, make three folders:
  1. CPY UF2
  2. CPY Libraries
  3. Hello

CircuitPlayground device setup: Make it run CircuitPython.

  1. find the latest version
  2. choose the .uf2 file for the correct device
  3. download the file, stick it in the CPY UF2 folder on your computer
  4. press reset to get CPLAYBOOT drive to show
  5. drag and drop to the CPLAYBOOT drive. 
  6. It automatically runs, and the drive becomes CIRCUITPY. 
  7. Create a file named on the CIRCUITPY drive. 
  8. Create a folder named lib on the CIRCUITPY drive. 

Mu Editor:

  1. Download the Mu editor
  2. Install the Mu editor
  3. Put it in Adafruit CircuitPython mode
  4. Press LOAD to load the program from the CIRCUITPY drive into the Mu Editor
  5. Open the Serial panel
  6. Click in the Serial Panel
  7. ctrl-c to cancel a program (and to read the CircuitPython version number)
  8. ctrl-d to restart a program

CircuitPython libraries:

  1. Check the CircuitPython version number (ctrl-c in Mu editor)
  2. Find the latest CircuitPython libraries
  3. Download the set that matches your version number. 
  4. Unzip the downloaded file
  5. Move the downloaded file to the CPY Libraries folder on your computer

Write a program

Write the program that says hello: 

  1. Type print(8)into the Mu Editor
  2. Press SAVE to save the program to the CIRCUITPY drive
    • WARNING: SAVE overwrites the on the CIRCUITPY drive
    • Whatever was before, it is gone.
    • The code runs automatically
  3. To save a copy of your program
    • Open the CircuitPython Programming folder on your computer
    • Create (or open) a folder named Hello inside the CircuitPython Programming folder. 
    • Open the CIRCUITPY drive folder
    • Copy from the CIRCUITPY drive to the Hello folder. 
  4. Type print(“Hello”)in the Mu Editor. 
  5. Press SAVE to save and run the new
  6. Save an updated copy of your new program
    • Copy from the CIRCUITPY drive to the Hello folder. 
    • KEEP BOTH!!!! On an Apple Mac, the latest will be renamed
    • On a windows machine, you may need to manually name the latest version to keep it from overwriting the existing version. 

Keeping a record of your code

General procedure for keeping records of your programs: 

  1. Create (or open) the CircuitPython Programming folder on your computer
  2. Create a file folder for each program you write, with a file name that tells you what you intend the program to do. 
  3. Copy from the CIRCUITPY drive to the this program namefolder. Keep both, don’t overwrite. 

Looking at previous versions of programs:     

  1. Open the folder on your computer with the program name you want to look at.    
  2. Open the program that’s in the folder. Whatever version you want to see.

(On a mac, it should open in TextEdit. If it doesn’t, start TextEdit and open it from the file menu in TextEdit)

(On a PC, it should open in NotePad. If it doesn’t, start NotePad and open it from the file menu in NotePad)

Putting a program you have already written onto the CIRCUITPY drive:    

  1. In Mu, close the current program tab. 
  2. On the CIRCUITPY, delete the program. 
  3. On your computer, find the program you want to run on the device, and copy it to CIRCUITPY. 
  4. On CIRCUITPY, rename the program so that its name is  It will run automatically. 
  5. In Mu, press the Load button. Select the on the CIRCUITPY drive to load the one that is running. 

Code Block for using the onboard buttons

#Code Block to use the onboard buttons

import time
import board
import digitalio
from digitalio import DigitalInOut, Direction, Pull

buttonA = DigitalInOut(board.BUTTON_A)
buttonA.direction = Direction.INPUT
buttonA.pull = Pull.DOWN

while True:
    if buttonA.value:
        print("buttonA is pressed")
        print("buttonA is not pressed")

#End of Code Block

Interlude: Reading the program output, and Stopping and Starting your program

In Mu, click on the Serial button to open the Serial Dialogue panel.

There you will see the program outputs. 

Click anywhere inside the Serial Dialogue panel, press ctrl-c to stop a program. Press ctrl-d to restart a program. 

If you are not able to run the program, click on the Serial button to close the panel, and then click Serial again to reconnect and reopen the panel. 

Conditionals and Conditions

If, Then, Else, Else-if: if, else, elif

While, For: while, for

if this, then do that, else do this other thing

if this, then do that, else if this other condition, then do this other thing, else do the third thing

while this, then do that, and then check again. while this, then do that again, and check again. 

For does the same thing as while. It’s just more compact, and thus confusing, so I don’t teach it at the introductory level. 

Equal                                ==

Not equal                         !=

Greater than                    >

Less than                        <

Greater than or equal to >=

Less than or equal to      <=

And                               and

Not                                not

Or                                 or

Interlude: Code Block for using the RGB led lights on the board

#Code Block to use onboard Neopixel RGB led lights

import board
import neopixel
import time

num_leds = 10
pix = neopixel.NeoPixel(board.D8, num_leds, brightness=0.1, auto_write=False)

while True: 
    red = 255
    green = 128
    blue = 15

    pix.fill((red, green, blue))

#End of Code Block

By combining the blocks of code above, you can write programs to set the color of the lights using the two on board buttons.

Code Block for using capacitive touch sensing

#Code Block to use Capacitive Touch Sensing

import board
import touchio
import time

touchA1 = touchio.TouchIn(board.A1)

while True:
    if touchA1.value:
        print("A1 is being touched")
        print("no touch detected")
#End of Code Block

Change program to set colors using the capacitive touch sensing. 


Sloshes a wave of electricity, looks for the electrical capacity of the area

Recalibrates on each program restart

How to wire touch sensors. 

Stability requirements particular to capacitive touch sensing

Use coaxial wire to shield from touch en route. 

Code Block for using external switches

#Code Block to use External Switches (or any external digital input)

import board
import time
from digitalio import DigitalInOut, Direction, Pull

switchA1 = DigitalInOut(board.A1)
switchA1.direction = Direction.INPUT
switchA1.pull = Pull.UP

while True:
    if switchA1.value:
    #do these things if value is high = 1, which happens when A1 is connected to 3.3V
        print("A1 is high")
        print("A1 is low")
#End of Code Block

Notes: Pull up for external switches (Active Low), so you don’t short your power supply to ground by mistake. 

Types of external switches

Improvising external switches

Code Block for sensing force 

#Code Block for Sensing Force (or any other analog input)

import board
import time
from analogio import AnalogIn

analoginA1 = AnalogIn(board.A1)

while True:
    readingA1 = analoginA1.value
    print((readingA1,32767)) #makes a Tuple with the halfway value showing. 

#End of Code Block


Do this with a Force Sensitive Resistor, with a pull up bias resistor to 3.3V

Using the Serial Plotter

How to foam works: collapsing the bubbles makes more contact area. 

Why you need a bias resistor. 

Making use of analog input data

Scale data by mathematical manipulation. Possible, but not easy. 

Mapping inputs to outputs: simpleio library map_range function. 

Where to get the library:

Where to put the library: Put it in the lib folder on CIRCUITPY.

Using the map_range function with five arguments. 

Map the input range of 0 to 65535 onto the brightness range of 0 to 1.0

#Code Block for mapping force to brightness

import board
import time
import neopixel
import simpleio
from analogio import AnalogIn

num_leds = 10
pix = neopixel.NeoPixel(board.D8, num_leds, brightness=0.1, auto_write=False)

analoginA1 = AnalogIn(board.A1)

while True:
    red = 255
    green = 128
    blue = 15
    pix.fill((red, green, blue))
    readingA1 = analoginA1.value
    brightness = simpleio.map_range (analoginA1.value, 0, 66535, 0, 1.0)
    print((brightness,0.5)) #makes a Tuple with the halfway value showing.
    pix.brightness = brightness
#End of Code Block

Improvised force sensors

Two pieces of metal, with conductive foam in between.

One piece connects to GND.

Other piece connects two places: analog input, and bias pull up resistor. 

Use a pullup bias resistor. Start w 1k. if the readout stays above the halfway level, try something like 10k. If it is always below the halfway level, try something smaller. Don’t go any lower than 200Ω.