Continuous DC motors can turn all the way around like a wheel. You can get them un-geared where they turn at a stunning 2000 to 6000 times a minute (RPM) or geared where they turn at only about 250 RPM.

Check this page for more info on DC motor types!

You will also need an H-Bridge chip:

Run two solenoids, or a single DC motor with up to 800mA per channel using the super-simple L9110H H-bridge driver. This bridge chip is an 8 DIP package so it's easy to fit onto...
$1.50
In Stock

With an H-bridge driver, you can control the direction the motor turns (clockwise or counter-clockwise) and the speed, from stopped to full speed.

All DC motors have two wires that are used to power and control them

Wiring the Circuit

Follow the diagram above to breadboard the circuit. These are the connections you'll make:

Pico

  • Pico pin 34 (PWM6A)  to H-bridge pin 7 (Input B)
  • Pico pin 32 (PWM5B) to H-bridge pin 6 (Input A)
  • Pico GND to H-bridge pin 8 (GND)
  • Pico GND to H-bridge pin 5 (GND)
  • Pico pin 30 (RESET) to reset button to GND

Power

  • Power supply 5V+ to 47uF capacitor to GND
  • 5V rail to H-bridge pin 2 (VCC)
  • 5V rail to H-bridge pin 3 (VCC)

Motor

  • either motor wire to H-bridge pin 1 (Output A)
  • other motor wire to H-bridge pin4 (Output B)
The Pico is not connected to the +5V power supply, and will be powered over USB.

Code

Copy the code from the element below, and paste it into a fresh file in Mu. Save the file to your Pico's CIRCUITPY drive as code.py It will automatically run the code!

This is the basic motor test code that will run the motor at two different speeds in each direction and then stop. It's a great way to check that everything's working before moving on to something more complex.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example uses an L9110 H-bridge driver to run a DC Motor using two PWM pins.
#  https://www.adafruit.com/product/4489

# Hardware setup:
#   DC motor via L9110 H-bridge driver on two PWM pins that are on their own channels
#   e.g., RP2040 Pico pins GP28, GP27

import time
import board
import pwmio
from adafruit_motor import motor

PWM_PIN_A = board.GP28  # pick any pwm pins on their own channels
PWM_PIN_B = board.GP27

# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
motor1 = motor.DCMotor(pwm_a, pwm_b)

print("***DC motor test***")

print("\nForwards slow")
motor1.throttle = 0.5
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nForwards")
motor1.throttle = 1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards")
motor1.throttle = -1.0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards slow")
motor1.throttle = -0.5
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("  throttle:", motor1.throttle)
time.sleep(1)

print("\nSpin freely")
motor1.throttle = None
print("  throttle:", motor1.throttle)

print("\n***Motor test is complete***")

How It Works

Libraries

First, the libraries are imported for time so it can pause, board for pin definitions, pwmio for Pulse Width Modulation (PWM), and the motor module from adafruit_motor for easy motor control.

import time
import board
import pwmio
from adafruit_motor import motor

Setup

Two PWM pins are used to control the two H-bridge drivers in the L9110. PWM is a digital approximation of an analog signal, allowing a smooth-ish range of varying control to be specified for the motor speed. By using two PWM pins and the dual H-bridge driver, the motor can be set to move both forward and backward.

The Pico has eight channels of PWM, with two pins per channel. In order to clock them separately, the two pins chosen must be on different PWM channels.

The PWM outputs will be set to a particular pin and PWM frequency (the duty cycle is what will be varied by the motor.throttle() control).

Finally, the motor object is created with motor.DCMotor(pwm_a, pwm_b).

PWM_PIN_A = board.GP28
PWM_PIN_B = board.GP27

pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
motor1 = motor.DCMotor(pwm_a, pwm_b)

Control

Once the motor has been set up, it is controlled with the motor.throttle command. It can be set to anywhere from -1.0 (full speed backward) to 1.0 (full speed forward). Practically speaking, a throttle value between -0.3 to 0.3 won't turn the motor, so keep that in mind.

A throttle of 0 stops the motor, while a value of None releases it to rotate freely.

motor1.throttle = 0.5
time.sleep(1)

motor1.throttle = 0
time.sleep(1)

motor1.throttle = 1.0
time.sleep(1)

motor1.throttle = 0
time.sleep(1)

motor1.throttle = -1.0
time.sleep(1)

motor1.throttle = 0
time.sleep(1)

motor1.throttle = -0.5
time.sleep(1)

motor1.throttle = 0
time.sleep(1)

motor1.throttle = None

This guide was first published on Feb 17, 2021. It was last updated on 2021-02-17 14:48:05 -0500.

This page (DC Motors) was last updated on Apr 14, 2021.

Text editor powered by tinymce.