ESP8266 - DRV8833 DC Motor Driver

This tutorial instructs you how to use ESP8266 together with the small DRV8833 driver to spin DC motors forward and backward at any speed. In detail, we will learn:

We suggest:

ESP8266 NodeMCU DRV8833 DC motor 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
2×5V DC Motor
1×5V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
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 DRV8833 Motor Driver

The DRV8833 is a tiny dual H-bridge chip made by Texas Instruments. One module can run two brushed DC motors on their own (it can also run one bipolar stepper motor, but that is not covered here). Its outputs are MOSFETs with a low on-resistance of about 360 mΩ, so the motor gets almost all of the supply voltage and the chip stays cool. This is a big step up from the old L298N, which loses around 2V inside itself.

Key facts:

DRV8833
Motor supply (VCC pin) 2.7V to 10.8V
Output current about 1.5A RMS per channel, 2A peak (with good cooling)
Number of motors 2 DC motors (or 1 bipolar stepper)
Logic input level 3.3V or 5V (no separate logic supply needed)
PWM frequency up to 50 kHz
Protection under-voltage lockout, over-current, thermal shutdown

The chip makes its own internal logic voltage from the motor supply. Because of this, the 3.3V signals of the ESP8266 can drive it directly, with no level shifter. If you need more current for one big motor, the two channels can be wired in parallel for about 3A, but this is optional.

The DRV8833 Motor Driver Pinout

DRV8833 motor driver pinout

The common red or blue DRV8833 breakout board has 12 pins:

  • VCC pin: must be connected to the motor power supply (2.7V to 10.8V). This is NOT the ESP8266 3.3V pin.
  • GND pin: must be connected to the ground of the motor supply AND to the GND of the ESP8266
  • IN1, IN2 pins: control inputs for motor A, must be connected to two PWM pins of the ESP8266
  • IN3, IN4 pins: control inputs for motor B, must be connected to two other PWM pins
  • OUT1, OUT2 pins: outputs to motor A
  • OUT3, OUT4 pins: outputs to motor B
  • EEP pin: the sleep pin (nSLEEP). It must be HIGH to turn the driver on. Connect it to 3.3V of the ESP8266. If it is LOW, the chip sleeps and the motors do not move.
  • ULT pin: the fault pin (nFAULT). It goes LOW when the chip detects a problem (over-current, overheating, low voltage). It is optional and can be left unconnected.

※ NOTE THAT:

The pin labels change from one maker to another. Some boards print AIN1/AIN2/BIN1/BIN2, AOUT1/AOUT2/BOUT1/BOUT2, SLP and FLT instead of the names above. The job of each pin is the same. Always follow the labels printed on your own module.

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 input pins. The table below is for motor A (IN1, IN2). Motor B works the same way with IN3 and IN4.

IN1 IN2 Motor A
LOW LOW Coast (motor is released and free-runs to a stop)
HIGH LOW Spins forward
LOW HIGH Spins in reverse
HIGH HIGH Brake (motor stops quickly)

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

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

Because of this, both input pins of each channel must be PWM-capable pins of the ESP8266.

Choosing the ESP8266 Pins

Almost every NodeMCU GPIO can output PWM with analogWrite(), but some pins are a bad choice for a motor driver:

  • D0 (GPIO16) cannot output PWM.
  • D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are boot-mode pins. A device pulling them the wrong way at power-up can stop the ESP8266 from booting.

So in this tutorial we use D5 and D6 for motor A. For motor B we use D1 and D2. D1 and D2 are the default I2C pins, but they are free here because no I2C device is connected. If you add an I2C module later, move motor B to D7 and another free pin.

About the PWM range: since ESP8266 Arduino core 3.0, analogWrite() takes a value from 0 to 255 by default. The older core 2.x used 0 to 1023. The code below calls analogWriteRange(255) in setup(), so 255 always means full speed, no matter which core version you have installed.

How To Program For DRV8833

  • The first step is to define the ESP8266 pins that are connected to IN1 and IN2:
#define PIN_IN1 D5 #define PIN_IN2 D6
  • Set both pins as outputs and fix the PWM range to 0 - 255:
pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); analogWriteRange(255);
  • Spin forward at a given speed (0 to 255):
analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0);
  • Spin in reverse at a given speed:
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed);
  • Coast (let the motor roll to a stop):
analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0);
  • Brake (stop quickly):
analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255);

Part 1 - ESP8266 Controls One DC Motor via DRV8833

