ESP8266 - BTS7960 Motor Driver

A garage door opener is a good example of a heavy motor job. One strong 12V or 24V DC gear motor pulls the door up, waits, and later lets it down again. The ESP8266 has Wi-Fi on board, so a NodeMCU could one day open that door from your phone. The problem: a NodeMCU pin can only give a tiny current, and a door motor can need many amps. The BTS7960 motor driver module sits in the middle and does the heavy work. This tutorial instructs you how to use ESP8266 NodeMCU to run a 12V DC motor through the BTS7960, in both directions and at any speed. In detail, we will learn:

ESP8266 NodeMCU NodeMCU controls a 12V DC motor both ways with a BTS7960 43A driver

We suggest:

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×BTS7960 Motor Driver Module
1×12V DC Motor
1×12V Power Adapter
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 BTS7960 Motor Driver

BTS7960 Motor Driver Module Pinout
image source: diyables.io

The DIYables BTS7960 43A Motor Driver Module is the same board that many shops sell as "IBT-2" or "HW-039". On the top side of the PCB, you find two Infineon BTS7960B chips, one 74HC14 chip and the 8-pin control header. A big heat sink is bolted under the board to take the heat away. Each BTS7960B is a half-bridge, and two half-bridges make one full H-bridge. So the module drives one brushed DC motor, forward or backward, with the speed set by PWM. Next to small drivers like the L298N or the DRV8833, it is the big brother: it is made for large gear motors, wiper motors, winches, scooters and garage door motors.

BTS7960 Motor Driver Module
Driver type one full H-bridge, made of two BTS7960B half-bridges
Motors it can drive 1 brushed DC motor, both directions
Motor supply on B+ / B- 5.5V to 27V DC (for 6V, 12V or 24V motors)
Current up to 43A (the current-limit level of the chips, a peak value)
PWM on RPWM / LPWM up to 25 kHz
Input side a 74HC14 buffer chip, fed from VCC
Built-in safety over-temperature protection and current limiting

How It Works in a Few Words

Each half-bridge looks after one motor wire and links it either to B+ or to B-. When one half links its wire to B+ and the other half links its wire to B-, current flows through the motor in one direction. Let the two halves trade jobs, and the current, and so the motor, go the other way. The speed comes from PWM: the ESP8266 switches the RPWM or LPWM pin on and off many times a second. The bigger the "on" part of each cycle (the duty cycle), the faster the motor runs. The 74HC14 chip sits between the ESP8266 and the big chips and passes the control signals on.

The BTS7960 Motor Driver Pinout

The control header has 2 x 4 male pins. Its top row is VCC, R_IS, R_EN, RPWM. The bottom row is GND, L_IS, L_EN, LPWM. The four large screw terminals on the other edge are for the power supply and the motor.

  • RPWM pin: must be connected to a PWM-capable pin of the ESP8266. A PWM signal here spins the motor forward (right half-bridge).
  • LPWM pin: must be connected to another PWM-capable pin. A PWM signal here spins the motor in reverse (left half-bridge).
  • R_EN pin: must be connected to a digital output pin. HIGH turns the right half-bridge on.
  • L_EN pin: must be connected to a digital output pin. HIGH turns the left half-bridge on.
  • R_IS and L_IS pins: they report the current and any fault. This tutorial does not need them, so they stay free.
  • VCC pin: must be connected to the VIN pin of the NodeMCU (5V from USB). It feeds only the 74HC14 logic chip and the power LED, never the motor.
  • GND pin: must be connected to a GND pin of the NodeMCU. On the module, this pin and B- are joined.
  • B+ and B- terminals: the plus and the minus of the 12V motor supply.
  • M+ and M- terminals: the two wires of the DC motor.

※ NOTE THAT:

Read the printed text, not the pin position. Look for "R_S" and "L_S" on the DIYables board: these are R_IS and L_IS. Other IBT-2 style boards may print the labels a little differently. Check your own module before you plug in any wire.

Which Pin Levels Move the Motor

When R_EN and L_EN are both HIGH, the two PWM pins choose what the motor does:

