Arduino Nano ESP32 - DC Motor
This tutorial shows how to use Arduino Nano ESP32 to control a DC motor through the DRV8833 Motor Driver. In detail, we will learn how to change the speed and the direction of the DC motor, and how to stop it. We will start with one DC motor, and then see how the same DRV8833 can drive two DC motors.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of DC Motor
DC Motor Pinout
A DC Motor includes two wires: Negative(black), and Positive(red)

How DC Motor Works
The direction and speed of DC motor will be determined by how we provide power to it. The below images how the detaied relation between power and speed/direction

In the case of using PWM, there bigger duty cycle The PWM is, the bigger speed the montor spins.
The following animation shows how a PWM signal is used to control the speed of a DC motor:

How to control speed and direction of DC motor using Arduino Nano ESP32
Firstly, The DC motor works with high voltage that can burn Arduino Nano ESP32 ⇒ We cannot connects DC motor directly to Arduino Nano ESP32. We need a hardware driver between DC motor and Arduino Nano ESP32. The driver takes three responsibilities:
- Protecting Arduino Nano ESP32 from the high voltage
- Receiving the signal from Arduino Nano ESP32 to change the pole of power supply to control the motor's direction.
- Amplifying the PWM signal from Arduino Nano ESP32 (current and voltage) to control the motor's speed

There are a lot of DC motor drivers. This tutorial uses the DRV8833 driver.
Overview of DRV8833 Driver
The DRV8833 module has two small H-bridges inside. With them, it can drive two DC motors, or one small bipolar stepper motor. In this tutorial, we use only one H-bridge (channel A) for one DC motor.
The older L298N loses about 2V inside, so a 5V motor got only about 3V; the DRV8833 uses MOSFET outputs that lose almost nothing, so we use it here. It also stays cool without a heat sink, it is very small, and it reads the 3.3V signals of the Arduino Nano ESP32 directly.
Some numbers to know:
- Motor power on the VCC pin: from 2.7V to 10.8V. A 5V adapter is a good fit for a 5V DC motor.
- Output current: about 1.5A for each channel, up to 2A for a short time.
- Logic: works with 3.3V and 5V boards.
- Built-in protection: over-current, short circuit, overheating and low voltage.
DRV8833 Driver Pinout

The module has 12 pins, six on each side:
- IN1, IN2 pins: the control inputs of motor A. They go to two PWM pins of the Arduino Nano ESP32.
- OUT1, OUT2 pins: the outputs of motor A. The two wires of the DC motor go here.
- IN3, IN4 and OUT3, OUT4 pins: the same for a second motor (motor B). This tutorial does not use them.
- VCC pin: the motor power input. Connect it to the + of the 5V adapter.
- GND pin: connect it to the - of the 5V adapter and to GND of the Arduino Nano ESP32.
- SLEEP pin (printed EEP on some boards): the on/off pin of the chip. Inside, the chip pulls it LOW, and then the driver sleeps. So connect it to the 3V3 pin of the Arduino Nano ESP32 to keep the driver awake.
- FAULT pin: it goes LOW when the chip finds a problem. It is optional, and we do not use it.
How To Control the Speed of DC Motor via DRV8833 Driver
The DRV8833 has no separate speed (enable) pin. The two input pins set both the direction and the speed. To set the speed, we send a PWM signal to one input and keep the other input LOW. On the Arduino Nano ESP32, we make the PWM signal with the analogWrite() function:
The speed is a value between 0 and 255. If it is 255, the motor runs at maximum speed. If it is 0, the motor stops.
How To Control the Direction of DC Motor via DRV8833 Driver
The direction of motor A depends on which input gets the PWM signal. The table below shows what IN1 and IN2 do:
| IN1 pin | IN2 pin | Direction |
|---|---|---|
| PWM | LOW | DC Motor A rotates in clockwise direction |
| LOW | PWM | DC Motor A rotates in anticlockwise direction |
| HIGH | HIGH | DC Motor A brakes (fast stop) |
| LOW | LOW | DC Motor A coasts (free stop) |
Here is how to write it in code:
- Turn motor A in the clockwise direction
- Turn motor A in the anticlockwise direction
※ NOTE THAT:
If the motor turns the wrong way, swap its two wires on the OUT1 and OUT2 pins.
How To Stop DC Motor
There are two ways to stop a DC motor
- Coast: set both inputs LOW. The motor is free and slows down by itself.
- Brake: set both inputs HIGH. The motor stops quickly.
※ NOTE THAT:
On the Arduino Nano ESP32, a pin stays in PWM mode after you use analogWrite() on it. That is why the code uses analogWrite() with 0 for LOW and 255 for HIGH, instead of digitalWrite().
How to control a DC motor using DRV8833 driver.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
| DRV8833 Pin | Connect To |
|---|---|
| IN1 | D10 of Arduino Nano ESP32 |
| IN2 | D9 of Arduino Nano ESP32 |
| GND | GND of Arduino Nano ESP32 and - of the 5V adapter |
| VCC | + of the 5V adapter (through the DC power jack) |
| OUT1, OUT2 | the two wires of the DC motor |
| SLEEP (EEP) | 3V3 of Arduino Nano ESP32 |
| FAULT | not connected |
Jumper wires can come loose when the motor shakes. To make the wiring firm, plug the Arduino Nano ESP32 into a Screw Terminal Expansion Board for Arduino Nano and screw each wire in place. The pins stay the same:

This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
Let's see the below code that does the foloowing thing one by one:
- Arduino Nano ESP32 controls the DC motor's speed increasingly.
- Arduino Nano ESP32 inverts the DC motor's direction
- Arduino Nano ESP32 controls the DC motor's speed decreasingly.
- Arduino Nano ESP32 stops the DC motor
Detailed Instructions
- If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
- Wire the DRV8833, the DC motor and the 5V adapter as in the diagram above. Do not forget the wire from SLEEP to 3V3: without it, the motor does not move.
- Copy the above code and paste it to Arduino IDE.
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- Plug in the 5V adapter.
- Watch the DC motor, You will see:
- DC motor is accelerated and spins at the maximum speed during 2 second
- DC motor's direction is reversed
- DC motor spins in the reverse direction at the maximum speed during 2 seconds
- DC motor is decelerated
- DC motor stop 2 second
- The above process is repeated infinitely.
※ NOTE THAT:
This tutorial provides instructions on how to control the DC motor speed relatively. To control the absolute speed (round per second), it needs to use a PID controller and the feedback from an encoder.
How to Control two DC Motors using DRV8833 Driver
The DRV8833 has a second channel: IN3 and IN4 control it, and the second motor connects to OUT3 and OUT4. To wire and program two motors, for example the two wheels of a small robot car, see the Arduino Nano ESP32 - DRV8833 DC Motor Driver tutorial.