Raspberry Pi Pico - DRV8833 DC Motor Driver

In this guide, we will learn to set up and use the DRV8833 motor driver with the Raspberry Pi Pico. The DRV8833 is a tiny board, so it fits well on a breadboard next to the Pico, and it runs happily from a 3 to 9 volt battery pack. We will cover the following details:

Raspberry Pi Pico and DRV8833 DC motor driver

If you only want the basics of DC motors with the bigger L298N board, see the Raspberry Pi Pico - DC Motor tutorial.

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 (2 pcs for Part 2)
1×5V Power Adapter
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 DRV8833 Motor Driver

A GPIO pin of the Pico gives only a few milliamps, which is far too weak to spin a motor. The DRV8833 (a chip made by Texas Instruments) sits in the middle: the Pico sends small 3.3 volt signals, and the chip switches the real motor current from a separate power supply. It has two H-bridges inside, so one module can run two brushed DC motors (or one small bipolar stepper motor). Its outputs use MOSFETs with low resistance, so it wastes much less voltage and makes much less heat than the older L298N.

DRV8833 Module
Motor Supply Voltage (VCC) 2.7 volts to 10.8 volts
Logic Signal Voltage 3.3 volts or 5 volts (no separate logic supply needed)
Output Current about 1.5 A per channel, 2 A peak (with good cooling)
Number of Channels 2 (motor A and motor B)
PWM Frequency up to 50 kHz
Protection under-voltage lockout, over-current, thermal shutdown

Pinout

DRV8833 motor driver pinout

The common red or blue DRV8833 breakout has these pins. The labels vary between sellers (some print AIN1/AIN2/BIN1/BIN2, AOUT1/AOUT2/BOUT1/BOUT2, SLP and FLT), but the jobs are the same:

  • VCC pin: connect to the motor power supply (2.7 volts to 10.8 volts). The chip makes its own logic voltage from this pin.
  • GND pin: connect to GND (0 volts). This ground must be shared with the Pico and the motor supply.
  • IN1, IN2 pins: control motor A. Connect to two PWM-capable GPIO pins of the Pico.
  • IN3, IN4 pins: control motor B. Connect to two more PWM-capable GPIO pins.
  • OUT1, OUT2 pins: connect to the two wires of motor A.
  • OUT3, OUT4 pins: connect to the two wires of motor B.
  • EEP pin (nSLEEP): connect to 3.3 volts to turn the driver on. If it is LOW, the chip sleeps and the motors do not move.
  • ULT pin (nFAULT): goes LOW when the chip finds a fault (too hot, too much current, too low voltage). It is optional, you can leave it unconnected.

How the IN Pins Control a Motor

Unlike the L298N, the DRV8833 has no ENA/ENB speed pin. Each motor is controlled only by its pair of IN pins:

IN1 (or IN3) IN2 (or IN4) What the motor does
LOW LOW Coast - the motor is free and slowly rolls to a stop
HIGH LOW Spins forward
LOW HIGH Spins in reverse
HIGH HIGH Brake - the motor stops quickly

To change the speed, the code sends a PWM signal to one IN pin and keeps the other pin LOW. For forward, the PWM goes to IN1 (or IN3). For reverse, the PWM goes to IN2 (or IN4). This is why both IN pins of a motor must be PWM pins. On the Pico this is easy: every GP pin can make PWM, and MicroPython sets the duty with duty_u16(), from 0 (off) to 65535 (full speed).

PART 1: Control One DC Motor

Wiring Diagram

In this part, the motor uses channel B of the DRV8833: it connects to OUT3 and OUT4, and the Pico drives it through IN3 and IN4. The motor gets its power from the 5 volt adapter through the DC power jack, not from the Pico. The Pico only sends the control signals. Make sure the GND of the adapter, the DRV8833 and the Pico are all connected together.

The wiring diagram between Raspberry Pi and Pico DRV8833 one DC motor

This image is created using Fritzing. Click to enlarge image

DRV8833 Module Raspberry Pi Pico / Other
IN3 GP5 (pin 7)
IN4 GP4 (pin 6)
EEP 3V3(OUT) (pin 36)
GND GND (pin 38) and the minus (-) of the DC power jack
VCC the plus (+) of the DC power jack (5 volt adapter)
OUT3, OUT4 the two wires of the DC motor

※ NOTE THAT:

