Arduino Nano ESP32 - BTS7960 Motor Driver

In this guide, we will learn how to control a large 12V DC motor from the Arduino Nano ESP32 with a BTS7960 motor driver module. Here is a project to keep in mind: the roof vent of a greenhouse. On a hot afternoon, a gear motor pushes the window open to let the heat out. In the evening, it pulls the window shut again. A motor that lifts a window can need more current than a small driver can give, so the BTS7960 is a good match. And since the Nano ESP32 has WiFi built in, the same vent could later be opened and closed from a web page. In detail, we will learn:

Arduino Nano ESP32 driving a 12V DC motor through the BTS7960 43A 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×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 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 BTS7960 Motor Driver

The Nano ESP32 can decide when a motor runs, but its pins cannot power the motor. Each pin gives 3.3V and only a small current. The BTS7960 module takes over that heavy part. On the upper face of the PCB you find two Infineon BTS7960B power chips. A large heat sink, bolted underneath, keeps them cool. Each chip looks after one of the two motor wires, and it switches that wire between the + and the - of the supply. Together, the two half-bridges make one full H-bridge. So you can run one brushed DC motor both ways, and the duty cycle of a PWM signal decides the speed (bigger duty cycle, faster motor). Compared with an L298N or a DRV8833, this driver plays in a heavier class. It is built for loads like wiper motors, winches, scooter or wheelchair motors, large gear motors and heavy robots. In online shops, you may find it listed as "IBT-2" or "HW-039".

BTS7960 Module
Motor supply (B+ and B-) 5.5V to 27V DC (6V, 12V and 24V motors)
Motor current up to 43A (the current limit of the chip)
Motors one brushed DC motor, both directions
PWM frequency on RPWM and LPWM up to 25 kHz
Logic chip 74HC14 buffer, powered by the VCC pin
Protection current limiting, over-temperature
Extra outputs R_IS and L_IS (current sense / fault signal)

Read the 43A as a ceiling, not as a working value. Once the current gets near that value, the chip begins to hold it back. A vent motor may run for many seconds at a time, so pick a motor that draws far less. For a long run, the real limit is how much heat the heat sink can carry away.

BTS7960 motor driver module pinout - 8-pin control header, B+ and B- power terminals, M+ and M- motor terminals

Eight male pins stand in two rows on the control header. Pins 1, 3, 5 and 7 (RPWM, R_EN, R_IS, VCC) form one row. Pins 2, 4, 6 and 8 (LPWM, L_EN, L_IS, GND) form the other, so RPWM faces LPWM, R_EN faces L_EN, and so on. Two labels on the board look odd: the silkscreen says R_S where it means R_IS, and L_S where it means L_IS. The signals do not go straight to the power chips. They first pass a 74HC14 buffer chip, which the product page calls the isolation chip. The pins one by one, and where each goes in this guide:

  • RPWM pin: speed signal for the forward direction. It drives the right half-bridge. Connect to D9 of the Nano ESP32.
  • LPWM pin: speed signal for the reverse direction. It drives the left half-bridge. Connect to D10.
  • R_EN pin: the enable of the right half-bridge. At HIGH that half works, at LOW it rests. Connect to D12.
  • L_EN pin: works like R_EN, but for the left half-bridge. Connect to D11.
  • R_IS / L_IS pins: give out a current-sense or error signal. Not needed here, so leave them free.
  • VCC pin: the supply of the logic side only (the 74HC14 chip plus the power LED). No motor current comes from it. Connect to VBUS (5V).
  • GND pin: connect to GND. Inside the module, it is tied to B-.
  • B+ / B- screw terminals: where the 12V for the motor comes in, + on B+ and - on B-.
  • M+ / M- screw terminals: hold the motor's two wires.

How the four control pins move the motor

First, both enable pins are HIGH. Then RPWM and LPWM choose the motion:

RPWM LPWM What the motor does
PWM (0 to 255) 0 turns forward, the duty cycle sets the speed
0 PWM (0 to 255) turns in reverse, the duty cycle sets the speed
0 0 brakes - the driver joins the two motor wires, so it stops fast

There is a fourth state. Pull both enable pins LOW, and the two half-bridges switch off. Now no power flows to the motor. The shaft coasts: it keeps turning and slows down on its own. When you write your own code later, remember this: either RPWM or LPWM has to be 0 at every moment. Giving PWM to both together is not allowed.

Power and ground

  • VCC goes to VBUS, which carries 5V while the Nano ESP32 runs on USB. A single buffer chip is all that VCC powers. The module maker gives 5V as its supply. Its inputs accept 3.3V levels, so the Nano ESP32 pins can drive them as they are.
  • Nothing is plugged into R_IS and L_IS, and the four inputs have pull-down resistors. So the module sends nothing back to the Nano ESP32, and 5V can never reach one of its pins.
  • On the module, the GND pin is already connected to B-. Because of that, one GND-to-GND wire between the two boards also shares the ground with the 12V side. You do not run a wire to B-.
  • B+ and B- bring the 12V from the adapter to the motor. Choose an adapter rated for more current than your motor draws.

