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:

Raspberry Pi Pico DC motor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×DRV8833 Motor Driver Module
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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
DC Motor Pinout

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.

How to control DC motor

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

How to control speed of DC motor by using PWM

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.
Raspberry Pi Pico control DC motor

※ 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

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

The wiring diagram between Raspberry Pi and Pico DC Motor DRV8833 Driver

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.
in2.duty_u16(0) # IN2 stays LOW in1.duty_u16(int(speed * 65535 / 255)) # Set motor speed on IN1, range 0-255

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.
in2.duty_u16(0) in1.duty_u16(int(speed * 65535 / 255))
  • Motor A rotates in the opposite direction to clockwise.
in1.duty_u16(0) in2.duty_u16(int(speed * 65535 / 255))

※ 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.
in1.duty_u16(0) in2.duty_u16(0)
  • Brake: give IN1 and IN2 full duty (HIGH), and the motor comes to a quick stop.
in1.duty_u16(65535) in2.duty_u16(65535)

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
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-dc-motor """ from machine import Pin, PWM from time import sleep # Define pins IN1_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to the IN1 pin of DRV8833 IN2_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to the IN2 pin of DRV8833 # The DRV8833 has no ENA pin: both IN pins use PWM # DRV8833 SLEEP is wired to 3V3(OUT), pin 36, so the driver is awake in1 = PWM(Pin(IN1_PIN)) in2 = PWM(Pin(IN2_PIN)) in1.freq(1000) # Set frequency to 1kHz for PWM in2.freq(1000) def to_duty(speed): return int(speed * 65535 / 255) # Convert 0-255 speed to 0-65535 for PWM def motor_forward(speed): in2.duty_u16(0) # IN2 LOW in1.duty_u16(to_duty(speed)) # PWM on IN1 def motor_backward(speed): in1.duty_u16(0) # IN1 LOW in2.duty_u16(to_duty(speed)) # PWM on IN2 def motor_stop(): in1.duty_u16(0) # both LOW = coast in2.duty_u16(0) while True: # Speed up in the forward direction for speed in range(256): motor_forward(speed) sleep(0.01) sleep(1) # full speed for 1 second # Change direction at full speed motor_backward(255) sleep(1) # Slow down for speed in range(255, -1, -1): motor_backward(speed) sleep(0.01) motor_stop() sleep(1)

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!