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.

Arduino Nano ESP32 DC motor with DRV8833 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
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×Jumper Wires
1×Breadboard
1×Optionally, DC Power Jack
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 .

Overview of DC Motor

DC Motor Pinout

A DC Motor includes two wires: Negative(black), and Positive(red)

DC Motor Pinout

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

How to control DC motor

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 of DC motor by using PWM

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
Arduino Nano ESP32 control DC motor

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

DRV8833 Motor 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:

analogWrite(PIN_IN1, speed); // PWM on IN1 sets the speed analogWrite(PIN_IN2, 0); // IN2 stays LOW

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
analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0);
  • Turn motor A in the anticlockwise direction
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed);

※ 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.
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0);
  • Brake: set both inputs HIGH. The motor stops quickly.
analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255);

※ 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

The wiring diagram between Arduino Nano ESP32 and DC Motor DRV8833 Driver

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:

The wiring diagram between Arduino Nano ESP32 and DC Motor DRV8833 Driver Screw Terminal

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
/* * 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-dc-motor */ #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 // The setup function runs once on reset or power-up void setup() { // initialize the two control pins as outputs. pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); // start with the motor stopped (IN1 = LOW, IN2 = LOW) analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); } // The loop function repeats indefinitely void loop() { // clockwise: PWM on IN1, IN2 stays LOW analogWrite(PIN_IN2, 0); for (int speed = 0; speed <= 255; speed++) { analogWrite(PIN_IN1, speed); // speed up delay(10); } delay(2000); // rotate at maximum speed 2 seconds in clockwise direction // change direction: IN1 LOW, full PWM on IN2 analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 255); delay(2000); // rotate at maximum speed for 2 seconds in anti-clockwise direction for (int speed = 255; speed >= 0; speed--) { analogWrite(PIN_IN2, speed); // speed down delay(10); } // now IN1 = LOW and IN2 = LOW: the motor coasts to a stop delay(2000); // stop motor 2 seconds }

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.

Video Tutorial

Language References

※ 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!