RPWM LPWM Result
PWM (0 to 255) 0 forward, a higher value gives more speed
0 PWM (0 to 255) reverse, a higher value gives more speed
0 0 brake, the driver joins M+ and M-, so the motor stops fast

When R_EN and L_EN are both LOW, both half-bridges turn off. Nothing drives or holds the motor now, so it coasts: it keeps rolling and slows down by itself. A brake stops a heavy load in a short time. A coast is softer, but it takes longer.

One rule never changes: at least one of RPWM and LPWM must be 0. Do not send PWM to both of them at the same time.

Two Power Sides: Logic and 12V Motor

  • Logic side: VCC goes to the VIN pin of the NodeMCU, which carries 5V from the USB cable. VCC powers only the 74HC14 buffer chip, and 5V is the value the module maker gives for it. The buffer still accepts the 3.3V signals of the ESP8266, so D5 to D8 can drive it with no extra parts. Its inputs have pull-down resistors, so no 5V gets back into a NodeMCU pin.
  • Motor side: the 12V power adapter feeds B+ and B- through a DC power jack, plus to B+ and minus to B-. The adapter has to supply more current than the garage door motor draws. Keep B+ away from the NodeMCU: no pin of the board may ever get 12V.
  • Ground: GND and B- are already joined on the module. So the single GND wire from the NodeMCU to the header is enough to give the board and the 12V adapter one common ground.
  • No way back: R_IS and L_IS stay free, so no signal comes back from the module to the ESP8266. No high voltage can reach its 3.3V pins.

What does "43A" mean for you? It is the level where the current limit of the chips steps in, a peak value. A motor that runs for a long time should draw much less than that. For a motor that runs a long time, the heat sink sets the real limit.

Wiring Diagram

In the picture, six jumper wires run straight from the VIN, GND, D5, D6, D7 and D8 pins of the NodeMCU to the header of the BTS7960. No breadboard is used. The motor and the DC power jack of the 12V adapter are screwed into the big terminal block of the module.

The wiring diagram between ESP8266 NodeMCU and NodeMCU BTS7960 motor 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.

BTS7960 Module ESP8266 NodeMCU / 12V Side
VCC VIN (5V from USB)
GND GND
R_EN D5 (GPIO14)
L_EN D6 (GPIO12)
LPWM D7 (GPIO13)
RPWM D8 (GPIO15)
R_IS, L_IS not used, no wire
B+ + of the 12V power adapter (via the DC power jack)
B- - of the 12V power adapter (via the DC power jack)
M+, M- the 12V DC motor (one wire on each)

WARNING

Plug in the 12V adapter last, after every wire is in place. Before you do it, check that + goes to B+ and - goes to B-. Swapped polarity can damage the module.

Why These Six NodeMCU Pins?

On the NodeMCU, GND, D5, D6, D7 and D8 sit side by side in one row. Their order is the same as the order of the wires on the header of the module, so these five jumper wires run next to each other and do not cross. VIN is on the other side of the board. It gives 5V when the NodeMCU runs on USB, so its wire goes to VCC on its own. All of D5 to D8 can make PWM with analogWrite(), and RPWM (D8) and LPWM (D7) need that.

D8 (GPIO15) is a boot pin that must be LOW at start-up, but the NodeMCU pulls it LOW and the BTS7960 input does not pull it HIGH, so it is fine here.

Some other pins are kept free on purpose:

  • D0 (GPIO16) cannot make PWM.
  • D3 (GPIO0) and D4 (GPIO2) must be HIGH at start-up, so it is better not to hang a driver input on them.
  • D1 and D2 stay free for an I2C screen or sensor later, for example a small display that shows if the door is open.

How To Program For BTS7960 Motor Driver

No library is needed. Two built-in functions do all the work: analogWrite() sets the speed, and digitalWrite() switches the enable pins.

  • Give each NodeMCU pin a name. D5 to D8 are the names printed on the board, and the ESP8266 core knows them:
#define PIN_RPWM D8 #define PIN_LPWM D7 #define PIN_R_EN D5 #define PIN_L_EN D6
  • In setup(), make the four pins outputs and fix the PWM range at 0 to 255. New ESP8266 cores already use this range, but old 2.x cores used 0 to 1023. With this line, 255 is full speed on both:
