Learn Circuit Playground
Link to Adafruit's Circuit Playground Website
Link to Adafruit's Circuit Playground Website
LED On
LED On
"""This example turns on the little red LED."""
from adafruit_circuitplayground import cp
while True:
cp.red_led = True
LED Blink
LED Blink
"""This is the "Hello, world!" of CircuitPython: Blinky! This example blinks the little red LED on
and off!"""
import time
from adafruit_circuitplayground import cp
while True:
cp.red_led = True
time.sleep(0.5)
cp.red_led = False
time.sleep(0.5)
"""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 import cp
while True:
cp.red_led = not cp.red_led
time.sleep(0.5)
Slide Switch
Slide Switch
"""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 import cp
while True:
print("Slide switch:", cp.switch)
time.sleep(0.1)
"""This example uses the slide switch to control the little red LED."""
from adafruit_circuitplayground import cp
# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement
while True:
if cp.switch:
cp.red_led = True
else:
cp.red_led = False
"""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 import cp
while True:
cp.red_led = cp.switch
Tap
Tap
"""This example prints to the serial console when the board is double-tapped."""
import time
from adafruit_circuitplayground import cp
# Change to 1 for single-tap detection.
cp.detect_taps = 2
while True:
if cp.tapped:
print("Tapped!")
time.sleep(0.05)
"""This example turns on the little red LED and prints to the serial console when you double-tap
the Circuit Playground!"""
import time
from adafruit_circuitplayground import cp
# Change to 1 for detecting a single-tap!
cp.detect_taps = 2
while True:
if cp.tapped:
print("Tapped!")
cp.red_led = True
time.sleep(0.1)
else:
cp.red_led = False
"""This example prints to the serial console when the Circuit Playground is shaken."""
from adafruit_circuitplayground import cp
while True:
if cp.shake():
print("Shake detected!")
"""This example flashes the little red LED when the Circuit Playground is shaken."""
from adafruit_circuitplayground import cp
while True:
if cp.shake(shake_threshold=20):
print("Shake detected!")
cp.red_led = True
else:
cp.red_led = False
"""This example lights up all the NeoPixel LEDs red."""
from adafruit_circuitplayground import cp
while True:
cp.pixels.fill((50, 0, 0))
"""This example lights up the first NeoPixel red."""
from adafruit_circuitplayground import cp
cp.pixels.brightness = 0.3
while True:
cp.pixels[0] = (255, 0, 0)
"""This example lights up random NeoPixels."""
from adafruit_circuitplayground import cp
import time
import random
cp.pixels.brightness = 0.3
while True:
ran = random.randint(0,9)
cp.pixels[ran] = (255, 0, 255)
time.sleep(0.1)
cp.pixels[ran] = (0, 0, 0)
"""This example lights up the first and second NeoPixel, red and blue respectively."""
from adafruit_circuitplayground import cp
cp.pixels.brightness = 0.3
while True:
cp.pixels[0] = (255, 0, 0)
cp.pixels[1] = (0, 0, 255)
"""This example uses the light sensor on your Circuit Playground, located next to the picture of
the eye. Try shining a flashlight on your Circuit Playground, or covering the light sensor with
your finger to see the values increase and decrease."""
import time
from adafruit_circuitplayground import cp
while True:
print("Light:", cp.light)
time.sleep(0.2)
"""If you're using Mu, this example will plot the light levels from the light sensor (located next
to the eye) on your Circuit Playground. Try shining a flashlight on your Circuit Playground, or
covering the light sensor to see the plot increase and decrease."""
import time
from adafruit_circuitplayground import cp
while True:
print("Light:", cp.light)
print((cp.light,))
time.sleep(0.1)
"""
This example uses the light sensor on the Circuit Playground, located next to the picture of the
eye on the board. Once you have the library loaded, try shining a flashlight on your Circuit
Playground 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 import cp
cp.pixels.auto_write = False
cp.pixels.brightness = 0.3
def scale_range(value):
"""Scale a value from 0-320 (light range) to 0-9 (NeoPixel range).
Allows remapping light value to pixel position."""
return round(value / 320 * 9)
while True:
peak = scale_range(cp.light)
print(cp.light)
print(int(peak))
for i in range(10):
if i <= peak:
cp.pixels[i] = (0, 255, 255)
else:
cp.pixels[i] = (0, 0, 0)
cp.pixels.show()
time.sleep(0.05)
"""
This example uses the accelerometer on the Circuit Playground. It prints the values. Try moving
the board to see the values change. If you're using Mu, open the plotter to see the values plotted.
"""
import time
from adafruit_circuitplayground import cp
while True:
x, y, z = cp.acceleration
print((x, y, z))
time.sleep(0.1)
"""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 Circuit Playground
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 import cp
# 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 cp.switch:
# If the switch is to the right, it returns False!
print("Slide switch off!")
cp.pixels.fill((0, 0, 0))
continue
else:
R = 0
G = 0
B = 0
x, y, z = cp.acceleration
print((x, y, z))
cp.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
"""This example turns on the little red LED when button A is pressed."""
from adafruit_circuitplayground import cp
while True:
if cp.button_a:
print("Button A pressed!")
cp.red_led = True
"""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 import cp
cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
while True:
if cp.button_a:
cp.pixels[2] = (0, 255, 0)
else:
cp.pixels[2] = (0, 0, 0)
if cp.button_b:
cp.pixels[7] = (0, 0, 255)
else:
cp.pixels[7] = (0, 0, 0)
"""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 import cp
cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0)) # Turn off the NeoPixels if they're on!
while True:
if cp.button_a:
cp.pixels[0:5] = [(255, 0, 0)] * 5
else:
cp.pixels[0:5] = [(0, 0, 0)] * 5
if cp.button_b:
cp.pixels[5:10] = [(0, 255, 0)] * 5
else:
cp.pixels[5:10] = [(0, 0, 0)] * 5
"""This example uses the temperature sensor on the Circuit Playground, 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 import cp
while True:
print("Temperature C:", cp.temperature)
print("Temperature F:", cp.temperature * 1.8 + 32)
time.sleep(1)
"""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 import cp
while True:
print("Temperature C:", cp.temperature)
print("Temperature F:", cp.temperature * 1.8 + 32)
print((cp.temperature, cp.temperature * 1.8 + 32))
time.sleep(0.1)
"""
This example use the temperature sensor on the Circuit Playground, 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 import cp
cp.pixels.auto_write = False
cp.pixels.brightness = 0.3
# Set these based on your ambient temperature in Celsius for best results!
minimum_temp = 24
maximum_temp = 30
def scale_range(value):
"""Scale a value from the range of minimum_temp to maximum_temp (temperature range) to 0-10
(the number of NeoPixels). Allows remapping temperature value to pixel position."""
return int((value - minimum_temp) / (maximum_temp - minimum_temp) * 10)
while True:
peak = scale_range(cp.temperature)
print(cp.temperature)
print(int(peak))
for i in range(10):
if i <= peak:
cp.pixels[i] = (0, 255, 255)
else:
cp.pixels[i] = (0, 0, 0)
cp.pixels.show()
time.sleep(0.05)
"""This example prints to the serial console when you touch capacitive touch pad A1."""
from adafruit_circuitplayground import cp
while True:
if cp.touch_A1:
print('Touched pad A1')
"""This example prints to the serial console when you touch the capacitive touch pads."""
from adafruit_circuitplayground import cp
while True:
if cp.touch_A1:
print('Touched pad A1')
if cp.touch_A2:
print('Touched pad A2')
if cp.touch_A3:
print('Touched pad A3')
if cp.touch_A4:
print('Touched pad A4')
if cp.touch_A5:
print('Touched pad A5')
if cp.touch_A6:
print('Touched pad A6')
if cp.touch_TX:
print('Touched pad TX')
"""This example uses the capacitive touch pads on the Circuit Playground. They are located around
the outer edge of the board and are labeled A1-A6 and TX. (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 import cp
cp.pixels.brightness = 0.3
while True:
if cp.touch_A1:
print("Touched A1!")
cp.pixels[6] = (255, 0, 0)
if cp.touch_A2:
print("Touched A2!")
cp.pixels[8] = (210, 45, 0)
if cp.touch_A3:
print("Touched A3!")
cp.pixels[9] = (155, 100, 0)
if cp.touch_A4:
print("Touched A4!")
cp.pixels[0] = (0, 255, 0)
if cp.touch_A5:
print("Touched A5!")
cp.pixels[1] = (0, 135, 125)
if cp.touch_A6:
print("Touched A6!")
cp.pixels[3] = (0, 0, 255)
if cp.touch_TX:
print("Touched TX!")
cp.pixels[4] = (100, 0, 155)
time.sleep(0.1)
"""This example uses the capacitive touch pads on the Circuit Playground. They are located around
the outer edge of the board and are labeled A1-A6 and TX. (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 import cp
cp.pixels.brightness = 0.3
while True:
if cp.touch_A1:
print("Touched A1!")
cp.pixels.fill((255, 0, 0))
if cp.touch_A2:
print("Touched A2!")
cp.pixels.fill((210, 45, 0))
if cp.touch_A3:
print("Touched A3!")
cp.pixels.fill((155, 100, 0))
if cp.touch_A4:
print("Touched A4!")
cp.pixels.fill((0, 255, 0))
if cp.touch_A5:
print("Touched A5!")
cp.pixels.fill((0, 135, 125))
if cp.touch_A6:
print("Touched A6!")
cp.pixels.fill((0, 0, 255))
if cp.touch_TX:
print("Touched TX!")
cp.pixels.fill((100, 0, 155))
time.sleep(0.1)
"""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 import cp
cp.play_tone(262, 1)
cp.play_tone(294, 1)
"""This example plays a different tone for a duration of 1 second for each button pressed."""
from adafruit_circuitplayground import cp
while True:
if cp.button_a:
cp.play_tone(262, 1)
if cp.button_b:
cp.play_tone(294, 1)
"""This example plays a different tone for each button, while the button is pressed."""
from adafruit_circuitplayground import cp
while True:
if cp.button_a:
cp.start_tone(262)
elif cp.button_b:
cp.start_tone(294)
else:
cp.stop_tone()
"""
This example prints out sound levels using the sound sensor on a Circuit Playground Bluefruit.
Try making sounds towards the board to see the values change.
NOTE: This example does NOT support Circuit Playground Express.
"""
import time
from adafruit_circuitplayground import cp
while True:
print("Sound level:", cp.sound_level)
time.sleep(0.1)