ESP32 - DRV8833 DC Motor Driver

This tutorial instructs you how to use ESP32 to spin DC motors through the small DRV8833 motor driver board. We start with a single motor (speed up, slow down, change direction, coast and brake), and then we hook up a second motor so the ESP32 can drive both wheels of a little robot car.

ESP32 DRV8833 DC motor driver

If you already own the bigger L298N board, the ESP32 - DC Motor tutorial covers that one. The DRV8833 is the smaller and cooler-running option, and it fits battery-powered projects very well.

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - DRV8833 DC Motor Driver tutorial.

Hardware Used In This Tutorial

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×DRV8833 Motor Driver Module
2×5V DC Motor
1×5V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

The first part only needs one DC motor. The second motor is for the two-motor part.

Introduction to DRV8833 Motor Driver

The DRV8833 is a dual H-bridge chip made by Texas Instruments. One chip has two channels, so one module can run two brushed DC motors in both directions (or one small bipolar stepper motor, which is not covered here). The outputs are MOSFETs with a low resistance of about 360 mΩ, so very little voltage is lost inside the driver. With the L298N, a good part of a 6V battery is wasted as heat. With the DRV8833, almost all of it reaches the motor.

DRV8833
The motor supply voltage (VCC/VM) 2.7V to 10.8V
The output current per channel about 1.5A RMS, 2A peak (with good cooling)
The control logic 3.3V and 5V signals both work
The PWM frequency up to 50kHz
The protection under-voltage lockout, over-current, thermal shutdown

The chip makes its own internal logic voltage from the motor supply, so the module has no separate logic power pin. This is why the 3.3V GPIO pins of the ESP32 can control it directly. If you need more current for one big motor, the two channels can be connected in parallel for about 3A, but we will not do that here.

DRV8833 Pinout

DRV8833 motor driver pinout

The common red/blue DRV8833 breakout board has these pins. The labels vary between makers: some boards print AIN1/AIN2/BIN1/BIN2, AOUT1/AOUT2/BOUT1/BOUT2, SLP and FLT instead.

  • IN1, IN2 pins: connect these pins to two PWM-capable ESP32 GPIO pins. They control motor A.
  • IN3, IN4 pins: connect these pins to two other PWM-capable ESP32 GPIO pins. They control motor B.
  • OUT1, OUT2 pins: connect these pins to the two wires of motor A.
  • OUT3, OUT4 pins: connect these pins to the two wires of motor B.
  • VCC pin: connect this pin to the positive of the motor power supply (2.7V to 10.8V).
  • GND pin: connect this pin to the negative of the motor power supply AND to a GND pin of the ESP32.
  • EEP pin (nSLEEP): connect this pin to the 3.3V pin of the ESP32 (or to a GPIO set HIGH). When it is LOW, the chip sleeps and the motors do not move.
  • ULT pin (nFAULT): an open-drain output that goes LOW when the chip detects a fault. It is optional, you can leave it unconnected.

How DRV8833 Controls Speed and Direction

Unlike the L298N, the DRV8833 has no ENA/ENB speed pin. Each channel is controlled only by its two input pins. The table below is for motor A. Motor B works the same way with IN3 and IN4.

IN1 IN2 Motor A
LOW LOW Coast (the motor spins freely until it stops)
HIGH LOW Forward
LOW HIGH Reverse
HIGH HIGH Brake (the motor stops quickly)

To set the speed, the ESP32 sends a PWM signal to one input and keeps the other one LOW:

  • To go forward at a chosen speed: PWM on IN1, IN2 LOW
  • To go in reverse at a chosen speed: IN1 LOW, PWM on IN2

This means both input pins of each channel must support PWM. On the ESP32 this is easy, because almost every output GPIO can make PWM. The code in this tutorial uses analogWrite() with a value from 0 (stop) to 255 (full speed), the same function the other ESP32 tutorials on this site use.

How to Control One DC Motor using DRV8833

Wiring Diagram

The motor runs on an external 5V adapter, not on the ESP32. Never take motor power from the 5V or 3.3V pin of the ESP32 board. The only shared link between the two power sides is the ground wire.

  • How to connect ESP32 and the DRV8833 module with jumper wires
ESP32 DRV8833 one DC motor wiring diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and DRV8833 one DC motor using screw terminal block

This image is created using Fritzing. Click to enlarge image

DRV8833 Module ESP32 / Other
IN1 → GPIO27
IN2 → GPIO26
EEP → 3.3V
GND → GND of ESP32 and negative (-) of the 5V adapter
VCC → positive (+) of the 5V adapter
OUT1, OUT2 → the two wires of the DC motor

How To Program

  • Set both input pins as outputs:
pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT);
  • Spin forward at a speed from 0 to 255:
analogWrite(PIN_IN2, 0); analogWrite(PIN_IN1, speed);
  • Spin in reverse at a speed from 0 to 255:
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed);
  • Let the motor coast to a stop:
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0);
  • Brake the motor hard:
analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255);

ESP32 Code - One DC Motor

