Code with CircuitPython

If you like, you can code this project using CircuitPython!

Getting Familiar

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. Here are some guides which cover the basics:

Be sure you have the latest CircuitPython loaded onto your board per the second guide.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

Download Library Files

Plug your Circuit Playground Express board into your computer via a USB cable. Please be sure the cable is a good power+data cable so the computer can talk to the Circuit Playground Express board.

A new disk should appear in your computer's file explorer/finder called CIRCUITPY. This is the place we'll copy the code and code library. If you can only get a drive named CPLAYBOOT, load CircuitPython per the guide above.

Create a new directory on the CIRCUITPY drive named lib.

Download the latest CircuitPython motor driver to your computer using the green button below. Match the library you get to the version of CircuitPython you are using. Save to your computer's hard drive where you can find it.

With your file explorer/finder, browse to the bundle and open it up. Copy the following folder from the library bundle to your CIRCUITPY lib directory you made earlier:

  • adafruit_motor

All of the other necessary code is baked into CircuitPython!

Download Code

Below is the code for this project. Select download code.py below and save it to your computer's hard drive where you can find it.

# Lucky Cat Maneki-neko with Circuit Playground Express
# Mike Barela for Adafruit Industries, MIT License

import time
import board
import pulseio
from adafruit_motor import servo

# create a PWMOut object on Pin A1
pwm = pulseio.PWMOut(board.A1, frequency=50)

# Create a servo object, my_servo
my_servo = servo.Servo(pwm)

while True:
    for angle in range(50):         # 0 to 49 degrees in 1 deg steps
        my_servo.angle = angle
        time.sleep(0.005)           # Tiny delay each steps
    time.sleep(0.25)                # More time at end of arm down
    for angle in range(50, 0, -1):  # 50 to 0 degrees in 1 deg steps
        my_servo.angle = angle
        time.sleep(0.005)
    time.sleep(0.25)                # More time when arm up

Load the code.py file into Mu. Double click the tab code.py (save as) and select your Circuit Playground Express CIRCUITPY flash drive. When the file copies over, it will start immediately, as you have the USB powered so be careful of a moving paw. If it did not start, check you saved code.py onto the CIRCUITPY flash drive.

Adjusting the timing of the servo

You will notice the timing of the time.sleep functions is different from the MakeCode values. MakeCode and CircuitPython code run at different speeds when converted to machine code.

You can change the values for the time.sleep but make small changes, lower = faster, higher = slower.

This guide was first published on Feb 05, 2019. It was last updated on Feb 05, 2019. This page (Code with CircuitPython) was last updated on Jun 25, 2019.