制造商零件编号 3093
CRICKIT CIRCUIT PLAYGROUND EXP
Adafruit Industries LLC
License: See Original Project Circuit Playground
Courtesy of Adafruit
Guide by Dano Wall
Overview
Everything CRICKIT can do, all in one project!
This project shows how to connect a series of buttons, sensors, NeoPixel LED lights, motors, a speaker and more to showcase all the awesome things CRICKIT can do!
This project provides an excellent and entertaining demonstration of the wide variety of functions that CRICKIT for Circuit Playground Express can perform.
Electronics
Other Materials
Circuit Diagram
Before building the box and mounting the components, it's a good idea to wire up the electronics and make sure everything is working!
Buttons
Potentiometer
Capacitive Touch
NeoPixels
Speaker
DC Motor
Electromagnet and Solenoid
The Servo
CircuitPython Code
Getting to know CircuitPython
CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.
CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.
If you've never used Circuit Playground Express (CPX) with CRICKIT before, make sure you've updated it with the latest special Crickit/seesaw version of the Circuit Playground Express firmware. This guide will show you how.
Preparing your Board
To get your CRICKIT for CPX set up to run this code, follow these steps:
1) Install the latest CircuitPython for CRICKIT from the CircuitPython GitHub page
2) Get the latest 4.0 library pack, unzip it, and drag the library simpleio.mpy over into the /lib folder on CIRCUITPY. If there is no lib directory, create one to put the file into.
More info on installing libraries here.
Before continuing make sure your board's lib folder has the simplio library copied over.
Uploading
Make sure you've connected the Circuit Playground Express to your computer (mac/PC/Linux) via a known good USB A to micro-B cable. Your board should show up as a flash disk drive named CIRCUITPY (If you see a disk name CPLAYBOOT, try pressing the reset button again. If the only drive name you get is named CPLAYBOOT, CircuitPython may not be loaded on the board. You can load CircuitPython as per this guide).
Once your board is connected, copy code.py from the window below and paste it into Mu. Press the Save button and your code should automatically be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py.
"""
Crickit Exhibit
Project by Dano Wall and Isaac Wellish
Code by Isaac Wellish
The Crickit Exhibit demonstrates almost all of the capabilities
which CRICKIT can offer in one project
"""
# Functions:
#1. Hit a button to trigger a solenoid
#2. Hit a button to turn on an electromagnet
#3. Touch conductive tape to trigger a neopixel animation
#4. Turn a potentiometer to control a servo
#5. Shine light on the CPX to trigger and change the speed of a DC motor
#6. Hit both buttons to trigger a sound from the speaker!
import time
from adafruit_crickit import crickit
import board
import neopixel
from analogio import AnalogIn
from simpleio import map_range, tone
# RGB values
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
# For signal control, we'll chat directly with seesaw, use 'ss' to shorted typing!
# create seesaw object
ss = crickit.seesaw
# Two buttons are pullups, connect to ground to activate
BUTTON_1 = crickit.SIGNAL1
BUTTON_2 = crickit.SIGNAL2
ss.pin_mode(BUTTON_1, ss.INPUT_PULLUP)
ss.pin_mode(BUTTON_2, ss.INPUT_PULLUP)
#solenoid at drive spot 1
crickit.drive_1.frequency = 1000
#electromagnet at drive spot 2
crickit.drive_2.frequency = 1000
# initialize NeoPixels to num_pixels
num_pixels = 30
# The following line sets up a NeoPixel strip on Crickit CPX pin A1
pixels = neopixel.NeoPixel(board.A1, num_pixels, brightness=0.3, auto_write=False)
#sleep var for pushing both buttons
SLEEP_DELAY = 0.1
# NeoPixel function
def color_chase(color, wait):
for i in range(num_pixels):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
# potentiometer connected to signal #3
pot = crickit.SIGNAL8
# initialize the light sensor on the CPX and the DC motor
light_in = AnalogIn(board.LIGHT)
while True:
# button + solenoid & electromagnet code
# button 1 - solenoid on
if not ss.digital_read(BUTTON_1):
print("Button 1 pressed")
crickit.drive_1.fraction = 1.0 # all the way on
time.sleep(0.01)
crickit.drive_1.fraction = 0.0 # all the way off
time.sleep(0.5)
else:
crickit.drive_1.fraction = 0.0
# button 2 electromagnet on
if not ss.digital_read(BUTTON_2):
print("Button 2 pressed")
crickit.drive_2.fraction = 1.0 # all the way on
time.sleep(0.5)
else:
crickit.drive_2.fraction = 0.0 # all the way off
# Capacitive touch + neopixel code
touch_raw_value = crickit.touch_1.raw_value
if touch_raw_value>800:
print("chase")
color_chase(PURPLE, 0.1)
else:
pixels.fill((0,0,0))
pixels.show()
# potentiomter + servo
# uncomment this line to see the values of the pot
# print((ss.analog_read(pot),))
# time.sleep(0.25)
# maps the range of the pot to the range of the servo
angle = map_range(ss.analog_read(pot), 0, 1023, 180, 0)
# sets the servo equal to the relative position on the pot
crickit.servo_1.angle = angle
# Light sensor + DC motor
# uncomment to see values of light
# print(light_in.value)
# time.sleep(0.5)
# reads the on-board light sensor and graphs the brighness with NeoPixels
# light value remaped to motor speed
peak = map_range(light_in.value, 3000, 62000, 0, 1)
# DC motor
crickit.dc_motor_1.throttle = peak # full speed forward
# hit both buttons to trigger noise
if not ss.digital_read(BUTTON_1) and not ss.digital_read(BUTTON_2):
print("Buttons 1 and 2 pressed")
for f in (262, 294, 330, 349, 392, 440, 494, 523):
tone(board.A0, f, 0.25)
time.sleep(SLEEP_DELAY)
Make sure the file saved to CIRCUITPY is named "code.py", this will allow it to run automatically when your CPX is powered on.
Test It Out
Go ahead and test out the code! What happens?
The following should occur:
Troubleshooting
Problem: I get an error in the REPL that says "map_range undefined"
Solution: Make sure simplio library is installed! Info on installing libraries here.
Problem: My Circuit Playground Express isn't recognized by Mu!
Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.
Problem: My buttons don't work!
Solution: Make sure you've updated the CircuitPython firmware for CRICKIT from the CRICKIT guide.
Problem: Nothing is moving!
Solution: Check that your Circuit Playground Express is connected to a 5V power supply and the small slide switch on CRICKIT is set to "ON". Be sure the lightest servo wire faces away from the CRICKIT board, the darkest (black.brown) is closest to the center of CRICKIT.
Prepare the Box
Find yourself a large, flat box, with enough surface area to accommodate all the electronics you want to mount on it.
At this point, it's a good idea to lay out your electronics to get an idea for how they'll fit when mounted.
Some choices about where to arrange certain items must be made at this point, giving consideration to the placement of CRICKIT's various plugs and terminal blocks.
Sketch out placement of holes
Trace outlines of where you will need holes on cardboard box.
The holes cut in the box with make routing the cables to and from CRICKIT easy to organize.
For younger makers, we suggest having an older maker assist. All should be careful of sharp objects.
Cut out holes
Use something sharp (like a pen tip or tweezers) to poke holes.
Use a box cutter to cut out the larger rectangular holes.
Your box should now be ready to install CRICKIT and all its assorted electronics.
Making Connections
Buttons
Screw arcade buttons into holes in the box.
Connect the button terminals to CRICKIT's Signal port using male-to-male jumper wires plugged into the quick connect wires.
These buttons can be configured to control other things connected to CRICKIT, like the electromagnet and solenoid.
NeoPixel Strip
Feed the NeoPixel strip through hole in box.
Hold the strip in place with sections of double sided foam tape.
Capacitive Touch
Place a line of conductive tape on the box, leaving a small section at the end free to connect to an alligator clip.
When touched, this capacitive strip can be used to trigger things such as NeoPixel animations.
Potentiometer
Install the potentiometer, screwing it tightly into place
Connect alligator clips to the terminals of the potentiometer inside the box.
Press the potentiometer knob into place.
Servo Motor
Press servo motor through rectangular hole, hold in place with a couple dabs of hot glue if necessary.
A protractor can make a fun addition as an angle indicator, or draw your own indicator directly on the box.
Cut an arrow out of paper and tape this to the servo arm, then press onto the servo horn.
The potentiometer in this example is connected to the servo motor, allowing the user to control the angle of the motor arm by turning the potentiometer knob.
DC Motor
Screw the DC motor into place. If bolts aren't handy, a square of double-sided tape works just as well.
Affix a wheel to the hub of the motor.
For this project, the DC motor is used as a brightness indicator, increasing in speed in accordance with data received from the light sensor onboard Circuit Playground Express.
Solenoid
Use a small square of double sided foam tape to stick the solenoid to the box.
A bell taken from a generic hotel bell or bike bell works great when paired with this mini solenoid.
The solenoid can be triggered by a button press, and used to tap a bell or tuning fork to make it ring.
Electromagnet
Use a small piece of cardboard to hold the electromagnet perpendicular to the surface of the box.
Screw the wires coming out of the electromagnet into the Drive terminals on CRICKIT.
The electromagnet generates a magnetic field current is run through it, and loses its magnetism when the current is cut off. A nail or other sufficiently large piece of metal works well as an indicator for when the electromagnet is on or off.
Speaker
The small mono enclosed speaker can be stuck in place with double-sided tape, or connected with bolts to the box.
Pressing two buttons at once can perform a different function that either button on its own, like in this example, playing sound through the speaker.