This tutorial drives one or two DC motors from an ESP32 using a DRV8833 motor driver. The DRV8833 is a small dual H-bridge chip that runs cooler and wastes less power than the bigger L298N, which makes it a good fit for battery-powered robots. You will control speed and direction with plain analogWrite() calls, then wire up a second motor to build a two-wheeled car.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 - DRV8833 DC Motor Driver
What you need
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- 5V DC Motor
- 5V Power Adapter
- DC Power Jack
- Breadboard
- Jumper Wires
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect IN1 and IN2 of the DRV8833 to two PWM-capable ESP32 GPIO pins to control motor A; OUT1 and OUT2 go to the motor's two wires. For a second motor, connect IN3 and IN4 to two more GPIO pins, with motor B on OUT3 and OUT4. VCC takes the motor's external power supply (2.7V to 10.8V), and GND connects to both that supply's negative and a GND pin on the ESP32. Tie EEP (nSLEEP) to 3.3V so the chip stays awake. The motor power always comes from an external adapter, never from the ESP32's 5V or 3.3V pin; the only link between the two power sides is ground.
How it works
The DRV8833 has no separate enable pin like the L298N's ENA/ENB; each channel is driven entirely by its two input pins. Send a PWM signal on IN1 while IN2 stays LOW to spin forward at a chosen speed, or swap them to reverse. Because the chip makes its own internal logic voltage from the motor supply, the ESP32's 3.3V GPIOs can drive it directly, no extra logic pin needed. Both inputs of a channel must support PWM, which is easy on the ESP32 since nearly every GPIO can do it.
Code
The one-motor sketch sets the two input pins as outputs, then cycles through speeding up forward, holding full speed, coasting, running in reverse at about 70%, and braking hard, printing each step to the Serial Monitor. The two-motor version adds motorA(speed) and motorB(speed) helper functions that take a signed value from -255 to 255, where positive spins forward, negative reverses, and 0 coasts, so each car maneuver only needs two lines in loop().
esp32-drv8833-dc-motor-driver.ino
/*
* 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("----------");
}
Things that go wrong
If a motor spins the wrong way, swap its two wires rather than rewriting code. When turning, motor A and motor B should spin in opposite directions so the car rotates on the spot; if it drives in an arc instead, check that logic. Keep the wheels off the table while testing so the car doesn't drive away, and leave the 5V adapter unplugged until the wiring and code are confirmed.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://esp32io.com/tutorials/esp32-drv8833-dc-motor-driver

Top comments (0)