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 with CRICKIT before, make sure you've updated it with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

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

# CircuitPython for the Adafruit Learning System Tutorial
# Universal Marionette Kit
# Project by Dano Wall, code by Mike Barela for Adafruit Industries
# MIT License
import time
from adafruit_crickit import crickit

# For signal control, we'll chat directly with seesaw, use 'ss' to shorted typing!
ss = crickit.seesaw

# four buttons with pullups, connect to ground to activate
BUTTON_1 = crickit.SIGNAL1  # button #1 connected to signal port 1 & ground
BUTTON_2 = crickit.SIGNAL2  # button #2 connected to signal port 2 & ground
BUTTON_3 = crickit.SIGNAL3  # button #3 connected to signal port 3 & ground
BUTTON_4 = crickit.SIGNAL4  # button #4 connected to signal port 4 & ground

ss.pin_mode(BUTTON_1, ss.INPUT_PULLUP)  # Set as input with a pullup resistor
ss.pin_mode(BUTTON_2, ss.INPUT_PULLUP)
ss.pin_mode(BUTTON_3, ss.INPUT_PULLUP)
ss.pin_mode(BUTTON_4, ss.INPUT_PULLUP)

while True:
    if not ss.digital_read(BUTTON_1):
        print("Button 1 pressed")
        crickit.servo_1.angle = 40
        time.sleep(0.1)
    else:
        crickit.servo_1.angle = 140
    if not ss.digital_read(BUTTON_2):
        print("Button 2 pressed")
        crickit.servo_2.angle = 140
        time.sleep(0.1)
    else:
        crickit.servo_2.angle = 40
    if not ss.digital_read(BUTTON_3):
        print("Button 3 pressed")
        crickit.servo_3.angle = 40
        time.sleep(0.1)
    else:
        crickit.servo_3.angle = 140
    if not ss.digital_read(BUTTON_4):
        print("Button 4 pressed")
        crickit.servo_4.angle = 140
        time.sleep(0.1)
    else:
        crickit.servo_4.angle = 40
Make sure the file saved to CIRCUITPY is named "code.py", this will allow it to run automatically when your CPX is powered on.

Troubleshooting

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 Feb 27, 2019. It was last updated on Feb 27, 2019. This page (CircuitPython Code) was last updated on Oct 21, 2019.