Quick Reference Guide - Adafruit Circuit Playground Bluefruit

The adafruit_circuitplayground library provides a convenient interface to all the board features. The following tables provide summary information of the variables and functions in the interface. These can be used in Python scripts and also tested from the serial command line (REPL).

The library must be accessed (imported) using the following instruction before the following fragments will work:

from adafruit_circuitplayground import cp

This is needed either from scripts or at the serial command line (REPL).

Raw Sensor Values

The library abstracts the sensor inputs into attributes within the cp module which can be treated as read-only variables within Python code.

Sensor

Attribute

Values and Units

left ‘A’ pushbutton

cp.button_a

True when pressed, else False

right ‘B’ pushbutton

cp.button_b

True when pressed, else False

slide switch

cp.switch

True is slider is on left, False if on right

accelerometer

cp.acceleration

(x,y,z) in meters/sec^2

sound-level sensor

cp.sound_level

(floating-point number, units and range not yet determined)

light-level sensor

cp.light

integer, 0-512, units not calibrated

temperature sensor

cp.temperature

floating-point number in degrees C

capacitive touch

cp.touch_A1

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_A2

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_A3

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_A4

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_A5

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_A6

True when touched, otherwise False, auto-calibrating

capacitive touch

cp.touch_TX

True when touched, otherwise False, auto-calibrating

Accelerometer Notes

Accelerometer orientation:

  • X positive when right edge on top (components toward left)

  • Y positive when USB connector on top

  • Z positive when component-side up

A typical usage pattern is to assign the signal to three individual variables:

x, y, z = cp.acceleration

Processed Sensor Values

The physical sensor data is processed into several derived signals. These are available as a mix of attributes and function calls within the cp module.

Signal

Example Usage

Notes

physical tap

cp.tapped

True if a tap has been recorded; resets when the attribute is read.

physical shaking

cp.shake()

True if shaking currently exceeds the default threshold.

 

cp.shake(10)

True if shaking currently exceeds the specified (most sensitive) threshold.

sound level

cp.loud_sound()

True if sound currently exceeds the default threshold.

 

cp.loud_sound(50)

True if sound currently exceeds the specified (more sensitive) threshold.

Outputs

The board has a set of ten RGB LEDS (NeoPixels), a speaker with amplifier, and a single red indicator LED. These are controlled via a combination of attributes and function calls.

Output

Example Usage

Notes

red indicator LED

cp.red_led

True if lit, False if dark

 

cp.red_led = True

turn on indicator LED

 

cp.red_led = False

turn off indicator LED

ten RGB LEDs

cp.pixels

list of ten (red, green, blue) tuples: [(0, 0, 0), (0, 0, 0), (0, 0, 0), … (0, 0, 0)]

 

cp.pixels[0]

tuple with color of first LED: (0, 0, 0)

 

cp.pixels[0]=(255,0,0)

set the first LED to pure red

speaker

cp.start_tone(440)

start a pure tone playing at a specified frequency (440 Hz is concert ‘A’)

 

cp.stop_tone()

stop the sound

 

cp.play_tone(880, 0.2)

play a pure tone for the specific frequency (880 Hz) and duration (0.2) before continuing

 

cp.play_file('Rooster.wav')

play a WAV file from onboard storage before continuing; example assumes Rooster.wav has been copied to CIRCUITPY

 

cp.play_mp3('Rooster.wav')

play a MP3 file from onboard storage before continuing; example assumes Rooster.mp3 has been copied to CIRCUITPY

Speaker notes

  1. It appears that after start_tone() is invoked, stop_tone() must be called before start_tone() is called again, else it has no effect.

NeoPixel notes

Each NeoPixel color tuple has three color channel values. Each channel value is an integer from 0 to 255, where 0 is off and 255 is full intensity.

NeoPixel

color

(0,0,0)

black (dark)

(255,0,0)

pure red

(0,255,0)

pure green

(0,0,255)

pure blue

(0,255,255)

cyan

(255,0,255)

magenta

(255,255,0)

yellow

(255,255,255)

white

The ten NeoPixels are arranged like the hours on a clock only with the 12 and 6 positions missing. The following assumes the board is oriented with the USB connector at the ‘up’ 12 o’clock position:

