Code with CircuitPython

The program we'll use will do three primary things:

  • play audio files
  • rotate Servo 1 back and forth to move the eyes
  • rotate Servo 2 back and forth to flap the wings

This is pretty straightforward in CircuitPython, especially because the Crickit and Circuit Playground Express combination is capable of playing back wave files while still doing other things!

First, we'll get everything set up, then we'll look at the code.

Setup the CPX and Crickit

The Circuit Playground Express (CPX) paired with the Crickit is a powerful, yet simple to use combination for building animatronics! To get started, you'll want to set up the CPX for use with CircuitPython by following this guide. When you're ready, and can upload code to the board return here.

To use the Crickit with the CPX, follow the steps listed here to install the special build of CircuitPython, as well as the latest library bundle.

Adafruit really likes using the Mu editor to edit the CircuitPython code. See this guide on loading and using Mu.

Playing Audio

The Circuit Playground Express plays back .wav files. If you would like to prepare your own files, follow the instructions in this guide. Download the following files and then uncompress the .zip file. Copy the .wav files to your CPX, which shows up as CPLAYBOOT on your computer.

Servo Eyes and Wings

To move the eyes and flap the wings, we'll send commands to the servo motors. Typical servos have a 180° range of motion, from 0° to 180° with 90° being the center position.

When our code starts we'll center both servos by telling them to go to 90°. Then, as each audio file is played back we'll send commands to the servos to swing back and forth between positions.

Here's the code. Download it and then save it to your CPX as main.py

import time
import board
import audioio
from adafruit_crickit import crickit

# Minerva Owl Robot

wavefiles = ["01.wav", "02.wav", "03.wav", "04.wav",
             "05.wav", "06.wav", "07.wav", "08.wav"]

# Two servos
eye_servo = crickit.servo_1
wing_servo = crickit.servo_2
# TowerPro servos like 500/2500 pulsewidths
eye_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
wing_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)

# Servo angles
EYES_START = 90
EYES_LEFT = 110
EYES_RIGHT = 70
WINGS_START = 90
WINGS_END = 160

# Starting servo locations
eye_servo.angle = EYES_START
wing_servo.angle = WINGS_START

# Audio playback object and helper to play a full file
a = audioio.AudioOut(board.A0)


def play_file(wavfile):
    print("Playing", wavfile)
    with open(wavfile, "rb") as f:
        wav = audioio.WaveFile(f)
        a.play(wav)
        while a.playing:  # turn servos, motors, etc. during playback
            eye_servo.angle = EYES_LEFT
            time.sleep(.25)
            eye_servo.angle = EYES_START
            time.sleep(.25)
            wing_servo.angle = WINGS_END
            time.sleep(.2)
            wing_servo.angle = WINGS_START
            time.sleep(.2)
            eye_servo.angle = EYES_RIGHT
            time.sleep(.25)
            eye_servo.angle = EYES_START
            time.sleep(.25)


while True:
    for i in range(8):
        play_file(wavefiles[i])
        time.sleep(2.5)

Editing Your Code

If you'd like to make changes to the code at any time, Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython on your computer (Mac, PC, Linux). You can learn about Mu and installation in this tutorial.

This guide was first published on Jun 27, 2018. It was last updated on Jun 27, 2018. This page (Code with CircuitPython) was last updated on Aug 16, 2019.