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.
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
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:
# 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.
Page last edited March 08, 2024
Text editor powered by tinymce.