Running on an Itsy Bitsy M0 means that we can code this project with CircuitPython!

Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and installation in this tutorial.

We're using the adafruit_motor library for CircuitPython to easily control the servo by declaring the pin as a continuous servo motor.

You can learn about installing the adafruit_motor library in the CircuitPython Essentials Guide on CircuitPlayground Libraries. It is easiest to install the whole library package.

The code is listed below and is available on Adafruit's GitHub repository.

# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

from adafruit_motor import servo
import board
import pwmio
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

pwm = pwmio.PWMOut(board.D5, frequency=50)

switch = DigitalInOut(board.D7)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
pot = AnalogIn(board.A0)

continuous = servo.ContinuousServo(pwm)

def val(pin):
    # divides voltage (65535) to get a value between 0 and 1
    return pin.value / 65535

while True:

    if switch.value:
        continuous.throttle = val(pot) * -1
        continuous.throttle = val(pot) * 1


The potentiometer is going to read a minimum value of 0 and a maximum value of 1 because the servo's speed will range between 0 for no movement and 1 for maximum speed.

Additionally, for the continuous rotation servo, a maximum value of 1 means the maximum speed for clockwise motion and -1 means the maximum speed for counterclockwise motion. This comes into play in the loop, where the switch begins to play a role. For the loop, whether or not the switch is HIGH will determine whether the servo is rotating clockwise or counterclockwise by multiplying the speed registered by the potentiometer by either 1 or -1.

This guide was first published on May 09, 2018. It was last updated on Feb 14, 2024.

This page (CircuitPython Code) was last updated on Feb 14, 2024.

Text editor powered by tinymce.