※ NOTE THAT:

B+ carries 12V: keep it away from every pin of the Nano ESP32. Also find out which adapter wire is + and which is - before the power goes on. If they are swapped, the module can break.

Wiring Diagram

The module has two sides. From the control header, six jumper wires run to the Nano ESP32: four signals, plus VBUS and GND. On the screw terminal side, the DC power jack passes the 12V from the adapter to B+ and B-, while M+ and M- take the motor wires. The Nano ESP32 itself runs from the USB cable and never touches the 12V. Any digital pin of the Nano ESP32 can make PWM, so we use four pins that sit side by side on one header: D9, D10, D11 and D12.

The wiring diagram between Arduino Nano ESP32 and BTS7960 motor driver

This image is created using Fritzing. Click to enlarge image

BTS7960 Pin Arduino Nano ESP32 Pin / Other
RPWM D9
LPWM D10
R_EN D12
L_EN D11
VCC VBUS (labelled 5V on the screw terminal board)
GND GND
R_IS, L_IS leave unconnected
B+ + of the 12V adapter (through the DC power jack)
B- - of the 12V adapter (through the DC power jack)
M+, M- the two wires of the 12V DC motor

Pushing jumper wires into the headers is quick, but the wires can slip out. Simply plug the Arduino Nano ESP32 into a Screw Terminal Expansion Board for Arduino Nano. The diagram below shows the same wiring with the screw terminal board.

The wiring diagram between Arduino Nano ESP32 and BTS7960 motor driver Screw Terminal

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Keep the 12V adapter unplugged while you wire. Plug it in last, after the Nano ESP32 is wired and the code is uploaded. The enable wires are easy to mix up: R_EN belongs on D12, L_EN on D11.

How To Program For BTS7960 Motor Driver

Nothing needs to be installed. The sketch uses only analogWrite() for the two PWM pins and digitalWrite() for the two enable pins.

  • Name the four control pins:
#define PIN_RPWM D9 #define PIN_LPWM D10 #define PIN_R_EN D12 #define PIN_L_EN D11
  • Inside setup(), every control pin becomes an OUTPUT. Write 0 to RPWM and LPWM before anything else, then set both enables HIGH to wake up the driver:
pinMode(PIN_RPWM, OUTPUT); pinMode(PIN_LPWM, OUTPUT); pinMode(PIN_R_EN, OUTPUT); pinMode(PIN_L_EN, OUTPUT); analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); digitalWrite(PIN_R_EN, HIGH); digitalWrite(PIN_L_EN, HIGH);
  • Forward, with speed as a value between 0 and 255:
analogWrite(PIN_LPWM, 0); analogWrite(PIN_RPWM, speed);
  • Turn in reverse. Just swap the roles of the two PWM pins:
analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, speed);
  • Brake. Write 0 to RPWM and to LPWM, and keep the enables HIGH:
analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0);
  • Coast. Both enable pins go LOW, so the half-bridges switch off:
digitalWrite(PIN_R_EN, LOW); digitalWrite(PIN_L_EN, LOW);

Why does the brake use analogWrite(pin, 0) and not digitalWrite(pin, LOW)? On the Nano ESP32, a pin that has run analogWrite() stays in PWM mode. So the sketch keeps each pin on one function only: PWM for RPWM and LPWM, plain HIGH and LOW for R_EN and L_EN.

Arduino Nano ESP32 Code - BTS7960 Motor Driver

The full sketch wraps the steps above in five small helpers: driverOn(), motorForward(), motorReverse(), motorBrake() and motorCoast(). One round takes about 12 seconds: speed up forward, hold full speed, slow down, brake, run backward at half speed, and coast. Then the sketch turns the driver on again and starts over. For every step, a line shows up in the Serial Monitor. The lines borrow the greenhouse picture: forward is "Open vent", and reverse is "Close vent".