Wiring Diagram

The motor is powered by an external 5V adapter through the DC power jack. The ESP8266 only sends the control signals.

The wiring diagram between ESP8266 NodeMCU and DRV8833 one DC motor

This image is created using Fritzing. Click to enlarge image

DRV8833 Module ESP8266 NodeMCU / Other
IN1 D5
IN2 D6
EEP 3.3V
GND GND (ESP8266) and negative (-) of the 5V supply
VCC positive (+) of the 5V supply
OUT1, OUT2 the two wires of the DC motor
ULT not connected

※ NOTE THAT:

  • Never power the motor from the 3.3V or VIN pin of the ESP8266. Motors draw high current and make electrical noise that can reset the board.
  • The GND of the motor supply, the DRV8833 and the ESP8266 must all be connected together. Without a common ground the motor will not respond.

ESP8266 Code for DRV8833 - One Motor

This code speeds the motor up in the forward direction, holds full speed, lets it coast, spins it in reverse at about 70% speed, then brakes. Each step is printed to the Serial Monitor.

/* * 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-drv8833-dc-motor-driver */ /* * ESP8266 NodeMCU - control ONE DC motor with the DRV8833 driver * * Channel A of the DRV8833 is used: * IN1 <- D5 (GPIO14) * IN2 <- D6 (GPIO12) * EEP (sleep pin) is tied to 3.3V, so the driver is always awake. * * The DRV8833 has no enable/speed pin. Speed is set by PWM on IN1 or IN2: * forward : PWM on IN1, IN2 = LOW * reverse : IN1 = LOW, PWM on IN2 * coast : IN1 = LOW, IN2 = LOW * brake : IN1 = HIGH, IN2 = HIGH */ #define PIN_IN1 D5 // ESP8266 pin connected to IN1 of the DRV8833 #define PIN_IN2 D6 // ESP8266 pin connected to IN2 of the DRV8833 void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); // ESP8266 core 3.x uses 0-255 by default, core 2.x used 0-1023. // Setting it here makes the sketch work the same on both versions. analogWriteRange(255); motorCoast(); // start with the motor stopped } void loop() { // 1. Forward: ramp the speed up from 0 to full Serial.println("Forward - speeding up"); for (int speed = 0; speed <= 255; speed += 5) { motorForward(speed); delay(40); } // 2. Hold full speed for 2 seconds Serial.println("Forward - full speed"); delay(2000); // 3. Coast: release the motor, it slows down by itself Serial.println("Coast - motor free-runs to a stop"); motorCoast(); delay(2000); // 4. Reverse at about 70% speed Serial.println("Reverse - 70% speed"); motorReverse(180); delay(2000); // 5. Brake: short the motor for a quick stop Serial.println("Brake - fast stop"); motorBrake(); delay(2000); } // speed: 0 (stopped) .. 255 (full speed) void motorForward(int speed) { analogWrite(PIN_IN1, speed); analogWrite(PIN_IN2, 0); } void motorReverse(int speed) { analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, speed); } void motorCoast() { analogWrite(PIN_IN1, 0); analogWrite(PIN_IN2, 0); } void motorBrake() { analogWrite(PIN_IN1, 255); // 255 = always HIGH analogWrite(PIN_IN2, 255); }

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 above and paste it into the Arduino IDE. No extra library is needed.
  • 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.
  • Open the Serial Monitor and watch the motor:
    • It slowly speeds up, then runs at full speed for 2 seconds.
    • It is released and rolls to a stop.
    • It turns the other way at a lower speed.
    • It stops sharply when the brake is applied.
    ∞
    Newbiely | Arduino IDE 2.3.8
    ──
    ☐
    ✕
    File
    Edit
    Sketch
    Tools
    Help
    Nodemcu 1.0 (ESP-12E Module)
    Newbiely.ino
    ···
    8 Serial.println("Hello World!");
    Output
    Serial Monitor
    Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
    New Line
    9600 baud
    Forward - speeding up Forward - full speed Coast - motor free-runs to a stop Reverse - 70% speed Brake - fast stop Forward - speeding up Forward - full speed Coast - motor free-runs to a stop
    Ln 11, Col 1
    Nodemcu 1.0 (ESP-12E Module) on COM15
    2

    ※ NOTE THAT:

    If the motor turns the "wrong" way, just swap the two motor wires on OUT1 and OUT2. No code change is needed.

Part 2 - ESP8266 Controls Two DC Motors via DRV8833