pinMode(PIN_RPWM, OUTPUT); pinMode(PIN_LPWM, OUTPUT); pinMode(PIN_R_EN, OUTPUT); pinMode(PIN_L_EN, OUTPUT); analogWriteRange(255);
  • Put 0 on both PWM pins first. Only then wake the driver up with both enable pins HIGH:
analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); digitalWrite(PIN_R_EN, HIGH); digitalWrite(PIN_L_EN, HIGH);
  • For forward, set LPWM to 0 and put the speed (0 to 255) on RPWM. For reverse, swap the two pins:
analogWrite(PIN_LPWM, 0); analogWrite(PIN_RPWM, speed);
  • For a brake, write 0 to RPWM and LPWM and leave the enable pins HIGH.
  • For a coast, write 0 to both PWM pins and pull R_EN and L_EN LOW. Set them HIGH again before the motor should run.

ESP8266 NodeMCU Code for BTS7960 Motor Driver

The sketch below puts those lines into small functions: motorForward(), motorReverse(), motorBrake(), motorCoast(), and enableDriver() for the two enable pins. Then loop() repeats one test run, again and again:

  • Forward: speed up from 0 to 255 in small steps, hold full speed for 2 seconds, then slow down to 0
  • Brake for 1 second
  • Reverse for 2 seconds at a value of 128, which is about half speed
  • Coast for 2 seconds with the driver off, then switch the driver on again

At the start of each step, the NodeMCU sends a short line over USB, so you can follow along in 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-bts7960-motor-driver */ /* * ESP8266 NodeMCU drives ONE 12V DC motor through a BTS7960 (IBT-2) module * * Header wires (GND, D5, D6, D7, D8 sit in one row on the NodeMCU; VIN is on the other side): * VCC <- VIN (5V from USB, logic power of the module only) * GND <- GND (on the module, GND is joined to B-) * R_EN <- D5 (GPIO14) * L_EN <- D6 (GPIO12) * LPWM <- D7 (GPIO13) * RPWM <- D8 (GPIO15) * R_IS, L_IS: not connected * * With R_EN and L_EN HIGH: * RPWM = speed, LPWM = 0 -> forward * RPWM = 0, LPWM = speed -> reverse * RPWM = 0, LPWM = 0 -> brake (fast stop) * With R_EN and L_EN LOW -> coast (the motor rolls to a stop) */ #define PIN_RPWM D8 // NodeMCU D8 (GPIO15) -> RPWM, PWM for forward #define PIN_LPWM D7 // NodeMCU D7 (GPIO13) -> LPWM, PWM for reverse #define PIN_R_EN D5 // NodeMCU D5 (GPIO14) -> R_EN, right half-bridge on/off #define PIN_L_EN D6 // NodeMCU D6 (GPIO12) -> L_EN, left half-bridge on/off #define SPEED_STEP 3 // 255 / 3 = 85 steps per ramp #define STEP_TIME 20 // ms per step, so one ramp takes about 1.7 s #define HALF_SPEED 128 // about 50% duty cycle void setup() { Serial.begin(9600); pinMode(PIN_RPWM, OUTPUT); pinMode(PIN_LPWM, OUTPUT); pinMode(PIN_R_EN, OUTPUT); pinMode(PIN_L_EN, OUTPUT); // 255 = full speed on every ESP8266 core (old 2.x cores used 1023) analogWriteRange(255); // no PWM yet, then wake the driver up analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); enableDriver(); Serial.println("BTS7960 on NodeMCU: driver enabled, motor still"); } void loop() { Serial.println("FORWARD soft start RPWM 0 .. 255"); for (int speed = 0; speed <= 255; speed += SPEED_STEP) { motorForward(speed); delay(STEP_TIME); } Serial.println("FORWARD top speed RPWM 255 for 2 s"); delay(2000); Serial.println("FORWARD soft stop RPWM 255 .. 0"); for (int speed = 255; speed >= 0; speed -= SPEED_STEP) { motorForward(speed); delay(STEP_TIME); } Serial.println("BRAKE quick stop RPWM 0, LPWM 0 for 1 s"); motorBrake(); delay(1000); Serial.println("REVERSE half speed LPWM 128 for 2 s"); motorReverse(HALF_SPEED); delay(2000); Serial.println("COAST free roll R_EN, L_EN LOW for 2 s"); motorCoast(); delay(2000); enableDriver(); // the enable pins must be HIGH again before the next forward run } // R_EN and L_EN HIGH: both half-bridges are allowed to work void enableDriver() { digitalWrite(PIN_R_EN, HIGH); digitalWrite(PIN_L_EN, HIGH); } // speed: 0 (stopped) .. 255 (full speed) void motorForward(int speed) { analogWrite(PIN_LPWM, 0); // always set the other side to 0 first analogWrite(PIN_RPWM, speed); } void motorReverse(int speed) { analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, speed); } // both PWM inputs 0 while the driver is on: // the two motor wires are joined, so the motor stops fast void motorBrake() { analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); } // driver off: nothing holds the motor, it slows down by itself void motorCoast() { analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); digitalWrite(PIN_R_EN, LOW); digitalWrite(PIN_L_EN, LOW); }