The code below repeats this cycle: it speeds the motor up in the forward direction, holds full speed, lets it coast, runs it in reverse at about 70% speed, and then brakes. Each step is printed to the Serial Monitor.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-drv8833-dc-motor-driver */ // ESP32 controls ONE DC motor through the DRV8833 motor driver (channel A) // The DRV8833 has no enable pin. The speed comes from a PWM signal on IN1 or IN2. #define PIN_IN1 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833 #define PIN_IN2 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833 void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); // start with the motor coasting (both inputs LOW) analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); } void loop() { // 1) Forward: PWM on IN1, IN2 stays LOW. Ramp the speed from 0 to 255 Serial.println("Forward - speeding up"); analogWrite(PIN_IN2, 0); for (int speed = 0; speed <= 255; speed += 5) { analogWrite(PIN_IN1, speed); delay(40); } // 2) Hold the full speed for 2 seconds Serial.println("Forward - full speed"); delay(2000); // 3) Coast: both inputs LOW, the motor slows down by itself Serial.println("Coast - motor free-runs to a stop"); analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); delay(2000); // 4) Reverse at about 70% speed: IN1 LOW, PWM on IN2 Serial.println("Reverse - 70% speed"); analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 180); delay(2000); // 5) Brake: both inputs HIGH, the motor stops quickly Serial.println("Brake - fast stop"); analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255); delay(2000); // release the brake before the next round analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); Serial.println("----------"); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image. Leave the 5V adapter unplugged for now.
  • Connect the ESP32 board to your PC via a USB Type-C cable.
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE. No extra library is needed.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE.
  • Plug in the 5V adapter to power the motor.
  • Open the Serial Monitor and watch the motor at the same time.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Forward - speeding up Forward - full speed Coast - motor free-runs to a stop Reverse - 70% speed Brake - fast stop ---------- Forward - speeding up Forward - full speed
Ln 11, Col 1
ESP32 Dev Module on COM15
2

※ NOTE THAT:

If the motor turns the "wrong" way when the Serial Monitor says Forward, just swap the two motor wires on OUT1 and OUT2. If the motor does not move at all, check that the EEP pin is connected to 3.3V.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How to Control Two DC Motors using DRV8833

Wiring Diagram

Now add the second motor to OUT3/OUT4 and connect IN3/IN4 to two more ESP32 pins. Think of motor A as the left wheel and motor B as the right wheel of a small car.

  • How to connect ESP32 and the DRV8833 module with jumper wires
ESP32 DRV8833 two DC motors wiring diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and DRV8833 two DC motors using screw terminal block

This image is created using Fritzing. Click to enlarge image

DRV8833 Module ESP32 / Other
IN1 → GPIO27
IN2 → GPIO26
IN3 → GPIO33
IN4 → GPIO32
EEP → 3.3V
GND → GND of ESP32 and negative (-) of the 5V adapter
VCC → positive (+) of the 5V adapter
OUT1, OUT2 → motor A (left)
OUT3, OUT4 → motor B (right)

ESP32 Code - Two DC Motors

To keep loop() short, the code has two helper functions, motorA(speed) and motorB(speed). The speed is a signed number from -255 to 255: a positive value spins forward, a negative value spins in reverse, and 0 lets the motor coast. With these two helpers, each car move needs only two lines.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-drv8833-dc-motor-driver */ // ESP32 controls TWO DC motors through one DRV8833 motor driver (like a small robot car) // Motor A = left wheel, Motor B = right wheel #define PIN_IN1 27 // ESP32 pin GPIO27 connected to the IN1 pin of the DRV8833 (motor A) #define PIN_IN2 26 // ESP32 pin GPIO26 connected to the IN2 pin of the DRV8833 (motor A) #define PIN_IN3 33 // ESP32 pin GPIO33 connected to the IN3 pin of the DRV8833 (motor B) #define PIN_IN4 32 // ESP32 pin GPIO32 connected to the IN4 pin of the DRV8833 (motor B) #define CAR_SPEED 200 // driving speed, from 0 to 255 // Drive one channel with a signed speed: // speed > 0 -> forward (PWM on the first pin) // speed < 0 -> reverse (PWM on the second pin) // speed = 0 -> coast (both pins LOW) void driveChannel(int pinA, int pinB, int speed) { speed = constrain(speed, -255, 255); if (speed > 0) { analogWrite(pinB, 0); analogWrite(pinA, speed); } else if (speed < 0) { analogWrite(pinA, 0); analogWrite(pinB, -speed); } else { analogWrite(pinA, 0); analogWrite(pinB, 0); } } void motorA(int speed) { driveChannel(PIN_IN1, PIN_IN2, speed); } void motorB(int speed) { driveChannel(PIN_IN3, PIN_IN4, speed); } void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); pinMode(PIN_IN3, OUTPUT); pinMode(PIN_IN4, OUTPUT); // both motors stopped at start motorA(0); motorB(0); } void loop() { Serial.println("Car: forward"); motorA(CAR_SPEED); motorB(CAR_SPEED); delay(2000); Serial.println("Car: backward"); motorA(-CAR_SPEED); motorB(-CAR_SPEED); delay(2000); Serial.println("Car: turn left"); // left wheel back, right wheel forward motorA(-CAR_SPEED); motorB(CAR_SPEED); delay(1000); Serial.println("Car: turn right"); // left wheel forward, right wheel back motorA(CAR_SPEED); motorB(-CAR_SPEED); delay(1000); Serial.println("Car: stop"); motorA(0); motorB(0); delay(2000); }

Quick Instructions

  • Do the wiring for two motors as above image.
  • Copy the above code and open with Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to ESP32.
  • Power the motors with the 5V adapter. If you test on a table, lift the wheels off the table first.
  • See the result on Serial Monitor and on the motors:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Car: forward Car: backward Car: turn left Car: turn right Car: stop Car: forward Car: backward
Ln 11, Col 1
ESP32 Dev Module on COM15
2

When turning left, motor A (left) runs backward and motor B (right) runs forward, so the car spins on the spot. Turning right is the opposite. If one wheel goes the wrong way, swap the two wires of that motor only.

※ NOTE THAT:

This tutorial controls the speed in a relative way (a PWM value from 0 to 255). To control the exact speed in rounds per minute, you need a motor with an encoder and a PID control loop.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Language References

※ OUR MESSAGES