pixel index

hour

NeoPixel location

0

11

left of the USB connector viewed with the USB ‘up’

1..4

10..7

counter-clockwise down the left

5..8

5..2

counter-clockwise up the right

9

1

right of the USB connector

Analog and Digital Pads

The CPB pads can be used to interface to external circuitry and hardware. This requires importing additional modules for access to the hardware:

import board
import digitalio
import analogio

Please see the sample code for examples of this use.

Most CPB pads can be used in either analog or digital modes. But in a confusing twist, the digital names do not correspond to the pad labels.

Pad Name

Analog Name

Digital Name

Other Functions

A1

A1

D6

A2

A2

D9

A3

A3

D10

SCL A4

A4

D3

SCL clock in I2C mode

SDA A5

A5

D2

SDA data in I2C mode

RX A6

A6

D0

RX receive in serial mode

TX

D1

TX transmit in serial mode

AUDIO

D12

Drives onboard speaker

VOUT

Voltage Output: USB or battery power

Reference: https://learn.adafruit.com/adafruit-circuit-playground-bluefruit/pinouts

Further CPB Details

The CPB library includes additional functions not listed here to further customize its behavior. For more information, please see the Adafruit Circuit Playground Library - API Reference.

Time

The time module partially implements the standard Python clock functions. It must be imported before use as follows:

import time
time.sleep(seconds)

Pause for the given number of seconds. System activity will continue but your program will wait. Note that this is only suitable for simple scripts, as any polled activity such as reading sensors, updating output, or receiving communications will stop.

time.monotonic()

Return the number of seconds since power was applied as a floating point number. Note that this clock is not cleared by the reset switch. The value initially includes three decimals of precision (milliseconds), but please note that the limited precision of a floating point number means means this resolution is quickly reduced. E.g. CircuitPython evaluates (4096.0 + 0.001 == 4096.0) as True due to limited numerical precision.

time.monotonic_ns()

Return the number of nanoseconds since power was applied as an integer. Note that this clock is not cleared by the reset switch. Since CircuitPython on the CPB support unlimited integer range this value does not lose precision.

General Function Demo Sketch

Many of these features are concisely demonstrated in the following general function demo for the Adafruit Circuit Playground Bluefruit board. Bits and pieces are taken from the Adafruit sample code.

Direct download: cpb_demo.py.

 1# cpb_demo.py
 2
 3# General function demo for the Adafruit Circuit Playground Bluefruit board.
 4# The current sensor data is displayed on the serial output stream.
 5# This program uses only onboard hardware.
 6# Bits and pieces are taken from the Adafruit sample code.
 7
 8# Import the board-specific input/output library.
 9from adafruit_circuitplayground import cp
10
11# Import the standard time module.
12import time
13
14# Initialize global variables.
15led_index = 0
16
17# Main event loop.
18while True:
19    
20    # Read the accelerometer, which functions primarily as a tilt sensor.
21    x, y, z = cp.acceleration
22
23    # Assemble all the touch True/False values into a list.
24    touches = [cp.touch_A1, cp.touch_A2, cp.touch_A3, cp.touch_A4, cp.touch_A5, cp.touch_A6, cp.touch_TX]
25        
26    # Print a one-line status report to the serial console.  This uses a Python
27    # 'formatted string literal' to concisely generate a print string from
28    # attributes in the environment.
29    print(f"But: {cp.button_a} {cp.button_b}  Sw: {cp.switch}  Tilt: {x:6.3f} {y:6.3f} {z:6.3f}  Light: {cp.light}  Temp: {cp.temperature} degC  Touch: {touches}")
30
31    # Treat the pushbuttons as a two-tone keyboard.
32    if cp.button_a:
33        cp.start_tone(440)
34    elif cp.button_b:
35        cp.start_tone(660)
36    else:
37        cp.stop_tone()
38        
39    # Chase the LEDs by lighting one at a time in index order.
40    cp.pixels[led_index] = (0,0,0)     # clear the previous LED to dark
41    led_index = (led_index + 1) % 10   # choose the next LED pixel index
42    cp.pixels[led_index] = (0,0,255)   # set one blue at a time
43    
44    # Pause to reduce the rate of output.
45    time.sleep(0.2)