Detailed Instructions

  • New to the ESP8266? Follow the how to setup environment for ESP8266 on Arduino IDE tutorial first.
  • Make the six jumper-wire connections from the table, straight from the NodeMCU pins to the module header. Screw the motor wires to M+ and M-, and the DC power jack to B+ and B-. Keep the 12V adapter unplugged.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the board NodeMCU 1.0 (ESP-12E Module) and the COM port it uses.
  • Paste the code into the Arduino IDE and click Upload. Leave the 12V side without power during the upload.
  • As soon as the IDE says the upload is finished, connect the 12V adapter to the jack. Now the motor side has power.
  • Open the Serial Monitor at 9600 baud, and watch the motor and the messages together:
∞
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
BTS7960 on NodeMCU: driver enabled, motor still FORWARD soft start RPWM 0 .. 255 FORWARD top speed RPWM 255 for 2 s FORWARD soft stop RPWM 255 .. 0 BRAKE quick stop RPWM 0, LPWM 0 for 1 s REVERSE half speed LPWM 128 for 2 s COAST free roll R_EN, L_EN LOW for 2 s FORWARD soft start RPWM 0 .. 255 FORWARD top speed RPWM 255 for 2 s
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Listen to the two kinds of stop. After "BRAKE", the motor stops almost at once. After "COAST", it spins down slowly.

A few things you may notice:

  • The motor turns the wrong way during "FORWARD"? Unplug the 12V adapter and swap the M+ and M- wires. The code stays the same.
  • A soft hum or whine at low speed is normal. The ESP8266 makes its PWM at about 1 kHz, and people can hear that sound.
  • Some strange characters at the top of the Serial Monitor are normal too. The ESP8266 prints its start-up messages at another speed (74880 baud).

Additional Knowledge

BTS7960, L298N or DRV8833?

Two of these drivers have ESP8266 tutorials on this site: the BTS7960 on this page and the DRV8833, which the ESP8266 - DC Motor tutorial uses too. Here is a quick look at how all three differ:

BTS7960 L298N DRV8833
Motors per module 1 2 2
Current up to 43A (peak) about 2A per motor about 1.5A per motor
Voltage lost inside the driver very little about 2V very little
ESP8266 pins per motor (in these tutorials) 4 3 2

It is evident that the BTS7960 is the right pick when one motor needs a lot of current, like a garage door or a winch. When you only need to spin two little motors, the tiny DRV8833 board does the job with fewer wires.

From Test Bench to a Wi-Fi Garage Door

This page only runs a fixed test. But the ESP8266 has Wi-Fi, so the next steps are not far away:

  • Open and close from your phone: put two buttons on a web page, like in the ESP8266 - Web Server tutorial, and call motorForward() or motorReverse() when one of them is pressed.
  • Stop at the ends: a real door must stop when it is fully open or fully closed. The ESP8266 - DC Motor Limit Switch tutorial shows how a limit switch stops a motor.
  • Join a smart home: send "open" and "close" commands to the NodeMCU through MQTT from a home dashboard.

A real garage door also needs safety parts, such as a sensor that stops the door when something is in its way. Do not connect your test setup to a real door before those parts are in place.

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!