Now let's use both channels of the DRV8833. Think of motor A as the left wheel and motor B as the right wheel of a small robot car.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and DRV8833 two DC motors

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 Module ESP8266 NodeMCU / Other
IN1 D5
IN2 D6
IN3 D1
IN4 D2
EEP 3.3V
GND GND (ESP8266) and negative (-) of the 5V supply
VCC positive (+) of the 5V supply
OUT1, OUT2 motor A (left wheel)
OUT3, OUT4 motor B (right wheel)

ESP8266 Code for DRV8833 - Two Motors

To keep loop() short, the code uses two helper functions, motorA(speed) and motorB(speed). The speed is a signed number from -255 to 255:

  • A positive value spins the motor forward
  • A negative value spins it in reverse
  • 0 makes it coast

The car drives forward, backward, turns left (left wheel back, right wheel forward), turns right, and then stops.

/* * 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-drv8833-dc-motor-driver */ /* * ESP8266 NodeMCU - control TWO DC motors with the DRV8833 driver * (like the left and right wheels of a small robot car) * * Motor A (left wheel): IN1 <- D5 (GPIO14), IN2 <- D6 (GPIO12) * Motor B (right wheel): IN3 <- D1 (GPIO5), IN4 <- D2 (GPIO4) * EEP (sleep pin) is tied to 3.3V, so the driver is always awake. */ #define PIN_IN1 D5 // Motor A #define PIN_IN2 D6 // Motor A #define PIN_IN3 D1 // Motor B #define PIN_IN4 D2 // Motor B #define DRIVE_SPEED 200 // speed used for driving (0 - 255) #define TURN_SPEED 160 // a bit slower for turning void setup() { Serial.begin(9600); pinMode(PIN_IN1, OUTPUT); pinMode(PIN_IN2, OUTPUT); pinMode(PIN_IN3, OUTPUT); pinMode(PIN_IN4, OUTPUT); analogWriteRange(255); // same 0-255 range on ESP8266 core 2.x and 3.x 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 backward motorB(TURN_SPEED); // right wheel forward delay(1000); Serial.println("Car: turn right"); motorA(TURN_SPEED); // left wheel forward motorB(-TURN_SPEED); // right wheel backward delay(1000); Serial.println("Car: stop"); stopCar(); delay(2000); } // Drive one DRV8833 channel with a signed speed: // speed > 0 -> forward, speed < 0 -> reverse, speed = 0 -> coast void setMotor(int pinA, int pinB, int speed) { speed = constrain(speed, -255, 255); if (speed > 0) { analogWrite(pinA, speed); analogWrite(pinB, 0); } else if (speed < 0) { analogWrite(pinA, 0); analogWrite(pinB, -speed); } else { analogWrite(pinA, 0); analogWrite(pinB, 0); } } void motorA(int speed) { setMotor(PIN_IN1, PIN_IN2, speed); } void motorB(int speed) { setMotor(PIN_IN3, PIN_IN4, speed); } // Brake both motors (both inputs HIGH) for a quick stop void stopCar() { analogWrite(PIN_IN1, 255); analogWrite(PIN_IN2, 255); analogWrite(PIN_IN3, 255); analogWrite(PIN_IN4, 255); }

Detailed Instructions

  • Wire the second motor to OUT3 and OUT4, and connect IN3 and IN4 to D1 and D2, as shown in the diagram.
  • Connect the ESP8266 to your PC with the USB cable.
  • Copy the code above into the Arduino IDE.
  • Click the Upload button to upload it to the ESP8266.
  • Plug in the 5V adapter.
  • Open the Serial Monitor. The output looks like this:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
Car: forward Car: backward Car: turn left Car: turn right Car: stop Car: forward Car: backward Car: turn left
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2
  • If one wheel turns the opposite way during "Car: forward", swap that motor's two wires.

Video Tutorial

Additional Knowledge

Let's compare the DRV8833 with the L298N driver used in the ESP8266 - DC Motor tutorial.

DRV8833 L298N
Motor voltage 2.7V to 10.8V 5V to 35V
Current per channel about 1.5A (2A peak) about 2A
Switch type MOSFET (low loss) BJT (about 2V loss)
Speed control PWM on IN pins PWM on ENA/ENB pins
Pins needed per motor 2 3
Size very small large, with heatsink

It is evident that the DRV8833 is the better choice for small 3V to 9V battery robots, while the L298N is still useful when the motors need 12V or more.

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