How to Control a DC Motor (Or Motors) Using Your Raspberry Pi
Controlling DC motors from your Raspberry Pi is quite easy! Whether you want to control a single motor or build a Raspberry Pi NERF tank, the principles are the same -- but the hardware needed will vary. You can use any Raspberry Pi for this project (Zero, Zero W, 3, 4, etc.)
This guide will cover a basic example -- using the TB6612 to drive a single DC motor for use in my Amazon Echo Furby project. This little chip is offered by Adafruit (and others), but I prefer the Adafruit version as it includes diodes to protect both the Pi and motors. Some chips require you solder your own diode circuit (such as the Sparkfun TB6612FNG), and I'd rather avoid this.
This little chip can drive either two DC motors or one stepper motor. Also, the chip is only $5. Not bad.
There are a few components that make Raspberry Pi DC motor control work:
- Motor driver/controller (TB6612, in this example).
- Motor power supply (e.g. battery).
- Your Pi itself.
GPIO and PWM
Basically, your Pi's GPIO (general purpose input/output) pins connect to the driver board, supplying both board power and data signals that will tell the motor when to run using PWM (pulse-width modulation). We'll then write a simple Python script that will toggle the GPIO pins HIGH or LOW, thus activating the motor!
A separate power source supplies power to the driver board which gets passed directly to the motors. Thus, there are two power sources in total:
- Power for the motors (provided by an external battery/power supply), and
- Power for the motor driver (provided by the Pi's GPIO pins).
Anyways, this is a very simple project that's a great foray into both Raspberry Pi GPIO and PWM. Let's get started!
|Adafruit TB6612 1.2A DC/Stepper Motor Driver||×||1|
Howchoo is reader-supported. As an Amazon Associate, we may earn a small affiliate commission at no cost to you when you buy through our links.