The Raspberry Pi Pico is an enticingly small, yet powerful board that offers users the chance to create projects both great and small. One type of project is robotics and for this we need motors. So how can we control DC motors with a Raspberry Pi Pico? Can we connect them directly to the GPIO? The short answer is no.
The GPIO pins of the Raspberry Pi Pico cannot deliver the current needed for a DC motor and, if we were to try, there is a good chance that we would damage the Pico. Instead we need a motor controller that acts as a bridge between the Pico and our motor. We turn two Pico GPIO pins on / off and they control the motor controller which will in turn, turn on / off two outputs to make the motor move.
For this project you will need
- Raspberry Pi Pico running MicroPython (see how to set up Raspberry Pi Pico)
- Thonny installed on your computer
- 4 x Male to male jumper wires
- Half or full-size breadboard
- Motor controller board. In our case, we used a DRV8833 chip, but L298 or L9110S chips should also work.
- 5V / 6V DC Motor. We used a micro gear metal motor, but a standard DC hobby motor will also work. Note that the motor will need 2 x male to male jumper wires to connect to the breadboard.
Hardware Setup of DC Motor with Raspberry Pi Pico
The chip we are using in this project is a DRV8833 and our particular version is made for breadboards, but there are many other versions including a version designed for embedding in robots. There are also other motor controllers on the market, such as the L298D and L9110S which are simple and affordable components for robotics. All motor controllers share the same input/output conventions.
- Place the Raspberry Pi Pico into the breadboard so that the micro USB port hangs over the end of the breadboard.
- Place the DRV8833 motor controller into the breadboard so that the pins are either side of the central channel.
- Connect the VBUS pin of the Raspberry Pi Pico to the VCC pin of the DRV8833 using a jumper wire. This will power the motor controller directly from the 5V supplied via USB.
- Connect the Raspberry Pi Pico GND pin to the GND pin of the DRV8833.
- Connect GPIO 14 of the Raspberry Pi Pico to IN1 of the DRV8833.
- Connect GPIO 15 of the Raspberry Pi Pico to IN2 of the DRV8833.
- Connect OUT1 and OUT2 to the pins of the motor, it doesn’t matter which for this test.
Software Setup of DC Motor with Raspberry Pi Pico
With the circuit built, connect your Raspberry Pi Pico and open the Thonny application.
import utime
from machine import Pin
2. Create two objects, motor1a and motor1b. These will store the GPIO pin numbers used as outputs to control the DRV8833 motor controller.
motor1a = Pin(14, Pin.OUT)
motor1b = Pin(15, Pin.OUT)
3. Create a function to move the motor “forward”. To do this we need to tell one pin to pull high, the other low. This, in turn, communicates our intended direction to the motor controller and the corresponding output pins will follow suit forcing the motor to move in a set direction.
def forward():
motor1a.high()
motor1b.low()
4. Create a function to move “backward.” This sees the GPIO pin states reverse, causing the motor to spin in the opposite direction.
def backward():
motor1a.low()
motor1b.high()
5. Create a function to stop the motor. By pulling both pins low, we tell the motor controller to stop all movement of the motor.
def stop():
motor1a.low()
motor1b.low()
6. Create a final “test” function that will call the previous functions and run a test sequence that will spin the motor “forward” for two seconds, then “backward” for two seconds. It then stops the motor.
def test():
forward()
utime.sleep(2)
backward()
utime.sleep(2)
stop()
7. Create a for loop which will run the test function five times.
for i in range(5):
test()
Save the code to the Raspberry Pi Pico as motor.py and click on the green arrow to run the code. The motor will rotate in both directions five times.