Skip to content

Crickit/CPX and CircuitPython Usage

Setup

Installing the CircuitPython Library Bundle

Download Library Bundle from here and copy it to lib on the CIRCUITPY volume.

Usage

Running a program

To run a program on the Crickit/CPX from Mu, save the code to a file named code.py in the CIRCUITPY volume.

Click on Serial to open a REPL and view values printed from the code with print().

Click on Plotter to view tuple values printed to the REPL. The plotter will print tuple (but not list) values.

The Light Sensor Example is good for sample plotter data.

Code Snipets

Examples borrow from the Adafruit docs

Boolean Touch Values
from adafruit_crickit import crickit

while True:
    if crickit.touch_1.value:
        print("Touched Cap Touch Pad 1")
    if crickit.touch_2.value:
        print("Touched Cap Touch Pad 2")
    if crickit.touch_3.value:
        print("Touched Cap Touch Pad 3")
    if crickit.touch_4.value:
        print("Touched Cap Touch Pad 4")
Raw Touch Values
import time
from adafruit_crickit import crickit

while True:
    print("({}, {}, {}, {})".format(crickit.touch_1.raw_value, crickit.touch_2.raw_value,
        crickit.touch_3.raw_value, crickit.touch_4.raw_value))    
    time.sleep(0.1)
Alternating LEDs
import time
import board
import neopixel

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.1)
pixels.fill((0, 0, 0))
pixels.show()

RED = (0x10, 0, 0) 
GREEN = (0, 0x10, 0)
BLUE = (0, 0, 0x10)
YELLOW = (0x10, 0x10, 0)

def lightUp(color):
    for i in range(10):
        pixels[i] = color
        time.sleep(.05)

while True:
    lightUp(RED)
    time.sleep(1)
    lightUp(GREEN)
    time.sleep(1)
    lightUp(BLUE)
    time.sleep(1)
    lightUp(YELLOW)
    time.sleep(1)
Using Touch Values with LEDs
import time
import board
import neopixel
from adafruit_crickit import crickit

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.1)
pixels.fill((0, 0, 0))
pixels.show()

RED = (0x10, 0, 0) 
GREEN = (0, 0x10, 0)
BLUE = (0, 0, 0x10)
YELLOW = (0x10, 0x10, 0)

def lightUp(color):
    for i in range(10):
        pixels[i] = color
        time.sleep(.05)

while True:
    if crickit.touch_1.value:
        lightUp(RED)
    if crickit.touch_2.value:
        lightUp(GREEN)
    if crickit.touch_3.value:
        lightUp(BLUE)
    if crickit.touch_4.value:
        lightUp(YELLOW)
Using the Buttons with LEDs
import time
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull

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

buttonB = DigitalInOut(board.BUTTON_B)
buttonB.direction = Direction.INPUT
buttonB.pull = Pull.DOWN

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.1)
pixels.fill((0, 0, 0))
pixels.show()

RED = (0x10, 0, 0) 
GREEN = (0, 0x10, 0)
BLUE = (0, 0, 0x10)
YELLOW = (0x10, 0x10, 0)

def lightUp(color):
    for i in range(10):
        pixels[i] = color

while True:
    if buttonA.value and buttonB.value: 
        lightUp(YELLOW)
    elif buttonA.value:
        lightUp(RED)
    elif buttonB.value:
        lightUp(BLUE)
    else:
        lightUp(GREEN)

    time.sleep(0.01)
Using the Slider with LEDs
import time
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull

switch = DigitalInOut(board.SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.1)
pixels.fill((0, 0, 0))
pixels.show()

RED = (0x10, 0, 0) 
GREEN = (0, 0x10, 0)

def lightUp(color):
    for i in range(10):
        pixels[i] = color

while True:
    if switch.value: 
        lightUp(RED)
    else:
        lightUp(GREEN)

    time.sleep(0.01)
Plotting CPX LIS3DH Accelerometer Values
import time
from adafruit_circuitplayground.express import cpx

while True:
    x, y, z = cpx.acceleration
    print("({}, {}, {})".format(x, y, z))
    time.sleep(0.1)
CPX Shake Detection
from adafruit_circuitplayground.express import cpx

while True:
    if cpx.shake(shake_threshold=10):
        print("Shake detected!")