Do not power the motor from the 3V3(OUT), VSYS or VBUS pin of the Pico. A motor pulls a big current when it starts, and this can reset the Pico or damage the USB port.

Raspberry Pi Pico Code - One DC Motor

The script below does one cycle again and again:

  • Slowly speeds the motor up in the forward direction, printing each step.
  • Runs at full speed for 2 seconds.
  • Lets the motor coast to a stop.
  • Runs in reverse at a medium speed.
  • Brakes hard.

The helper motor_b(speed) takes a value from -255 to 255. A positive number spins forward, a negative number spins in reverse, and 0 lets the motor coast.

""" 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-drv8833-dc-motor-driver """ # Raspberry Pi Pico + DRV8833: control ONE DC motor (channel B: IN3/IN4 -> OUT3/OUT4) # The DRV8833 has no speed (enable) pin. Speed comes from PWM on IN3 or IN4. from machine import Pin, PWM import time IN3_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to the IN3 pin of the DRV8833 IN4_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to the IN4 pin of the DRV8833 PWM_FREQ = 1000 # 1 kHz PWM frequency # Both IN pins of a channel need PWM, because the speed signal moves # to IN3 (forward) or to IN4 (reverse) in3 = PWM(Pin(IN3_PIN)) in4 = PWM(Pin(IN4_PIN)) in3.freq(PWM_FREQ) in4.freq(PWM_FREQ) def to_duty(speed): # Convert a speed of 0..255 to a MicroPython duty value of 0..65535 return int(abs(speed) * 65535 // 255) def motor_b(speed): # speed: -255..255. Positive = forward, negative = reverse, 0 = coast speed = max(-255, min(255, speed)) if speed > 0: in3.duty_u16(to_duty(speed)) # PWM on IN3 in4.duty_u16(0) # IN4 LOW elif speed < 0: in3.duty_u16(0) # IN3 LOW in4.duty_u16(to_duty(speed)) # PWM on IN4 else: coast() def coast(): # IN3 = 0, IN4 = 0: the motor is free and rolls to a stop in3.duty_u16(0) in4.duty_u16(0) def brake(): # IN3 = 1, IN4 = 1: the motor stops fast in3.duty_u16(65535) in4.duty_u16(65535) while True: print("Forward: speeding up") for speed in range(0, 256, 15): motor_b(speed) print(" speed =", speed) time.sleep(0.2) motor_b(255) print("Forward: full speed for 2 seconds") time.sleep(2) print("Coast: motor rolls to a stop") coast() time.sleep(2) print("Reverse: speed 180 for 2 seconds") motor_b(-180) time.sleep(2) print("Brake: fast stop") brake() time.sleep(2)

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.
  • New to the Pico? The Raspberry Pi Pico - Getting Started tutorial shows how to install both.
  • Build the circuit as in the wiring diagram above. Keep the 5 volt adapter unplugged for now.
  • Plug the Raspberry Pi Pico into your computer with the USB cable.
  • Open Thonny IDE, then go to Tools Options.
  • In the Interpreter tab, choose MicroPython (Raspberry Pi Pico) and check that the right port is picked (for example COM5 on Windows or /dev/ttyACM0 on Linux).
  • Copy the code above and paste it into the Thonny editor.
  • Save it on the Pico:
    • Press Ctrl+S or click the Save button.
    • Choose Raspberry Pi Pico in the save dialog.
    • Name the file main.py.
  • Now plug in the 5 volt adapter to power the motor.
  • Click the green Run button (or press F5).
  • Watch the motor, and read the messages in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Forward: speeding up speed = 0 speed = 15 speed = 30 speed = 45 ... speed = 225 speed = 240 speed = 255 Forward: full speed for 2 seconds Coast: motor rolls to a stop Reverse: speed 180 for 2 seconds Brake: fast stop Forward: speeding up speed = 0
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

A small motor often does not move at very low speed values. This is normal: it needs a minimum push to beat friction. In the Shell you may see the first few steps printed while the shaft is still not turning.

If the motor spins the wrong way for "forward", swap its two wires on OUT3 and OUT4.

PART 2: Control Two DC Motors

With two motors, the Pico can steer a two-wheel robot car. Motor A is the left wheel and motor B is the right wheel. To turn on the spot, one wheel spins forward and the other spins backward.

Wiring Diagram

