Uploading Code with Mu

But first...

To use Circuit Playground Express and CRICKIT together, you'll first need to install the special 'seesaw' version of the CPX firmware. This guide will take you through how to install CRICKIT support onto your circuit playground express.

Adafruit recommends editing your code in the Mu Editor which is super simple to use and has features to help us code and see results. See this guide on installing Mu.

Once you have completed this step you're ready to move on.

Testing 1...2...3...

Follow these three steps to get your motors turning

1) Connect Circuit Playground Express to your computer with a micro USB cable

 

2) Copy & paste the code below into the Mu Editor

 

3) Press the Save button in Mu - your code should be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py. If you name the program something else, like panda.py, the Circuit Playground Express won't automatically run it, that's why it's named code.py.

# Code for the Trash Panda tutorial with Adafruit Crickit and Circuit Playground Express
import time
import board
from digitalio import DigitalInOut, Direction
from adafruit_crickit import crickit

# built in LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

# TowerPro servos like 500/2500 pulsewidths, make the wings flap a full 180
crickit.servo_1.set_pulse_width_range(min_pulse=500, max_pulse=2500)
crickit.servo_2.set_pulse_width_range(min_pulse=500, max_pulse=2500)

print("Its TRASH PANDA TIME!")

while True:
    print("tick")
    led.value = True
    crickit.servo_1.angle = 0
    time.sleep(0.5)
    crickit.servo_2.angle = 180
    time.sleep(1.0)

    print("tock")
    led.value = False
    crickit.servo_1.angle = 180
    time.sleep(0.5)
    crickit.servo_2.angle = 0
    time.sleep(1.0)

How The Code Works

We start by importing all our helper libraries

Download: file
# Code for the Trash Panda tutorial with Adafruit Crickit and Circuit Playground Express
import time
import board
from digitalio import DigitalInOut, Direction
from adafruit_crickit import crickit

We'll use the little red LED on the CPX to let us know visually what it's up to, so set that up as an output

Download: file
# built in LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

We get from the crickit library access to the Servo #1 and Servo #2 ports, where we have our servos plugged in. The TowerPro servos we're using work best with a 500us to 2500us pulse width, so we let the servo objects know that.

Download: file
# TowerPro servos like 500/2500 pulsewidths
crickit.servo_1.set_pulse_width_range(min_pulse=500, max_pulse=2500)
crickit.servo_2.set_pulse_width_range(min_pulse=500, max_pulse=2500)

Finally, in our loop we go through a cycle of setting the crickit.servo_1 and crickit.servo_2 angles from 0 to 180 with delays. Try changing those time.sleep() delays to speed up or slow down the movement of your panda! But note, if you try to go too fast, the servo won't have time to move all the way.

We blink the LED so you can see what part of the loop its running, look on your CPX to see the red LED next to the USB connector!

Download: file
while True:
    print("tick")
    led.value = True
    crickit.servo_1.angle = 0
    time.sleep(0.5)
    crickit.servo_2.angle = 180
    time.sleep(1.0)

    print("tock")
    led.value = False
    crickit.servo_1.angle = 180
    time.sleep(0.5)
    crickit.servo_2.angle = 0
    time.sleep(1.0)

Save & Run Code!

Check that both servos are rotating back and forth.

You should also be able to open the REPL within Mu, via pressing the button labeled REPL, and see "tick tock" printing out on your screen.

Long Live Trash Panda!

Exploring further

If you enjoy CircuitPython and want to continue learning you can find lots more CircuitPython projects on the Adafruit Learn System.

This guide was first published on May 24, 2018. It was last updated on May 24, 2018.
This page (Uploading Code with Mu) was last updated on Jul 08, 2020.