Raspberry Pi Pico - DC Motor
In this guide, we will show you how to control the DC motor using the Raspberry Pi Pico. We will cover the following details:
- How a DC motor works
- Using a DRV8833 module to make a DC motor go faster, slower, forward or backward
- How to program Raspberry Pi Pico to control speed and direction of a DC motor

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of DC Motor
DC Motor Pinout
A DC motor comes with two wires.
- Positive wire: usually red
- Negative wire: usually black

How It Works
When you buy a DC motor, it is important to know the voltage it needs. For example, if you have a DC motor that requires 12 volts, you should connect it to a 12-volt power source.
- Connect 12V to the positive wire and GND to the negative wire to make the motor rotate clockwise at full speed.
- Connect 12V to the negative wire and GND to the positive wire to make the motor rotate counterclockwise at full speed.
When you swap the connections of two wires on the DC motor as explained before, the way it spins will change. This method controls the spinning direction of the DC motor. Instead of doing this manually, it is done by programming.
When we provide less than 12V to DC motors, they still operate but not at their maximum speed. This indicates that the motor's speed varies with different voltages. Directly adjusting the voltage can be difficult in practice. Therefore, a more straightforward way to control the speed of a DC motor is used. We maintain a constant voltage from the power supply and manipulate the speed using a PWM (Pulse Width Modulation) signal. The motor spins faster with a higher PWM duty cycle and slower with a lower one.

The following animation shows how a PWM signal is used to control the speed of a DC motor:

How to control DC motor using Raspberry Pi Pico
To manage a DC motor using a Raspberry Pi Pico, you must handle both its speed and its movement direction. The Raspberry Pi Pico generates a PWM signal which is not strong enough to operate the motor on its own. Consequently, you need a hardware driver that links the Raspberry Pi Pico to the DC motor. This driver has two important functions:
- Boost the power and voltage of the PWM signal from the Raspberry Pi Pico to control speed.
- Get the control signal from the Raspberry Pi Pico to switch the power supply's polarity to control direction.

※ NOTE THAT:
- This guide works for all small DC motors. Here, we're using a 5V DC motor to show you how.
- A Pico pin gives only 3.3V and a few milliamps. That is far too little for a 5V DC motor, so you always need a driver between the Raspberry Pi Pico and the motor.
The driver we use for that job in this guide is the DRV8833.
Overview of DRV8833 Driver
This guide will show you how to control a DC motor using the DRV8833 Driver. The DRV8833 is a tiny module with MOSFET switches. They waste almost no voltage, so the 5V motor really runs at about 5V. This is the main reason it took the place of the L298N in this guide.
DRV8833 Driver Pinout

This little board drives two separate DC motors at the same time. We call them motor A and motor B. There are 12 pins in total. VCC and GND share a row with the four inputs IN1, IN2, IN3 and IN4. Everything else (SLEEP, OUT1, OUT2, OUT3, OUT4 and FAULT) is on the other row.
The common pins for both motors:
- VCC pin: gives power to the motors. The voltage can be from 2.7V to 10.8V.
- GND pin: this is a common ground pin and should connect to GND (0V).
- SLEEP pin (EEP on some boards): inside the chip this pin is pulled LOW, so on its own the driver sleeps. The driver wakes up only when the pin is HIGH. Connect it to 3V3(OUT) of the Pico.
- FAULT pin: goes LOW if the chip gets too hot or sees too much current. It is optional and not used here.
Motor A pins (Channel A):
- IN1 & IN2 pins: They decide both the direction and the speed of Motor A. A PWM signal on one pin, with the other pin LOW, makes Motor A spin.
- OUT1 & OUT2 pins: These are linked to Motor A.
Motor B pins (Channel B):
- IN3 & IN4 pins: These control Motor B in the same way.
- OUT3 & OUT4 pins: These pins connect to Motor B.
A few more facts about the DRV8833:
- Each channel gives about 1.5A continuous and 2A peak.
- It needs no logic power pin, and the 3.3V signals of the Pico work directly.
- It protects itself from over-current, short circuits, over-heating and low supply voltage.
- It is very small and needs no heat sink.
You can control two DC motors simultaneously with a Raspberry Pi Pico and a DRV8833 Driver. To manage each motor, you need to connect two PWM pins from the Raspberry Pi Pico.
※ NOTE THAT:
This guide will teach you how to control a DC motor using channel A. You can control another DC motor in a similar method.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| DRV8833 Pin | Raspberry Pi Pico Pin |
|---|---|
| IN1 | GP5 (pin 7) |
| IN2 | GP4 (pin 6) |
| SLEEP | 3V3(OUT) (pin 36) |
| GND | GND |
- DRV8833 VCC goes to the + terminal of the DC power jack, and DRV8833 GND also goes to the - terminal. So the 5V adapter, the driver and the Pico share one ground.
- DRV8833 OUT1 and OUT2 go to the two wires of the DC motor.
- FAULT, IN3, IN4, OUT3 and OUT4 are not connected.
- Do not power the motor from a pin of the Pico. Use the 5V power adapter.
How To Control the Speed of DC Motor via DRV8833 Driver
The DRV8833 has no ENA pin. To change the speed, you send a PWM signal to one IN pin and keep the other IN pin at 0. Here is how you do it:
- Connect two pins from the Raspberry Pi Pico to IN1 and IN2 on the DRV8833 module, and set both as PWM pins.
- Use the duty_u16() function to send a PWM signal to IN1 (or IN2). The DRV8833 driver passes this PWM signal to the DC motor with the full 5V.
The speed can be set between 0 and 255. At 0, the motor will not move. At 255, the motor will move at its fastest speed.
How To Control the Direction of DC Motor via DRV8833 Driver
The pin that gets the PWM signal decides the direction. The table below shows all four cases for motor A.
| IN1 pin | IN2 pin | Direction |
|---|---|---|
| LOW | LOW | Motor A coasts to a stop |
| HIGH | HIGH | Motor A brakes (fast stop) |
| PWM | LOW | Motor A spins Clockwise |
| LOW | PWM | Motor A spins Anti-Clockwise |
- Motor A turns to the right.
- Motor A rotates in the opposite direction to clockwise.
※ NOTE THAT:
If the motor turns the wrong way, swap its two wires on OUT1 and OUT2, or swap IN1 and IN2 in the program code.
How To Stop DC Motor Spinning
There are two ways to switch off a DC motor with the DRV8833.
- Coast: give IN1 and IN2 a duty of 0, and the motor rolls freely until it stops.
- Brake: give IN1 and IN2 full duty (HIGH), and the motor comes to a quick stop.
How to control a DC motor using DRV8833 driver.
Raspberry Pi Pico Code
The code below performs the following actions:
- Speed up the DC motor
- Change the direction
- Slow down the DC motor
- Stop the motor
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Connect the Raspberry Pi Pico to the DC motor via DRV8833 module according to the provided diagram. Do not forget the SLEEP wire to 3V3(OUT).
- Plug the 5V power adapter into the DC power jack.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Observations:
- The DC motor speeds up, then rotates at maximum speed for 1 second.
- The direction of the DC motor changes.
- The DC motor rotates at maximum speed for 1 second in the reverse direction.
- The DC motor slows down.
- The DC motor pauses for 1 second.
- This cycle keeps repeating.
- Motor does not move? Check that SLEEP is connected to 3V3(OUT). If SLEEP is free, the DRV8833 stays asleep.
※ NOTE THAT:
In this guide, we will learn how to change the speed of a DC motor compared to its fastest speed. To manage the exact speed in rotations per second, we need to use a PID controller and an encoder. We will discuss how to control the exact speed of the DC motor in another guide.