Synchronized Servos

We will use the Mu Python editor to upload the CircuitPython code to Circuit Playground Express and control both servos simultaneously.

If you've not used CircuitPython before, set up and try it out with the Circuit Playground Express by following our installation/tutorial pages for the product, learning about CircuitPython and reading our CircuitPython on Circuit Playground Express guide

If you have not used Mu before, follow this guide on how to install it

For a quick primer on using CircuitPython and Crickit, it is recommended to read through this learn guide.


Make sure you've connected the Circuit Playground Express to your computer and have Mu open and connected to your board.

Copy and paste the code below into your Mu editor.

Save this code to your CIRCUITPY drive in your computer's file exploration program.

# Stumble bot, coded in CircuitPython
# Using an Adafruit Circuit Playground Express, Crickit, and 2 servos
# Dano Wall, Mike Barela for Adafruit Industries, MIT License, May, 2018
import time
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_crickit import crickit

led = DigitalInOut(board.D13)            # Set up Red LED
led.direction = Direction.OUTPUT

button_A = DigitalInOut(board.BUTTON_A)  # Set up switch A
button_A.direction = Direction.INPUT
button_A.pull = Pull.DOWN

# Create servos list
servos = [crickit.servo_1, crickit.servo_2]

# TowerPro servos like 500/2500 pulsewidths
servos[0].set_pulse_width_range(min_pulse=500, max_pulse=2500)
servos[1].set_pulse_width_range(min_pulse=500, max_pulse=2500)

# starting angle, middle
servos[1].angle = 90
servos[0].angle = 90

def servo_front(direction):
    if direction > 0:
        index = 50
        while index <= 100:
            servos[1].angle = index
            index = index + 2
    if direction < 0:
        index = 100
        while index >= 50:
            servos[1].angle = index
            index = index - 2

def servo_back(direction):
    if direction > 0:
        index = 60
        while index <= 90:
            servos[0].angle = index
            index = index + 4
    if direction < 0:
        index = 100
        while index >= 50:
            servos[0].angle = index
            index = index - 4

print("Its Stumble Bot Time")

while True:
    if button_A.value:     # If button A is pressed, start bot
        led.value = True   # Turn on LED 13 to show we're gone!
        for i in range(5):
            print("back 1")
            print("front 1")
            print("back 2")
            print("front 2")
        led.value = False
    # end if
# end while

Click the A button on the Circuit Playground to see it move!

Make sure the file saved to CIRCUITPY is named "", this will allow it to run automatically when your CPX is powered on.


Problem: My servos aren't turning!

Solution: Check that the switches on both CRICKIT and your battery pack are set to "ON".


Problem: My Circuit Playground Express isn't recognized by Mu!

Solution: Make sure your board is set up as a CIRCUITPY device. If it is showing up at "CPLAYBOOT" on your computer, you can follow the steps in this guide to get it back into CIRCUITPY mode.

This guide was first published on May 30, 2018. It was last updated on May 30, 2018.

This page (The Code) was last updated on Oct 15, 2020.