Installation¶
This driver depends on many other libraries! Please install it by downloading the Adafruit library and driver bundle.
Usage Example¶
Using it is super simple. Simply import the cpx
variable from the module
and then use it.
from adafruit_circuitplayground.express import cpx
while True:
if cpx.button_a:
print("Temperature:", cpx.temperature)
cpx.red_led = cpx.button_b
Contributing¶
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Documentation¶
For information on building library documentation, please check out this guide.
Table of Contents¶
Simple test¶
Ensure your device works with this simple test.
1 2 3 4 5 6 7 8 | import time
from adafruit_circuitplayground.express import cpx
while True:
x, y, z = cpx.acceleration
print(x, y, z)
time.sleep(0.1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # CircuitPython demo - NeoPixel
import time
from adafruit_circuitplayground.express import cpx
# The number of pixels in the strip
numpix = 10
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if (pos < 0) or (pos > 255):
return (0, 0, 0)
if pos < 85:
return (int(pos * 3), int(255 - (pos*3)), 0)
if pos < 170:
pos -= 85
return (int(255 - pos*3), 0, int(pos*3))
pos -= 170
return (0, int(pos*3), int(255 - pos*3))
def rainbow_cycle(wait):
for j in range(255):
for i in range(cpx.pixels.n):
idx = int((i * 256 / len(cpx.pixels)) + j)
cpx.pixels[i] = wheel(idx & 255)
cpx.pixels.show()
time.sleep(wait)
cpx.pixels.auto_write = False
cpx.pixels.brightness = 0.3
while True:
rainbow_cycle(0.001) # rainbowcycle with 1ms delay per step
|
1 2 3 4 5 | from adafruit_circuitplayground.express import cpx
while True:
if cpx.shake(shake_threshold=20):
print("Shake detected!")
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from adafruit_circuitplayground.express import cpx
# Set to check for single-taps.
cpx.detect_taps = 1
tap_count = 0
# We're looking for 2 single-taps before moving on.
while tap_count < 2:
if cpx.tapped:
tap_count += 1
print("Reached 2 single-taps!")
# Now switch to checking for double-taps
tap_count = 0
cpx.detect_taps = 2
# We're looking for 2 double-taps before moving on.
while tap_count < 2:
if cpx.tapped:
tap_count += 1
print("Reached 2 double-taps!")
print("Done.")
|
1 2 3 4 5 6 7 | from adafruit_circuitplayground.express import cpx
cpx.detect_taps = 1
while True:
if cpx.tapped:
print("Single tap detected!")
|
1 2 3 4 5 6 7 8 9 | from adafruit_circuitplayground.express import cpx
while True:
if cpx.button_a:
cpx.start_tone(262)
elif cpx.button_b:
cpx.start_tone(294)
else:
cpx.stop_tone()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from adafruit_circuitplayground.express import cpx
while True:
if cpx.touch_A1:
print('Touched pad A1')
if cpx.touch_A2:
print('Touched pad A2')
if cpx.touch_A3:
print('Touched pad A3')
if cpx.touch_A4:
print('Touched pad A4')
if cpx.touch_A5:
print('Touched pad A5')
if cpx.touch_A6:
print('Touched pad A6')
if cpx.touch_A7:
print('Touched pad A7')
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | """If the switch is to the right, it will appear that nothing is happening. Move the switch to the
left to see the NeoPixels light up in colors related to the accelerometer! The CPX has an
accelerometer in the center that returns (x, y, z) acceleration values. This program uses those
values to light up the NeoPixels based on those acceleration values."""
from adafruit_circuitplayground.express import cpx
# Main loop gets x, y and z axis acceleration, prints the values, and turns on
# red, green and blue, at levels related to the x, y and z values.
while True:
if not cpx.switch:
# If the switch is to the right, it returns False!
print("Slide switch off!")
cpx.pixels.fill((0, 0, 0))
continue
else:
R = 0
G = 0
B = 0
x, y, z = cpx.acceleration
print((x, y, z))
cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
|
1 2 3 4 5 6 7 | """This example turns on the little red LED when button A is pressed."""
from adafruit_circuitplayground.express import cpx
while True:
if cpx.button_a:
print("Button A pressed!")
cpx.red_led = True
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | """This example turns the little red LED on only while button B is currently being pressed."""
from adafruit_circuitplayground.express import cpx
# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement
while True:
if cpx.button_b:
cpx.red_led = True
else:
cpx.red_led = False
# Can also be written as:
# cpx.red_led = cpx.button_b
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | """This example lights up the third NeoPixel while button A is being pressed, and lights up the
eighth NeoPixel while button B is being pressed."""
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
while True:
if cpx.button_a:
cpx.pixels[2] = (0, 255, 0)
else:
cpx.pixels[2] = (0, 0, 0)
if cpx.button_b:
cpx.pixels[7] = (0, 0, 255)
else:
cpx.pixels[7] = (0, 0, 0)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | """This example lights up half the NeoPixels red while button A is being pressed, and half the
NeoPixels green while button B is being pressed."""
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
while True:
if cpx.button_a:
cpx.pixels[0:5] = [(255, 0, 0)] * 5
else:
cpx.pixels[0:5] = [(0, 0, 0)] * 5
if cpx.button_b:
cpx.pixels[5:10] = [(0, 255, 0)] * 5
else:
cpx.pixels[5:10] = [(0, 0, 0)] * 5
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the adafruit_irremote.mpy library.
This example uses the IR receiver found near the center of the board. Works with another CPX
running the cpx_ir_transmit.py example. The NeoPixels will light up when the buttons on the
TRANSMITTING CPX are pressed!"""
import pulseio
import board
import adafruit_irremote
from adafruit_circuitplayground.express import cpx
# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
decoder = adafruit_irremote.GenericDecode()
while True:
cpx.red_led = True
pulses = decoder.read_pulses(pulsein)
try:
# Attempt to convert received pulses into numbers
received_code = decoder.decode_bits(pulses, debug=False)
except adafruit_irremote.IRNECRepeatException:
# We got an unusual short code, probably a 'repeat' signal
continue
except adafruit_irremote.IRDecodeException:
# Something got distorted
continue
print("Infrared code received: ", received_code)
if received_code == [66, 84, 78, 65]:
print("Button A signal")
cpx.pixels.fill((100, 0, 155))
if received_code == [66, 84, 78, 64]:
print("Button B Signal")
cpx.pixels.fill((210, 45, 0))
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | """THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the adafruit_irremote.mpy library.
This example uses the IR transmitter found near the center of the board. Works with another CPX
running the cpx_ir_receive.py example. Press the buttons to light up the NeoPixels on the RECEIVING
CPX!"""
import time
import pulseio
import board
import adafruit_irremote
from adafruit_circuitplayground.express import cpx
# Create a 'pulseio' output, to send infrared signals from the IR transmitter
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
pulseout = pulseio.PulseOut(pwm)
# Create an encoder that will take numbers and turn them into NEC IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550],
zero=[550, 1700], trail=0)
while True:
if cpx.button_a:
print("Button A pressed! \n")
cpx.red_led = True
encoder.transmit(pulseout, [66, 84, 78, 65])
cpx.red_led = False
# wait so the receiver can get the full message
time.sleep(0.2)
if cpx.button_b:
print("Button B pressed! \n")
cpx.red_led = True
encoder.transmit(pulseout, [66, 84, 78, 64])
cpx.red_led = False
time.sleep(0.2)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | """THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the simpleio.mpy library.
This example uses the light sensor on the CPX, located net to the picture of the eye on the board.
Once you have the library loaded, try shining a flashlight on your CPX to watch the number of
NeoPixels lit up increase, or try covering up the light sensor to watch the number decrease."""
import time
from adafruit_circuitplayground.express import cpx
import simpleio
cpx.pixels.auto_write = False
cpx.pixels.brightness = 0.3
while True:
# light value remapped to pixel position
peak = simpleio.map_range(cpx.light, 0, 320, 0, 10)
print(cpx.light)
print(int(peak))
for i in range(0, 10, 1):
if i <= peak:
cpx.pixels[i] = (0, 255, 255)
else:
cpx.pixels[i] = (0, 0, 0)
cpx.pixels.show()
time.sleep(0.05)
|
1 2 3 4 5 6 7 8 9 | """This example uses the light sensor on your CPX, located next to the picture of the eye. Try
shining a flashlight on your CPX, or covering the light sensor with your finger to see the values
increase and decrease."""
import time
from adafruit_circuitplayground.express import cpx
while True:
print("Light:", cpx.light)
time.sleep(1)
|
1 2 3 4 5 6 7 8 | """This example lights up the first and second NeoPixel, red and blue respectively."""
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.3
while True:
cpx.pixels[0] = (255, 0, 0)
cpx.pixels[1] = (0, 0, 255)
|
1 2 3 4 5 6 7 8 9 10 | """If you're using Mu, this example will plot the light levels from the light sensor (located next
to the eye) on your CPX. Try shining a flashlight on your CPX, or covering the light sensor to see
the plot increase and decrease."""
import time
from adafruit_circuitplayground.express import cpx
while True:
print("Light:", cpx.light)
print((cpx.light,))
time.sleep(0.1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | """THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples
Copy the "dip.wav" and "rise.wav" files to your CIRCUITPY drive.
Once the files are copied, this example plays a different wav file for each button pressed!"""
from adafruit_circuitplayground.express import cpx
while True:
if cpx.button_a:
cpx.play_file("dip.wav")
if cpx.button_b:
cpx.play_file("rise.wav")
|
1 2 3 4 5 6 7 8 9 10 | """THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples
Copy the "dip.wav" file to your CIRCUITPY drive.
Once the file is copied, this example plays a wav file!"""
from adafruit_circuitplayground.express import cpx
cpx.play_file("dip.wav")
|
1 2 3 4 5 6 7 8 | """This example plays a different tone for a duration of 1 second for each button pressed."""
from adafruit_circuitplayground.express import cpx
while True:
if cpx.button_a:
cpx.play_tone(262, 1)
if cpx.button_b:
cpx.play_tone(294, 1)
|
1 2 3 4 5 6 | """This example plays two tones for 1 second each. Note that the tones are not in a loop - this is
to prevent them from playing indefinitely!"""
from adafruit_circuitplayground.express import cpx
cpx.play_tone(262, 1)
cpx.play_tone(294, 1)
|
1 2 3 4 5 6 7 8 9 10 | """This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on
and off!"""
import time
from adafruit_circuitplayground.express import cpx
while True:
cpx.red_led = True
time.sleep(0.5)
cpx.red_led = False
time.sleep(0.5)
|
1 2 3 4 5 6 7 8 | """This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on
and off! It's a shorter version of the other Blinky example."""
import time
from adafruit_circuitplayground.express import cpx
while True:
cpx.red_led = not cpx.red_led
time.sleep(0.5)
|
1 2 3 4 5 | """This example turns on the little red LED."""
from adafruit_circuitplayground.express import cpx
while True:
cpx.red_led = True
|
1 2 3 4 5 6 7 8 9 10 11 | """This example uses the slide switch to control the little red LED."""
from adafruit_circuitplayground.express import cpx
# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement
while True:
if cpx.switch:
cpx.red_led = True
else:
cpx.red_led = False
|
1 2 3 4 5 6 | """This example uses the slide switch to control the little red LED. When the switch is to the
right it returns False, and when it's to the left, it returns True."""
from adafruit_circuitplayground.express import cpx
while True:
cpx.red_led = cpx.switch
|
1 2 3 4 5 6 7 8 | """This example prints the status of the slide switch. Try moving the switch back and forth to see
what's printed to the serial console!"""
import time
from adafruit_circuitplayground.express import cpx
while True:
print("Slide switch:", cpx.switch)
time.sleep(0.1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | """This example uses the sound sensor, located next to the picture of the ear on your board, to
light up the NeoPixels as a sound meter. Try talking to your CPX or clapping, etc, to see the
NeoPixels light up!"""
import array
import math
import audiobusio
import board
from adafruit_circuitplayground.express import cpx
def constrain(value, floor, ceiling):
return max(floor, min(value, ceiling))
def log_scale(input_value, input_min, input_max, output_min, output_max):
normalized_input_value = (input_value - input_min) / (input_max - input_min)
return output_min + math.pow(normalized_input_value, 0.630957) * (output_max - output_min)
def normalized_rms(values):
minbuf = int(sum(values) / len(values))
return math.sqrt(sum(float(sample - minbuf) *
(sample - minbuf) for sample in values) / len(values))
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)
samples = array.array('H', [0] * 160)
mic.record(samples, len(samples))
input_floor = normalized_rms(samples) + 10
# Lower number means more sensitive - more LEDs will light up with less sound.
sensitivity = 500
input_ceiling = input_floor + sensitivity
peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print((magnitude,))
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
input_floor, input_ceiling, 0, 10)
cpx.pixels.fill((0, 0, 0))
for i in range(10):
if i < c:
cpx.pixels[i] = (i * (255 // 10), 50, 0)
if c >= peak:
peak = min(c, 10 - 1)
elif peak > 0:
peak = peak - 1
if peak > 0:
cpx.pixels[int(peak)] = (80, 0, 255)
cpx.pixels.show()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | """This example turns on the little red LED and prints to the serial console when you double-tap
the CPX!"""
import time
from adafruit_circuitplayground.express import cpx
# Change to 1 for detecting a single-tap!
cpx.detect_taps = 2
while True:
if cpx.tapped:
print("Tapped!")
cpx.red_led = True
time.sleep(0.1)
else:
cpx.red_led = False
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | """THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE.
This example requires the simpleio.mpy library.
This example use the temperature sensor on the CPX, located next to the picture of the thermometer
on the board. Try warming up the board to watch the number of NeoPixels lit up increase, or cooling
it down to see the number decrease. You can set the min and max temperatures to make it more or
less sensitive to temperature changes.
"""
import time
from adafruit_circuitplayground.express import cpx
import simpleio
cpx.pixels.auto_write = False
cpx.pixels.brightness = 0.3
# Set these based on your ambient temperature in Celsius for best results!
minimum_temp = 24
maximum_temp = 30
while True:
# temperature value remapped to pixel position
peak = simpleio.map_range(cpx.temperature, minimum_temp, maximum_temp, 0, 10)
print(cpx.temperature)
print(int(peak))
for i in range(0, 10, 1):
if i <= peak:
cpx.pixels[i] = (0, 255, 255)
else:
cpx.pixels[i] = (0, 0, 0)
cpx.pixels.show()
time.sleep(0.05)
|
1 2 3 4 5 6 7 8 9 10 11 | """If you're using Mu, this example will plot the temperature in C and F on the plotter! Click
"Plotter" to open it, and place your finger over the sensor to see the numbers change. The
sensor is located next to the picture of the thermometer on the CPX."""
import time
from adafruit_circuitplayground.express import cpx
while True:
print("Temperature C:", cpx.temperature)
print("Temperature F:", cpx.temperature * 1.8 + 32)
print((cpx.temperature, cpx.temperature * 1.8 + 32))
time.sleep(0.1)
|
1 2 3 4 5 6 7 8 9 10 | """This example uses the temperature sensor on the CPX, located next to the image of a thermometer
on the board. It prints the temperature in both C and F to the serial console. Try putting your
finger over the sensor to see the numbers change!"""
import time
from adafruit_circuitplayground.express import cpx
while True:
print("Temperature C:", cpx.temperature)
print("Temperature F:", cpx.temperature * 1.8 + 32)
time.sleep(1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | """This example uses the capacitive touch pads on the CPX. They are located around the outer edge
of the board and are labeled A1-A7. (A0 is not a touch pad.) This example lights up all the
NeoPixels a different color of the rainbow for each pad touched!"""
import time
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.3
while True:
if cpx.touch_A1:
print("Touched A1!")
cpx.pixels.fill((255, 0, 0))
if cpx.touch_A2:
print("Touched A2!")
cpx.pixels.fill((210, 45, 0))
if cpx.touch_A3:
print("Touched A3!")
cpx.pixels.fill((155, 100, 0))
if cpx.touch_A4:
print("Touched A4!")
cpx.pixels.fill((0, 255, 0))
if cpx.touch_A5:
print("Touched A5!")
cpx.pixels.fill((0, 135, 125))
if cpx.touch_A6:
print("Touched A6!")
cpx.pixels.fill((0, 0, 255))
if cpx.touch_A7:
print("Touched A7!")
cpx.pixels.fill((100, 0, 155))
time.sleep(0.1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | """This example uses the capacitive touch pads on the CPX. They are located around the outer edge
of the board and are labeled A1-A7. (A0 is not a touch pad.) This example lights up the nearest
NeoPixel to that pad a different color of the rainbow!"""
import time
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.3
while True:
if cpx.touch_A1:
print("Touched A1!")
cpx.pixels[6] = (255, 0, 0)
if cpx.touch_A2:
print("Touched A2!")
cpx.pixels[8] = (210, 45, 0)
if cpx.touch_A3:
print("Touched A3!")
cpx.pixels[9] = (155, 100, 0)
if cpx.touch_A4:
print("Touched A4!")
cpx.pixels[0] = (0, 255, 0)
if cpx.touch_A5:
print("Touched A5!")
cpx.pixels[1] = (0, 135, 125)
if cpx.touch_A6:
print("Touched A6!")
cpx.pixels[3] = (0, 0, 255)
if cpx.touch_A7:
print("Touched A7!")
cpx.pixels[4] = (100, 0, 155)
time.sleep(0.1)
|
adafruit_circuitplayground.express
¶
CircuitPython driver from CircuitPlayground Express.
- Author(s): Kattni Rembor, Scott Shawcroft
-
class
adafruit_circuitplayground.express.
Express
[source]¶ Represents a single CircuitPlayground Express. Do not use more than one at a time.
-
acceleration
¶ Obtain data from the x, y and z axes.
This example prints the values. Try moving the board to see how the printed values change.
from adafruit_circuitplayground.express import cpx while True: x, y, z = cpx.acceleration print(x, y, z)
-
adjust_touch_threshold
(adjustment)[source]¶ Adjust the threshold needed to activate the capacitive touch pads. Higher numbers make the touch pads less sensitive.
Parameters: adjustment (int) – The desired threshold increase from adafruit_circuitplayground.express import cpx cpx.adjust_touch_threshold(200) while True: if cpx.touch_A1: print('Touched pad A1')
True
when Button A is pressed.False
if not.from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: print("Button A pressed!")
True
when Button B is pressed.False
if not.from adafruit_circuitplayground.express import cpx while True: if cpx.button_b: print("Button B pressed!")
-
detect_taps
¶ Configure what type of tap is detected by
cpx.tapped
. Use1
for single-tap detection and2
for double-tap detection. This does nothing withoutcpx.tapped
.from adafruit_circuitplayground.express import cpx cpx.detect_taps = 1 while True: if cpx.tapped: print("Single tap detected!")
-
light
¶ The brightness of the CircuitPlayground in approximate Lux.
Try covering the sensor next to the eye to see it change.
from adafruit_circuitplayground.express import cpx import time while True: print("Lux:", cpx.light) time.sleep(1)
-
pixels
¶ Sequence-like object representing the ten NeoPixels around the outside of the CircuitPlayground. Each pixel is at a certain index in the sequence as labeled below. Colors can be RGB hex like 0x110000 for red where each two digits are a color (0xRRGGBB) or a tuple like (17, 0, 0) where (R, G, B). Set the global brightness using any number from 0 to 1 to represent a percentage, i.e. 0.3 sets global brightness to 30%.
See
neopixel.NeoPixel
for more info.Here is an example that sets the first pixel green and the second red.
from adafruit_circuitplayground.express import cpx cpx.pixels.brightness = 0.3 cpx.pixels[0] = 0x003000 cpx.pixels[9] = (30, 0, 0) # Wait forever. CTRL-C to quit. while True: pass
-
play_file
(file_name)[source]¶ Play a .wav file using the onboard speaker.
Parameters: file_name – The name of your .wav file in quotation marks including .wav from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: cpx.play_file("laugh.wav") elif cpx.button_b: cpx.play_file("rimshot.wav")
-
play_tone
(frequency, duration)[source]¶ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone.
Parameters: from adafruit_circuitplayground.express import cpx cpx.play_tone(440, 1)
-
red_led
¶ The red led next to the USB plug marked D13.
from adafruit_circuitplayground.express import cpx import time while True: cpx.red_led = True time.sleep(1) cpx.red_led = False time.sleep(1)
-
shake
(shake_threshold=30)[source]¶ Detect when device is shaken.
Parameters: shake_threshold (int) – The threshold shake must exceed to return true (Default: 30) from adafruit_circuitplayground.express import cpx while True: if cpx.shake(): print("Shake detected!")
Decreasing
shake_threshold
increases shake sensitivity, i.e. the code will return a shake detected more easily with a lowershake_threshold
. Increasing it causes the opposite.shake_threshold
requires a minimum value of 10 - 10 is the value when the board is not moving, therefore anything less than 10 will erroneously report a constant shake detected.from adafruit_circuitplayground.express import cpx while True: if cpx.shake(shake_threshold=20): print("Shake detected more easily than before!")
-
start_tone
(frequency)[source]¶ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone.
Parameters: frequency (int) – The frequency of the tone in Hz from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: cpx.start_tone(262) elif cpx.button_b: cpx.start_tone(294) else: cpx.stop_tone()
-
stop_tone
()[source]¶ Use with start_tone to stop the tone produced.
from adafruit_circuitplayground.express import cpx while True: if cpx.button_a: cpx.start_tone(262) elif cpx.button_b: cpx.start_tone(294) else: cpx.stop_tone()
-
switch
¶ True
when the switch is to the left next to the music notes.False
when it is to the right towards the ear.from adafruit_circuitplayground.express import cpx import time while True: print("Slide switch:", cpx.switch) time.sleep(1)
-
tapped
¶ True once after a detecting a tap. Requires
cpx.detect_taps
.Tap the CPX once for a single-tap, or quickly tap twice for a double-tap.
from adafruit_circuitplayground.express import cpx cpx.detect_taps = 1 while True: if cpx.tapped: print("Single tap detected!")
To use single and double tap together, you must have a delay between them. It will not function properly without it. This example uses both by counting a specified number of each type of tap before moving on in the code.
from adafruit_circuitplayground.express import cpx # Set to check for single-taps. cpx.detect_taps = 1 tap_count = 0 # We're looking for 2 single-taps before moving on. while tap_count < 2: if cpx.tapped: tap_count += 1 print("Reached 2 single-taps!") # Now switch to checking for double-taps tap_count = 0 cpx.detect_taps = 2 # We're looking for 2 double-taps before moving on. while tap_count < 2: if cpx.tapped: tap_count += 1 print("Reached 2 double-taps!") print("Done.")
-
temperature
¶ The temperature of the CircuitPlayground in Celsius.
Converting this to Farenheit is easy!
from adafruit_circuitplayground.express import cpx import time while True: temperature_c = cpx.temperature temperature_f = temperature_c * 1.8 + 32 print("Temperature celsius:", temperature_c) print("Temperature fahrenheit:", temperature_f) time.sleep(1)
-
touch_A1
¶ Detect touch on capacitive touch pad A1.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A1: print('Touched pad A1')
-
touch_A2
¶ Detect touch on capacitive touch pad A2.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A2: print('Touched pad A2')
-
touch_A3
¶ Detect touch on capacitive touch pad A3.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A3: print('Touched pad A3')
-
touch_A4
¶ Detect touch on capacitive touch pad A4.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A4: print('Touched pad A4')
-
touch_A5
¶ Detect touch on capacitive touch pad A5.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A5: print('Touched pad A5')
-
touch_A6
¶ Detect touch on capacitive touch pad A6.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A6: print('Touched pad A6')
-
touch_A7
¶ Detect touch on capacitive touch pad A7.
from adafruit_circuitplayground.express import cpx while True: if cpx.touch_A7: print('Touched pad A7')
-
were_pressed
¶ Returns a set of the buttons that have been pressed
from adafruit_circuitplayground.express import cpx while True: print(cpx.were_pressed)
-
-
class
adafruit_circuitplayground.express.
Photocell
(pin)[source]¶ Simple driver for analog photocell on the CircuitPlayground Express.
-
light
¶ Light level in SI Lux.
-
-
adafruit_circuitplayground.express.
cpx
= <adafruit_circuitplayground.express.Express object>¶ Object that is automatically created on import.
To use, simply import it from the module:
from adafruit_circuitplayground.express import cpx