Keep everything from Part 1 (the motor on OUT3/OUT4 is now motor B, the right wheel). Add the second motor on OUT1/OUT2 as motor A (the left wheel), and two more signal wires to IN1 and IN2. Both motors share the same 5 volt supply on VCC.

The wiring diagram between Raspberry Pi and Pico DRV8833 two DC motors

This image is created using Fritzing. Click to enlarge image

DRV8833 Module Raspberry Pi Pico / Other
IN1 GP7 (pin 10)
IN2 GP6 (pin 9)
IN3 GP5 (pin 7)
IN4 GP4 (pin 6)
EEP 3V3(OUT) (pin 36)
GND GND (pin 38) and the minus (-) of the DC power jack
VCC the plus (+) of the DC power jack (5 volt adapter)
OUT1, OUT2 motor A (left wheel)
OUT3, OUT4 motor B (right wheel)

Raspberry Pi Pico Code - Two DC Motors

This time a small drive() helper sets both wheels at once and prints the action name. The car moves forward, backward, turns left, turns right, and stops between each move.

""" 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-drv8833-dc-motor-driver """ # Raspberry Pi Pico + DRV8833: control TWO DC motors (a small robot car) # Motor A = left wheel (IN1/IN2), Motor B = right wheel (IN3/IN4) from machine import Pin, PWM import time IN1_PIN = 7 # The Raspberry Pi Pico pin GP7 connected to the IN1 pin of the DRV8833 IN2_PIN = 6 # The Raspberry Pi Pico pin GP6 connected to the IN2 pin of the DRV8833 IN3_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to the IN3 pin of the DRV8833 IN4_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to the IN4 pin of the DRV8833 PWM_FREQ = 1000 # 1 kHz PWM frequency DRIVE_SPEED = 200 # Speed for straight moves (0..255) TURN_SPEED = 160 # Speed for turns (0..255) def make_pwm(pin_number): pwm = PWM(Pin(pin_number)) pwm.freq(PWM_FREQ) pwm.duty_u16(0) return pwm in1 = make_pwm(IN1_PIN) in2 = make_pwm(IN2_PIN) in3 = make_pwm(IN3_PIN) in4 = make_pwm(IN4_PIN) def set_channel(pin_a, pin_b, speed): # speed: -255..255. Positive = forward, negative = reverse, 0 = coast speed = max(-255, min(255, speed)) duty = int(abs(speed) * 65535 // 255) # 0..255 -> 0..65535 if speed > 0: pin_a.duty_u16(duty) pin_b.duty_u16(0) elif speed < 0: pin_a.duty_u16(0) pin_b.duty_u16(duty) else: pin_a.duty_u16(0) pin_b.duty_u16(0) def motor_a(speed): set_channel(in1, in2, speed) def motor_b(speed): set_channel(in3, in4, speed) def drive(action, speed_a, speed_b, seconds): print(action) motor_a(speed_a) motor_b(speed_b) time.sleep(seconds) while True: drive("Car: forward", DRIVE_SPEED, DRIVE_SPEED, 2) drive("Car: stop", 0, 0, 1) drive("Car: backward", -DRIVE_SPEED, -DRIVE_SPEED, 2) drive("Car: stop", 0, 0, 1) drive("Car: turn left", -TURN_SPEED, TURN_SPEED, 1) # A reverse, B forward drive("Car: stop", 0, 0, 1) drive("Car: turn right", TURN_SPEED, -TURN_SPEED, 1) # A forward, B reverse drive("Car: stop", 0, 0, 2)

Detailed Instructions

  • Add the second motor and wires as shown in the diagram.
  • Open the new code in Thonny and save it to the Pico as main.py (replace the old file).
  • Plug in the 5 volt adapter, then click Run.
  • If the car is on the table, lift its wheels off the surface first so it does not drive away.
  • Look at the Shell and compare the messages with the wheels:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Car: forward Car: stop Car: backward Car: stop Car: turn left Car: stop Car: turn right Car: stop Car: forward
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If one wheel turns the wrong way during "Car: forward", swap the two wires of that motor (OUT1/OUT2 for the left wheel, OUT3/OUT4 for the right wheel). You do not need to change the code.

When the script is saved as main.py in the root of the Raspberry Pi Pico, it starts by itself every time the Pico gets power. So you can unplug the USB cable, power the Pico from a battery, and the car will run on its own.

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!