ESP8266 - DC Motor

This tutorial instructs you how to use ESP8266 to control DC Motor. In detail, we will learn:

ESP8266 NodeMCU NodeMCU controls the speed and direction of a 5V DC motor with a DRV8833 driver

Hardware Preparation

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12F (SMD-form)
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×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

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 has two wires, the positive one usually being red and the negative one black.

DC Motor pinout

How It Works

When purchasing a DC motor, it is essential to identify the voltage at which it operates. For instance, let us consider a 12V DC motor.

When you power the 12V DC motor by a 12V power source:

  • Connect 12V and GND to the positive wire and negative wire, respectively: the DC motor rotates at maximum speed in the clockwise direction
  • Connect 12V and GND to the negative wire and positive wire, respectively: the DC motor rotates at maximum speed in the anti-clockwise direction

As previously mentioned, by swapping the power pole between two wires of the DC motor, the direction of rotation is reversed. This is a method used to control the direction of the DC motor, not manually but through programming.

If the power supplied to DC motors is less than 12V, the motor will still rotate but not at its highest speed. This implies that by adjusting the voltage of the power source, we can alter the speed of the DC motor. Nevertheless, this approach is not commonly used due to the complexity of controlling the voltage of the power source. Therefore, the voltage of the power source is kept constant and the speed of the DC motor is regulated through a PWM signal. The greater the duty cycle of the PWM, the faster the DC motor rotates.

How to control DC motor

How to control DC motor using ESP8266

Controlling a DC motor involves two components: speed and direction. ESP8266 can generate a PWM signal, but it has low voltage and current, so it cannot be used to control the DC motor. To bridge the gap between ESP8266 and the DC motor, a hardware driver is needed. This driver performs two tasks:

  • Amplifying the PWM signal from ESP8266 in terms of current and voltage for speed control
  • Receiving the control signal from ESP8266 to switch the polarity of the power supply for direction control.
ESP8266 NodeMCU control DC motor

※ NOTE THAT:

  • The example in this tutorial uses a 5V DC motor. If your motor runs on 12V, use the BTS7960 driver instead, as shown in the ESP8266 - BTS7960 Motor Driver tutorial.
  • When you are controlling a 5V DC motor, you still need a driver in between the ESP8266 and the DC motor. The ESP8266 pin gives only 3.3V and a very small current, which is not enough for the DC motor.

There are numerous types of chips and modules that can be used as DC motor drivers. In this tutorial, we will be utilizing the small DRV8833 driver.

Overview of DRV8833 Driver

The DRV8833 Driver is a tiny board that can manage two DC motors or one bipolar stepper motor. This tutorial instructs you how to use it to control a DC motor.

Its outputs are MOSFET switches. They lose almost no voltage, so a 5V motor really gets about 5V. The L298N, which this page used before, lost about 2V, so the same motor got only about 3V. The DRV8833 also stays cool without a heat sink, and it reads the 3.3V signals of the ESP8266 directly.

  • Motor supply (VCC pin): 2.7V to 10.8V
  • Output current: about 1.5A RMS per channel, 2A peak
  • Protection: over-current, short circuit, thermal shutdown and under-voltage lockout

DRV8833 Driver Pinout

DRV8833 Driver pinout

The DRV8833 Driver is capable of controlling two DC motors independently simultaneously, referred to as motor A and motor B. This Driver has 12 pins in total, six on each side.

The common pins for both motors:

  • VCC pin: This pin supplies power for the motors, with a voltage range of 2.7 to 10.8V. In this tutorial, it gets 5V from the power adapter.
  • GND pin: This is a common ground pin. It goes to the GND of the ESP8266 and to the minus of the power adapter.
  • SLEEP pin (EEP on some boards): LOW puts the driver to sleep, and HIGH wakes it up. Inside the chip it is pulled LOW, so if you leave it free, the driver sleeps and the motor never moves. Wire it to the 3V3 pin of the NodeMCU.
  • FAULT pin: This pin goes LOW when the chip finds a problem. It is optional, and we do not use it here.

Motor A pins (Channel A):

  • IN1 & IN2 pins: They are the control pins of Motor A, for speed as well as direction. There is no separate speed pin. Both must be connected to PWM pins of the ESP8266.
  • OUT1 & OUT2 pins: These pins are connected to Motor A.

Motor B pins (Channel B):

  • IN3 & IN4 pins: These pins set the direction and the speed of Motor B, in the same way as IN1 and IN2.
  • OUT3 & OUT4 pins: These pins are connected to Motor B and provide power to it.

The DRV8833 driver needs only one power input, on the VCC and GND pins. It makes its own logic voltage inside, so no 5V logic supply is needed.

An ESP8266 and a DRV8833 Driver can run two DC motors at the same time, and each motor can do something different. To regulate each motor, we only require two pins from the ESP8266.

