Arduino Nano ESP32 - DRV8833 DC Motor Driver

In this guide, we will drive small DC motors from the Arduino Nano ESP32 through a DRV8833 motor driver module. The Nano ESP32 only gives 3.3V signals, and the DRV8833 accepts them as they are, so no level shifter is needed. In detail, we will learn:

Arduino Nano ESP32 DRV8833 DC motor driver

Hardware Preparation

1×Arduino Nano ESP32
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 Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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 .

※ NOTE THAT:

For Part 1 you only need one DC motor. The second motor is used in Part 2.

Overview of DRV8833 Motor Driver

A GPIO pin of the Arduino Nano ESP32 can give only a few milliamps, far too little to turn a motor. The DRV8833 from Texas Instruments sits in between: it holds two H-bridges, one for each motor, and switches the motor current from an external supply based on the signals it gets from the Nano ESP32. Because it uses MOSFET outputs, it loses much less voltage and runs cooler than the old L298N, which makes it a nice match for 3V to 9V battery robots. It can also drive one small bipolar stepper motor, but this guide covers DC motors only.

DRV8833 motor driver module pinout

Main features:

DRV8833
Motor supply (VCC pin) 2.7V to 10.8V
Logic signals 3.3V or 5V (no separate logic supply needed)
Output current about 1.5A RMS per channel, 2A peak with good cooling
Channels 2 (two DC motors), can be joined in parallel for about 3A on one motor
PWM frequency up to 50kHz
Protection under-voltage, over-current, over-temperature

Pinout

Labels vary between makers. Some boards print IN1/IN2/OUT1, others print AIN1/AIN2/AOUT1, SLP or FLT. The job of each pin is the same:

  • IN1, IN2 pins: control motor A. Connect to two PWM pins of the Arduino Nano ESP32.
  • IN3, IN4 pins: control motor B. Connect to two more PWM pins.
  • OUT1, OUT2 pins: connect to the two wires of motor A.
  • OUT3, OUT4 pins: connect to the two wires of motor B.
  • VCC pin: connect to the + of the motor power supply (2.7V to 10.8V).
  • GND pin: connect to the - of the motor power supply AND to GND of the Arduino Nano ESP32.
  • EEP pin (sleep): connect to 3.3V to turn the driver on. If this pin is LOW, the chip sleeps and the motors do not move.
  • ULT pin (fault): goes LOW when the chip finds a problem (over-current, overheating, low voltage). It is optional, you can leave it unconnected.

How the DRV8833 Controls a Motor

Unlike the L298N, the DRV8833 has no ENA/ENB speed pin. Each motor is controlled only by its two inputs. The table shows what happens on motor A (motor B works the same with IN3 and IN4):

IN1 IN2 Motor A
LOW LOW Coast (the motor is free and slows down by itself)
HIGH LOW Forward
LOW HIGH Reverse
HIGH HIGH Brake (the motor stops fast)

To change speed, send a PWM signal to one input and keep the other LOW:

  • Forward with speed: PWM on IN1, IN2 = LOW.
  • Reverse with speed: IN1 = LOW, PWM on IN2.

That is why all four IN pins must go to PWM-capable pins. On the Arduino Nano ESP32, every digital pin can output PWM, so we use D10, D9, D6 and D5.

※ NOTE THAT:

Never power a motor from the 3.3V or 5V pin of the Arduino Nano ESP32. Use an external supply for the VCC pin of the DRV8833, and connect all GNDs together. Without a shared GND, the driver cannot read the control signals.

If you want to compare, the Arduino Nano ESP32 - DC Motor tutorial does the same job with the bigger L298N driver.

PART 1: Control One DC Motor with the DRV8833

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and DRV8833 one DC motor

This image is created using Fritzing. Click to enlarge image

DRV8833 Module Arduino Nano ESP32 / Others
IN1 → D10
IN2 → D9
EEP → 3V3
GND → GND (and - of the 5V adapter)
VCC → + of the 5V adapter (via DC power jack)
OUT1, OUT2 → the two wires of the DC motor

How To Program

  • Set the two control pins as outputs.
pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT);
  • Run forward at a chosen speed (0 to 255).
analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0);
  • Run in reverse at a chosen speed.
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed);
  • Brake quickly by setting both inputs HIGH, or coast by setting both LOW.
analogWrite(PIN_IN1, 255); // brake analogWrite(PIN_IN2, 255);

※ NOTE THAT:

On the Arduino Nano ESP32, once a pin is used with analogWrite(), it stays in PWM mode. So the code writes 0 for LOW and 255 for HIGH with analogWrite() instead of mixing in digitalWrite().

Arduino Nano ESP32 Code - One DC Motor

