CircuitPython DC Motors

Connections from DC motors to the Circuit Playground Express Crickit is shown at left. There are two Motor drivers, labeled 1 and 2.

 

The center GND terminal is not used for most DC Motor applications.

The Feather Crickit connections for the Motor terminals is shown at left.

Motors are just as easy to use with the Crickit HAT for Raspberry Pi as other versions of Crickit.

You can drive two separate DC motors, so lets go ahead and get right to it!

DC motors are controlled by 4 PWM output pins, the 4 PWM pins let you control speed and direction. And we'll use our adafruit_motor library to help us manage the throttle (speed) and direction for us, making it very easy to control motors

Note that each DC motor is a little different, so just because you have two at the same throttle does not mean they'll rotate at the exact same speed! Some tweaking may be required

The two wires of the DC motor can be plugged in either way into each Crickit Motor port. If the motor spins the opposite way from what you want to call 'forward', just flip the wires!
Download: file
import time
from adafruit_crickit import crickit

print("Dual motor demo!")

# make two variables for the motors to make code shorter to type
motor_1 = crickit.dc_motor_1
motor_2 = crickit.dc_motor_2

while True:
    motor_1.throttle = 1  # full speed forward
    motor_2.throttle = -1 # full speed backward
    time.sleep(1)

    motor_1.throttle = 0.5  # half speed forward
    motor_2.throttle = -0.5 # half speed backward
    time.sleep(1)

    motor_1.throttle = 0  # stopped
    motor_2.throttle = 0  # also stopped
    time.sleep(1)

    motor_1.throttle = -0.5  # half speed backward
    motor_2.throttle = 0.5   # half speed forward
    time.sleep(1)

    motor_1.throttle = -1  # full speed backward
    motor_2.throttle = 1   # full speed forward
    time.sleep(1)

    motor_1.throttle = 0  # stopped
    motor_2.throttle = 0  # also stopped
    time.sleep(0.5)
    
    # and repeat!

Import Libraries

We start by importing the libraries that we need to have time delays ( import time ) and then the main crickit python library that will make it super easy to talk to the motors and sensors on crickit (from adafruit_crickit import crickit)

The crickit object represents the motors and servos available for control. The motors are available on the sub-objects named dc_motor_1 and dc_motor_2

Each of these are adafruit_motor.motor type objects for the curious

To make our code easier to read, we'll make new names for each motor:

Download: file
# make two variables for the motors to make code shorter to type
motor_1 = crickit.dc_motor_1
motor_2 = crickit.dc_motor_2

Control Motor

Now that we have our motor objects, we can simply assign the throttle, this will set the direction and speed. For example, to set the speed to full forward, use motor_1.throttle = 1 and to set to full speed backward use motor_1.throttle = -1. For speeds in between, use a fraction, such as 0.5 (half speed) or 0.25 (quarter speed). Setting the throttle = 0 will stop the motor.

This guide was first published on May 16, 2018. It was last updated on May 16, 2018. This page (CircuitPython DC Motors) was last updated on Nov 19, 2019.