※ NOTE THAT:

The remainder of this tutorial will focus on controlling a DC motor using channel A. Similar steps can be taken to control the other DC motor.

How To Control the Speed of DC Motor via DRV8833 Driver

The DRV8833 has no ENA pin. To set the speed, we send a PWM signal to one input pin and keep the other input pin LOW. This can be done by:

  • Using one ESP8266 pin that can do PWM for IN1, and another one for IN2 of the DRV8833
  • Generating a PWM signal on IN1 (or on IN2 for the other direction) by using analogWrite() function. The DRV8833 Driver will amplify the PWM signal to the DC motor.
analogWrite(PIN_IN1, speed); // speed is a value from 0 to 255 analogWrite(PIN_IN2, 0);

The speed can range from 0 to 255. When the speed is 0, the motor will cease to move. At a speed of 255, the motor will spin at its maximum velocity. Old ESP8266 cores used a range of 0 to 1023, so the code calls analogWriteRange(255) in setup() to make 255 the full speed on every core.

How To Control the Direction of DC Motor via DRV8833 Driver

The input pin that gets the PWM signal chooses the direction. The table below shows how to control the direction of channel A.

IN1 pin IN2 pin Direction
LOW LOW Motor A stops (coasts)
HIGH HIGH Motor A stops (brakes)
PWM LOW Motor A spins Clockwise
LOW PWM Motor A spins Anti-Clockwise

Accordingly:

  • ESP8266 code for rotating in a clockwise direction.
analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0);
  • ESP8266 code for rotating in a anti-clockwise direction.
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed);

※ NOTE THAT:

The rotation of the DC motor can be reversed by connecting the OUT1 and OUT2 pins in an opposite manner.

How To Stop DC Motor Spinning

There are two methods for stopping a DC motor:

  • Drive IN1 and IN2 to LOW together, so the motor gets no power and coasts to a stop.
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0);
  • When both inputs, IN1 and IN2, go HIGH, the driver brakes the motor, so it stops faster.
analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255);

How to control a DC motor using DRV8833 driver.

Wiring Diagram

The NodeMCU, the DRV8833 and the motor are connected with jumper wires only.

The wiring diagram between ESP8266 NodeMCU and NodeMCU DC Motor DRV8833 Driver

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

DRV8833 Pin Connect To
IN1 ESP8266 D5 (GPIO14)
IN2 ESP8266 D6 (GPIO12)
GND ESP8266 GND, and (-) of the 5V adapter via the DC power jack
VCC (+) of the 5V adapter via the DC power jack
OUT1, OUT2 the two wires of the DC motor
SLEEP ESP8266 3V3
FAULT not connected

D5 and D6 both support PWM, and they are not boot pins, so the motor cannot disturb the start-up of the ESP8266.

ESP8266 Code

The following code does:

  • Increases the speed of a DC motor
  • Alters the direction
  • Decreases the speed of a DC motor
  • Halts the motor
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-dc-motor */ #define PIN_IN1 D5 // The ESP8266 pin D5 (GPIO14) connected to the IN1 pin of the DRV8833 #define PIN_IN2 D6 // The ESP8266 pin D6 (GPIO12) connected to the IN2 pin of the DRV8833 // The setup function runs once on reset or power-up void setup() { // initialize digital pins as outputs. pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); analogWriteRange(255); // PWM values from 0 to 255 on every ESP8266 core version analogWrite(PIN_IN1, 0); // both inputs LOW: the motor does not move analogWrite(PIN_IN2, 0); } // The loop function repeats indefinitely void loop() { // clockwise: IN2 stays LOW, the PWM value on IN1 sets the speed 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); } delay(2000); // both inputs are LOW now: the motor stops for 2 seconds }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram. Keep the 5V adapter unplugged for now.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
  • Plug in the 5V adapter to power the motor.
  • If the motor does not move at all, look at the SLEEP wire first: it must go to 3V3, or the DRV8833 stays asleep.
  • You should observe:
    • The DC motor will speed up and then rotate at its maximum speed for 2 seconds.
    • The direction of the DC motor will be changed.
    • The DC motor will spin at its maximum speed in the opposite direction for 2 seconds.
    • The DC motor will slow down.
    • The DC motor will stop for 2 seconds.
    • This process will repeat itself.

    ※ NOTE THAT:

    This tutorial instructs you how to control the speed of a DC motor in relation to its maximum speed. To control the absolute speed (in revolutions per second), we will need to use a PID controller and an encoder. Controlling the absolute speed of the DC motor will be discussed in a separate tutorial.

How to Control two DC Motors using DRV8833 Driver

Motor B goes on OUT3 and OUT4, and IN3 and IN4 take two more PWM pins of the ESP8266. A full two-motor example, with robot car moves, is in the ESP8266 - DRV8833 DC Motor Driver tutorial.

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!