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.

  • Find the simplio library in the library pack as simplio.mpy
  • Drag to the lib folder in the CIRCUITPY drive
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:

  • Hit a button to trigger a solenoid
  • Hit a button to turn on an electromagnet
  • Touch conductive tape to trigger a NeoPixel animation
  • Turn a potentiometer to control a servo
  • Shine light on the CPX to trigger and change the speed of a DC motor
  • Hit both buttons to trigger a sound from the speaker!

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.

This guide was first published on Mar 05, 2019. It was last updated on Mar 05, 2019. This page (CircuitPython Code) was last updated on Nov 26, 2019.