CircuitPython Code

Getting familiar with 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.

Preparing your Board

To get your Circuit Playground Express set up to run this code, follow these steps:

1) Install the latest CircuitPython for CPX from CircuitPython.org. For this guide, please use version 4.0.0 or greater. Version 3.1.2 is not as efficient on memory and may give an error.

2) Get the latest library pack, unzip it, and drag the library folder adafruit_motor 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, read information here.)

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. You can select Download in the upper left tp save the code onto your computer.

# CircuitPython code for the Gyroscopic Marble Maze
# Adafruit Industries, 2019. MIT License
import time
import board
import pulseio
from adafruit_motor import servo
import simpleio
from adafruit_circuitplayground.express import cpx

# create a PWMOut object on Pin A2.
pwm1 = pulseio.PWMOut(board.A1, duty_cycle=2 ** 15, frequency=50)
pwm2 = pulseio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50)

# Create a servo object, my_servo.
my_servo1 = servo.Servo(pwm1)
my_servo2 = servo.Servo(pwm2)

NUM_READINGS = 8
roll_readings = [90] * NUM_READINGS
pitch_readings = [90] * NUM_READINGS

def Average(lst):
    return sum(lst) / len(lst)

while True:
    x, y, z = cpx.acceleration  # x = green
    print((x, y, z))

    roll = simpleio.map_range(x, -9.8, 9.8, 0, 180)
    roll_readings = roll_readings[1:]
    roll_readings.append(roll)
    roll = Average(roll_readings)

    print(roll)

    my_servo1.angle = roll

    pitch = simpleio.map_range(y, -9.8, 9.8, 0, 180)
    pitch_readings = pitch_readings[1:]
    pitch_readings.append(pitch)
    pitch = Average(pitch_readings)

    print(pitch)

    my_servo2.angle = pitch

    time.sleep(0.05)

Open the code up in the Mu editor. 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

Alternatively, you can use your operating system file explorer/finder to copy code.py to the CIRCUITPY drive.

The board will restart itself and start running the code.

Changing the Code

The default settings used in this code have been optimized for the particular servos used, but can be tweaked to suit your particular preferences. For example, you can increase NUM_READINGS from 8 up to 12 if you feel the maze is too jerky, or decrease it down to 2 (but not below 2) if you feel the maze is too slow to respond.

You can also update time.sleep from 0.05 to something like 0.20 to make the movement of the servos smoother.

Once your code is uploaded to Circuit Playground Express you can connect some motors to your board and see what this code does! Read on...

This guide was first published on Jun 24, 2019. It was last updated on Jun 24, 2019. This page (CircuitPython Code) was last updated on Jan 27, 2020.