/* * 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-bts7960-motor-driver */ // One 12V DC motor on a BTS7960 (IBT-2) H-bridge module. // RPWM and LPWM set the speed with analogWrite() (0 to 255). // R_EN and L_EN turn the two half-bridges on (HIGH) or off (LOW). // No library is needed. #define PIN_RPWM D9 // Arduino Nano ESP32 pin D9 -> RPWM of the BTS7960 (forward PWM) #define PIN_LPWM D10 // Arduino Nano ESP32 pin D10 -> LPWM of the BTS7960 (reverse PWM) #define PIN_R_EN D12 // Arduino Nano ESP32 pin D12 -> R_EN of the BTS7960 (right half-bridge on/off) #define PIN_L_EN D11 // Arduino Nano ESP32 pin D11 -> L_EN of the BTS7960 (left half-bridge on/off) #define HALF_SPEED 128 // duty cycle of about 50% void setup() { Serial.begin(9600); pinMode(PIN_RPWM, OUTPUT); pinMode(PIN_LPWM, OUTPUT); pinMode(PIN_R_EN, OUTPUT); pinMode(PIN_L_EN, OUTPUT); // no PWM on either side before the driver wakes up analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); driverOn(); Serial.println("Vent motor ready: BTS7960 enabled (R_EN D12, L_EN D11 HIGH)"); } void loop() { // 1) Forward: raise the speed step by step Serial.println("Open vent | forward, RPWM rising 0..255"); for (int speed = 0; speed <= 255; speed += 3) { motorForward(speed); delay(30); } // 2) Forward: stay at full speed Serial.println("Open vent | forward, RPWM 255 for 2 s"); delay(2000); // 3) Forward: lower the speed step by step Serial.println("Open vent | forward, RPWM falling 255..0"); for (int speed = 255; speed >= 0; speed -= 3) { motorForward(speed); delay(30); } // 4) Brake: both PWM inputs at 0 while the driver is on Serial.println("Stop | brake, RPWM 0 + LPWM 0 for 1 s"); motorBrake(); delay(1000); // 5) Reverse at about half speed Serial.println("Close vent | reverse, LPWM 128 (half) for 2 s"); motorReverse(HALF_SPEED); delay(2000); // 6) Coast: driver off, the motor rolls to a stop by itself Serial.println("Idle | coast, both EN pins LOW, 2 s"); motorCoast(); delay(2000); driverOn(); // turn the driver on again before the next forward run } // Both half-bridges on. Needed for forward, reverse and brake. void driverOn() { digitalWrite(PIN_R_EN, HIGH); digitalWrite(PIN_L_EN, HIGH); } // Forward: PWM on RPWM, LPWM stays at 0. speed = 0..255 void motorForward(int speed) { analogWrite(PIN_LPWM, 0); // the other side goes to 0 first, never both at once analogWrite(PIN_RPWM, speed); } // Reverse: PWM on LPWM, RPWM stays at 0. speed = 0..255 void motorReverse(int speed) { analogWrite(PIN_RPWM, 0); // the other side goes to 0 first, never both at once analogWrite(PIN_LPWM, speed); } // Brake: both PWM inputs at 0, enables stay HIGH. // The driver joins the two motor wires, so the motor stops fast. void motorBrake() { analogWrite(PIN_RPWM, 0); analogWrite(PIN_LPWM, 0); } // Coast: both PWM inputs at 0 and both half-bridges off. // The motor is free and 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

Follow these instructions step by step:

  • New to the Arduino Nano ESP32? Get the Arduino IDE ready first with how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Wire everything as in the table above. Leave the 12V adapter unplugged for now.
  • Connect the Nano ESP32 to your PC with the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Nano ESP32 board (e.g., Arduino Nano ESP32) and COM port.
  • Paste the code above into a new sketch, then click the Upload button. You do not need to install any library.
  • When the upload has finished, plug the 12V adapter into the DC power jack.
  • Open the Serial Monitor at 9600 baud. Then watch the motor and compare it with each new line.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Vent motor ready: BTS7960 enabled (R_EN D12, L_EN D11 HIGH) Open vent | forward, RPWM rising 0..255 Open vent | forward, RPWM 255 for 2 s Open vent | forward, RPWM falling 255..0 Stop | brake, RPWM 0 + LPWM 0 for 1 s Close vent | reverse, LPWM 128 (half) for 2 s Idle | coast, both EN pins LOW, 2 s Open vent | forward, RPWM rising 0..255 Open vent | forward, RPWM 255 for 2 s
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Pay attention to the two stops. At the "Stop" line, the shaft stops almost at once, because the driver joins the motor wires. At the "Idle" line, the driver is off, so the shaft keeps turning for a moment and slows down by itself. For a greenhouse vent, the brake is the better choice, because the window stops close to the spot where you cut the power. If the "ready" line is missing, the Serial Monitor simply opened a little late. The other lines keep coming.

※ NOTE THAT:

  • Does the motor turn the wrong way during the "Open vent" lines? Unplug the 12V adapter and swap the M+ and M- wires.
  • Do the lines appear, but the motor does not move? Check the 12V adapter, the GND wire between the two boards, and that R_EN is on D12 and L_EN on D11.
  • A hum or a whine at low speed is normal. analogWrite() on the Nano ESP32 makes a PWM of about 1 kHz, and your ears can hear that.

Is the BTS7960 too big for your motor? For small motors, see Arduino Nano ESP32 - DRV8833 DC Motor Driver or Arduino Nano ESP32 - DC Motor, which uses the DRV8833 too. The DRV8833 handles two motors, about 1.5A for each, up to 10.8V. The BTS7960 has room for just one motor, yet it takes far more current and up to 27V.

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!