The sketch below speeds the motor up slowly, holds full speed, lets it coast, runs it backward, and then brakes. Each step is printed to the Serial Monitor.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-drv8833-dc-motor-driver */ // One DC motor on channel A of the DRV8833 module. // The DRV8833 has no ENA pin: the speed comes from PWM on IN1 or IN2. // On the Nano ESP32, a pin used by analogWrite() stays in PWM mode, so this code // also uses analogWrite() for LOW (0) and HIGH (255) instead of digitalWrite(). #define PIN_IN1 D10 // The Arduino Nano ESP32 pin connected to the IN1 pin of the DRV8833 #define PIN_IN2 D9 // The Arduino Nano ESP32 pin connected to the IN2 pin of the DRV8833 void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); motorCoast(); // start with the motor free (both inputs LOW) Serial.println("DRV8833 ready"); } void loop() { // 1) Spin forward and ramp the speed from 0 up to full Serial.println("Forward: speeding up"); for (int speed = 0; speed <= 255; speed += 5) { motorForward(speed); delay(40); } // 2) Keep full speed for 2 seconds Serial.println("Forward: full speed"); delay(2000); // 3) Coast: cut the drive, the motor slows down by itself Serial.println("Coast (free stop)"); motorCoast(); delay(2000); // 4) Spin in reverse at about 70% speed Serial.println("Reverse: 70% speed"); motorReverse(180); delay(2000); // 5) Brake: both inputs HIGH, the motor stops quickly Serial.println("Brake (fast stop)"); motorBrake(); delay(2000); } // Forward: PWM on IN1, IN2 stays LOW. speed = 0..255 void motorForward(int speed) { analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0); } // Reverse: IN1 stays LOW, PWM on IN2. speed = 0..255 void motorReverse(int speed) { analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed); } // Coast: IN1 = LOW, IN2 = LOW void motorCoast() { analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); } // Brake: IN1 = HIGH, IN2 = HIGH void motorBrake() { analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255); }

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the DRV8833, the motor and the 5V adapter as shown above. Double-check that EEP goes to 3V3.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE. No extra library is needed.
  • Click the Upload button to send the code to the Arduino Nano ESP32.
  • Plug in the 5V adapter, then open the Serial Monitor.
  • Watch the motor: it speeds up, runs fast, glides to a stop, turns the other way, then stops sharply.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
DRV8833 ready Forward: speeding up Forward: full speed Coast (free stop) Reverse: 70% speed Brake (fast stop) Forward: speeding up Forward: full speed
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

※ NOTE THAT:

If "Forward" makes the motor spin the wrong way, just swap the two motor wires on OUT1 and OUT2. If the motor does not move at all, check the EEP pin first.

PART 2: Control Two DC Motors with the DRV8833

One DRV8833 has room for two motors, which is exactly what a two-wheel robot car needs. Motor A is the left wheel and motor B is the right wheel. To turn, one wheel goes forward while the other goes back.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and DRV8833 two DC motors

This image is created using Fritzing. Click to enlarge image

DRV8833 Module Arduino Nano ESP32 / Others
IN1 → D10
IN2 → D9
IN3 → D6
IN4 → D5
EEP → 3V3
GND → GND (and - of the 5V adapter)
VCC → + of the 5V adapter (via DC power jack)
OUT1, OUT2 → left motor (motor A)
OUT3, OUT4 → right motor (motor B)

Arduino Nano ESP32 Code - Two DC Motors

The code has a small helper for each motor: motorA(speed) and motorB(speed). The speed goes from -255 to 255. A positive number means forward, a negative number means reverse, and 0 lets the motor coast. With these two helpers, the car moves forward, backward, turns left, turns right and stops.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-drv8833-dc-motor-driver */ // Two DC motors (a small robot car) with one DRV8833 module. // Motor A = left wheel (IN1, IN2), Motor B = right wheel (IN3, IN4). // On the Nano ESP32, a pin used by analogWrite() stays in PWM mode, so this code // also uses analogWrite() for LOW (0) and HIGH (255) instead of digitalWrite(). #define PIN_IN1 D10 // The Arduino Nano ESP32 pin connected to the IN1 pin of the DRV8833 (motor A) #define PIN_IN2 D9 // The Arduino Nano ESP32 pin connected to the IN2 pin of the DRV8833 (motor A) #define PIN_IN3 D6 // The Arduino Nano ESP32 pin connected to the IN3 pin of the DRV8833 (motor B) #define PIN_IN4 D5 // The Arduino Nano ESP32 pin connected to the IN4 pin of the DRV8833 (motor B) #define DRIVE_SPEED 200 // speed used for driving, 0..255 #define TURN_SPEED 160 // a bit slower when turning void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); pinMode(PIN_IN3, OUTPUT); pinMode(PIN_IN4, OUTPUT); stopCar(); } void loop() { Serial.println("Car: forward"); motorA(DRIVE_SPEED); motorB(DRIVE_SPEED); delay(2000); Serial.println("Car: backward"); motorA(-DRIVE_SPEED); motorB(-DRIVE_SPEED); delay(2000); Serial.println("Car: turn left"); motorA(-TURN_SPEED); // left wheel goes back motorB(TURN_SPEED); // right wheel goes forward delay(1000); Serial.println("Car: turn right"); motorA(TURN_SPEED); // left wheel goes forward motorB(-TURN_SPEED); // right wheel goes back delay(1000); Serial.println("Car: stop"); stopCar(); delay(2000); } // Signed speed: -255..255. Positive = forward, negative = reverse, 0 = coast void motorA(int speed) { setChannel(PIN_IN1, PIN_IN2, speed); } void motorB(int speed) { setChannel(PIN_IN3, PIN_IN4, speed); } void setChannel(int pinX1, int pinX2, int speed) { speed = constrain(speed, -255, 255); if (speed > 0) { // forward: PWM on the first pin analogWrite(pinX1, speed); analogWrite(pinX2, 0); } else if (speed < 0) { // reverse: PWM on the second pin analogWrite(pinX1, 0); analogWrite(pinX2, -speed); } else { // 0: coast analogWrite(pinX1, 0); analogWrite(pinX2, 0); } } // Brake both motors (all inputs HIGH) so the car stops quickly void stopCar() { analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255); analogWrite(PIN_IN3, 255); analogWrite(PIN_IN4, 255); }

Detailed Instructions

  • Add the second motor and the IN3/IN4 wires as shown in the diagram.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button to send the code to the Arduino Nano ESP32.
  • Lift the car (or hold the motors) so the wheels can spin freely, then plug in the 5V adapter.
  • Open the Serial Monitor and compare the printed action with what the wheels do.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' 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
Arduino Nano ESP32 on COM15
2

※ NOTE THAT:

If the car turns when it should go straight, one motor is wired backward. Swap the two wires of that motor on its OUT pins.

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!