13.18 Driving a DC motor with the BTS7960
The car is driven by one brushed DC motor of size 775, switched by an IBT-2 module built around two Infineon BTS7960 chips.
The IBT-2 does the job of the discrete bridge in the H-bridge chapter with four transistors that carry tens of amperes, and adds the protection that a student-built bridge lacks.
A 775 motor on a 3S battery draws tens of amperes when it starts or is held, and a car on the table drives off it. Make every first run with the wheels off the ground or with the motor alone clamped to the bench. Keep fingers, hair and cables away from gears and wheels, and keep the main switch within reach.
The Guide
Parts
| Part | Use |
|---|---|
| ESP32 DevKit | the controller |
| IBT-2 module | the H-bridge |
| 775 motor | the drive motor |
| 10 kΩ potentiometer | the bench throttle |
| 10 kΩ resistor | pull-down on EN |
| AWG 16 wire (1.3 mm²) | motor and supply |
| AWG 22 wire (0.33 mm²) | logic |
| lab power supply | the first run |
| 3S battery, main switch, 20 A fuse | the car rail |
| multimeter | checks |
Our motor is marked RS775-5331 and DC12V (Figure 13.18.1). The marking gives the frame size and the rated voltage, and nothing about current or speed, so The brushed DC motor works with typical values and Exercise 1 measures our own.
The screw terminal of the IBT-2 carries the motor power and its 8-pin header the logic (Figure 13.18.18.18.2):



| Header pin | Name | Function |
|---|---|---|
| 1 | RPWM | input of the right half-bridge: high = its high side on, low = its low side on |
| 2 | LPWM | input of the left half-bridge, same function |
| 3 | R_EN | enable of the right half-bridge, low = both its switches off |
| 4 | L_EN | enable of the left half-bridge |
| 5 | R_IS | current sense and fault output of the right BTS7960; not connected |
| 6 | L_IS | current sense and fault output of the left BTS7960; not connected |
| 7 | VCC | supply of the logic buffer on the module |
| 8 | GND | logic ground, connected to B\(-\) on the module |
| Terminal | Function |
|---|---|
| B+ | motor supply, 6 to 27 V, from the car rail |
| B\(-\) | motor supply ground |
| M+, M\(-\) | the two motor wires |
Step 1: Wire the logic side
We solder the thin AWG 22 wires to the header pins on the underside of the module (Figure 13.18.18.18.2, middle), where the names are printed beside the pads; a soldered joint does not shake loose on the car the way a jumper on the header does. Wire the module to the ESP32 as in Figure 13.18.4: RPWM to GPIO 16, LPWM to GPIO 17, and R_EN and L_EN joined at the module to GPIO 19, with a 10 kΩ resistor from GPIO 19 to ground. VCC goes to the ESP32 3V3 pin, never to 5 V (Logic levels), and GND to ESP32 GND. R_IS and L_IS stay unconnected (Current sense outputs).
The potentiometer gets its ends on 3V3 and GND and its wiper on GPIO 35.
Every wire from the ESP32 to the module carries 3.3 V logic. The potentiometer is fed from 3V3, never from 5 V, VIN or the rail. GPIO 35 tolerates at most about 3.6 V, and 12 V on it destroys the ESP32.
Step 2: Wire the power side
Connect the motor to M+ and M\(-\). For the first run, connect B+ and B\(-\) to the lab power supply set to 12 V with its current limit at 3 A; the motor then starts slowly against the limit, and a wiring mistake does no damage. Use the thick AWG 16 wire (1.3 mm²) for B+, B\(-\), M+ and M\(-\), and keep these wires short. The terminals are named only on the underside, in the order B\(-\), B+, M+, M\(-\) from the capacitor end (Figure 13.18.18.18.2, bottom); the top side gives no hint, so check the order there before tightening the screws. The thin AWG 22 wire (0.33 mm²) is meant for milliamperes and overheats at these currents.
Once both sides are wired, flash the motor program from Chrome or Edge, without PlatformIO, and follow Step 4 with it. The program drives as soon as it starts, so turn the pot to centre and lift the wheels off the ground before flashing. If the motor follows the pot, the wiring is right, and any later fault in our own build lies in the code.
Step 3: Create the project
Create a PlatformIO project named motor as in the PlatformIO chapter and give it the plain platformio.ini of that chapter:
[env:esp32dev]
platform = espressif32@8.0.0
board = esp32dev
framework = arduino
upload_speed = 921600
monitor_speed = 115200Create src/motor.h and src/main.cpp from The code, build, and upload with the motor supply switched off.
Step 4: First run, wheels off the ground
Turn the potentiometer to its middle position, open the serial monitor and switch the motor supply on. The monitor prints five lines per second:
mode=pot throttle=1 % COAST duty=0
Turn the potentiometer slowly clockwise. Past the deadband the state changes to DRIVE, the duty rises from zero, and the motor starts, usually once the duty passes 25 to 50 of 255. A fast turn still gives a smooth start, since the duty rises by at most 10 every 20 ms (Soft start). If the wheel turns backwards for the car, swap M+ and M\(-\).
Turn back through the middle into the other half: the state becomes BRAKE and the motor stops hard. Return to the middle, wait half a second, and turn into that half again: the state becomes DRIVE with a negative duty and the motor runs backwards, the “double tap” of a hobby speed controller.
Type coast and press Enter, then turn the motor shaft by hand: it turns freely. Type brake and turn it again: it resists, the harder the faster we turn it. Type pot to return control to the potentiometer.
Step 5: Run from the battery
Switch the motor supply off, move B+ to the car rail after the 20 A fuse and the main switch and B\(-\) to the common ground, and repeat Step 4, still with the wheels off the ground. The motor now starts faster, since only the soft start limits the current.
Troubleshooting
The motor does not turn, the state changes correctly
Check that EN reaches the module: GPIO 19 must read 3.3 V on the multimeter in DRIVE. Check VCC on the module header (3.3 V) and the supply on B+. If the motor hums but does not start, the power supply is in current limit; raise the limit or use the battery.
The motor runs at full speed as soon as the supply is switched on
VCC of the module is on 5 V, or EN has no pull-down and floats high while the ESP32 starts. See Logic levels.
The ESP32 restarts when the motor starts
The motor current pulls the ESP32 supply down. On the bench, the ESP32 must not share the motor’s lab supply. On the car, it must be fed from the buck converter, and the ground wires of the module and the ESP32 must meet at the battery ground, not through a thin jumper.
How It Works
The brushed DC motor
In a brushed DC motor the current \(I\) flows through rotor windings that lie in the field of permanent magnets, and the force on the windings gives a torque proportional to the current, \[ T = k I, \] where \(k\) is the motor constant. Turning windings in a magnetic field also induce a voltage, the back-EMF, proportional to the speed \(\omega\), and with the same constant \(k\) in SI units. The terminal voltage \(V\) therefore drives the current through the winding resistance \(R\) against the back-EMF, \[ V = R I + k \omega . \tag{13.18.1}\]
Two cases of 13.18.1 bound what the motor does. At standstill the back-EMF is zero and only the winding resistance limits the current, giving the stall current \(I_s = V/R\) and the largest torque \(k I_s\). Without load the motor speeds up until the back-EMF almost equals \(V\) and only the small no-load current that covers friction flows. Between these ends the speed falls linearly with the load torque (Figure 13.18.5).
The number 775 names a frame size, and 775 motors sold for 12 V differ widely. A typical listing gives 7000 rpm and 0.8 A without load and a stall current of 36 A at 12 V; others give stall currents from 25 A to well over 100 A, so we measure our own (Exercise 1). With the listed values, \[ R = \frac{12}{36}~\Omega = 0.33~\Omega, \qquad \omega_0 = 7000 \cdot \frac{2\pi}{60}~\text{rad/s} = 733~\text{rad/s}, \] \[ k = \frac{V - R I_0}{\omega_0} = \frac{12 - 0.33 \cdot 0.8}{733}~\text{V s/rad} = 0.016~\text{V s/rad}, \] and the stall torque is \(k I_s = 0.016 \cdot 36~\text{N m} = 0.58~\text{N m}\), close to the 0.54 N m the same listing states.
At the full 12.6 V of the battery the stall current is \(12.6/0.33~\text{A} = 38~\text{A}\), twice the rating of the fuse, so the motor must never be held stalled.
The IBT-2 module
One BTS7960 is a half-bridge: a high-side switch from the supply to the output and a low-side switch from the output to ground, with a driver chip that turns one of them on at a time. The datasheet (Infineon, Rev. 1.1, 2004) gives a path resistance of 16 mΩ for the two switches together, a current limitation at typically 43 A, a supply range of 5.5 to 27.5 V, and PWM operation up to 25 kHz.
The IBT-2 is two half-bridges with the motor between their outputs, one full H-bridge: the right BTS7960 is driven by RPWM and R_EN, the left by LPWM and L_EN. The chip inputs IN (RPWM or LPWM) and INH (R_EN or L_EN) act as Table 13.18.2 lists, and INH low also puts the chip to sleep. The chip inserts a dead time between turning one switch off and the other on, so the two switches of a half-bridge never conduct together, and the shoot-through that a hand-built bridge must avoid in software cannot be commanded from the header.
A 74HC244 buffer, powered from the VCC pin, lies between the header and the chips.
Logic levels
The BTS7960 inputs alone would accept the ESP32: the datasheet guarantees that IN is read as high above 2.0 V and INH above 2.15 V, and as low below 1.1 V, and allows up to 5.3 V on both. The 74HC244 in front of them is stricter, since a 74HC chip reads a high only above about 70 % of its own supply:
| Input | Read as high above | ESP32 high of 3.3 V |
|---|---|---|
| BTS7960 IN, INH | 2.0 V, 2.15 V | read as high |
| 74HC244 with VCC at 5 V | about 3.5 V | read at random |
| 74HC244 with VCC at 3.3 V | about 2.3 V | read as high, with margin |
The buffer’s 3.3 V outputs are in turn well above the 2.15 V the BTS7960 needs, so the module VCC comes from the ESP32 3V3 pin.
During reset and start-up the ESP32 pins are inputs and drive nothing. The buffer can read a floating EN line as high, and with RPWM or LPWM also floating the motor could jerk. The 10 kΩ pull-down on GPIO 19 holds EN low, and the bridge in coast, until the program drives the pin.
The four bridge states
With EN shared, two PWM inputs and one enable line select the four states that four gate signals set in the H-bridge, where forward, reverse, coast and brake are explained:
| EN | RPWM | LPWM | Right half | Left half | Motor |
|---|---|---|---|---|---|
| high | high | low | high side on | low side on | forward, full voltage |
| high | low | high | low side on | high side on | reverse, full voltage |
| high | low | low | low side on | low side on | brake, terminals shorted |
| high | high | high | high side on | high side on | brake through the high sides |
| low | any | any | both off | both off | coast, terminals open |
In forward the current flows from B+ through the right high side, the motor and the left low side to ground; reverse uses the other diagonal. A full brake from full speed draws almost the stall current, \(k\omega/R = 11.7/0.33~\text{A} = 35~\text{A}\) with the 775 above, where the low side of the BTS7960 starts limiting its current (33 to 64 A, typically 43 A at 25 °C). That current circulates in the motor and the two low sides and does not pass the battery or the fuse.
Reversing a spinning motor is harder on the bridge: the supply and the back-EMF add, and at full speed the current would be \((12.6 + 11.7)/0.33~\text{A} = 74~\text{A}\), above the current limit of the chip. The throttle logic therefore always brakes before it reverses.
PWM speed control at 20 kHz
Frequency, duty cycle and the LEDC hardware that makes the signal are those of the servo (Frequency, duty cycle and resolution, Generating the pulse with LEDC). In forward, RPWM switches at a fixed frequency \(f\) and is high for the fraction \(D\) of each period. While RPWM is high the motor sees the supply voltage \(V_B\); while it is low, the right low side is on and both motor terminals are at ground, the brake state. The winding inductance \(L\) keeps the motor current from following these fast steps, so the motor responds to the average voltage \[ \bar V = D V_B . \] At 11.1 V and \(D = 0.5\), the motor runs as on a 5.6 V supply. The off-time grounds the motor instead of leaving it open, so the current keeps flowing in both halves of the period: through the left low side all the time, and through the right high or low side in turn. The speed then follows the duty cycle almost linearly; the datasheet calls this active freewheeling. The discrete bridge of the H-bridge switches its low side instead, because its high side turns off too slowly.
The inductance smooths the current only partly. It rises during the on-time and falls during the off-time, with a peak-to-peak ripple of about \[ \Delta I = \frac{V_B D (1 - D)}{L f}. \] No datasheet gives the inductance of a 775; motors of this size have some tens to a few hundred microhenry. With an assumed \(L = 100~\mu\text{H}\), \(V_B = 12~\text{V}\) and \(D = 0.5\), the ripple is \(12 \cdot 0.25/(10^{-4} \cdot 1000)~\text{A} = 30~\text{A}\) at 1 kHz and \(1.5~\text{A}\) at 20 kHz. The formula assumes a period much shorter than the time constant \(L/R = 0.3\) ms. At 1 kHz it is not, and the exact solution of \(L\,\mathrm{d}I/\mathrm{d}t = V - RI - k\omega\) in Figure 13.18.6 gives 25 A in place of 30 A, still five times the mean current.
The large ripple at low frequency heats the motor and the switches without adding torque, and the windings vibrate and sing at the PWM frequency. At 20 kHz the ripple is small, the tone lies at the upper end of human hearing, and the frequency stays below the 25 kHz the BTS7960 allows.
LEDC counts an 80 MHz clock, so one period of \(2^b\) counts at frequency \(f\) needs \(f \, 2^b \le 80~\text{MHz}\). At 20 kHz and 8 bit, \(20\,000 \cdot 256 = 5.1~\text{MHz}\), well inside the limit; 11 bit (\(41~\text{MHz}\)) would still fit. Eight bits give 256 duty steps, finer than we can feel on a throttle, and the largest duty, 255, switches the output fully on.
Current sense outputs
Each BTS7960 has an IS pin, brought out as R_IS and L_IS. In normal operation it sources about 1/8500 of the current through the high-side switch. When the chip detects overcurrent, overtemperature or overvoltage, it switches to a fixed fault current of 4 to 7 mA instead.
A resistor on the module from each IS pin to ground turns this current into a voltage. With both pins unconnected, as here, the current flows only into those resistors and cannot harm the ESP32. On an ADC pin, IS could report the battery current and faults. On many modules, though, the resistor is large enough to put far more than 3.3 V on the pin in a fault, so a sense input needs a resistor of its own and a careful check first.
The throttle logic
A throttle is one number from \(-100\) to \(100\) percent, so a negative value can mean brake or reverse. Hobby speed controllers decide with the double tap, and throttle() follows the same rules:
throttle().
| Throttle | Previous situation | Result |
|---|---|---|
| inside the deadband | any | coast (EN low) |
| positive | stopped at power-on, or last driven forward | forward |
| negative | stopped at power-on, or last driven in reverse | reverse |
| negative | last driven forward | brake |
| negative | braked, then neutral for at least 300 ms, then negative | reverse |
| positive | last driven in reverse | brake, then forward after neutral as above |
Driving against the last direction, either way, always passes a brake, a release to neutral of at least 300 ms, and a second push. A spinning motor is therefore never reversed under power, which protects the bridge (The four bridge states), and the stick motion that would reverse the car brakes it first. Neutral coasts, so the car rolls freely when the throttle is released.
Outside the deadband of \(d = 8\) percent, map() rescales the throttle \(t\) to a duty that starts at zero at the edge of the deadband, \[
\text{duty} = 255\,\frac{|t| - d}{100 - d},
\] so the motor does not jump to a duty of 8 % when the throttle leaves neutral. On the bench, map(analogRead(potPin), 0, 4095, -100, 100) turns the potentiometer reading into \(t\).
Soft start
A motor at standstill has no back-EMF, so full throttle from rest draws the stall current at once: \(9.6/0.33~\text{A} = 29~\text{A}\) from a nearly empty battery with the motor of The brushed DC motor. The internal resistance of the battery and the wires turns that current into a drop of the rail voltage, and the rail can fall below the 7.3 V or so that the XL4015 buck converter needs to hold its 7.0 V output. That output then sags, and the ESP32 and the servo reset or twitch (Buck converters).
motorRamp() therefore lets the duty grow by at most rampStep = 10 per 20 ms control step, so full throttle, 255, comes after 26 steps or about 0.5 s, while the back-EMF grows with the speed. At standstill and duty \(D\) the motor current is \(D V_B/R\), and the battery delivers it only for the fraction \(D\) of each period, so the battery current is \(D^2 V_B/R\): at \(D = 0.2\) only 4 % of the stall current. Lowering the duty, braking and coasting reduce the current and take effect at once. A change of direction starts the ramp from zero. When the throttle opens on a car that is already rolling, the first small duty cycles act as a mild brake until the duty catches up with the speed.
The code
motor.h holds the pins, the settings and every function that drives the motor:
// motor.h: the drive motor through the BTS7960 (IBT-2). The car program includes this
// file unchanged.
#pragma once
#include <Arduino.h>
const int rpwmPin = 16; // PWM for forward
const int lpwmPin = 17; // PWM for reverse
const int enPin = 19; // R_EN and L_EN together: low lets the motor spin freely
const int motorFreqHz = 20000; // above what we can hear
const int motorBits = 8; // duty from 0 to 255
const int rampStep = 10; // soft start: duty rises by at most 10 per 20 ms step,
// 0 to full in about half a second
const int deadband = 8; // throttle percent around zero that counts as neutral
int currentDuty = 0; // the duty sent last, -255 (full reverse) to 255 (full forward)
const char* motorMode = "COAST"; // COAST, DRIVE or BRAKE
void motorBegin() {
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW); // coast while we set up
ledcAttach(rpwmPin, motorFreqHz, motorBits);
ledcAttach(lpwmPin, motorFreqHz, motorBits);
}
// Both half-bridges off: the motor spins freely.
void motorCoast() {
digitalWrite(enPin, LOW);
ledcWrite(rpwmPin, 0);
ledcWrite(lpwmPin, 0);
currentDuty = 0;
motorMode = "COAST";
}
// Drive with duty -255..255 at once. Duty 0 holds both motor terminals at ground,
// which brakes.
void motorDrive(int duty) {
duty = constrain(duty, -255, 255);
if (duty >= 0) { ledcWrite(lpwmPin, 0); ledcWrite(rpwmPin, duty); } // the idle side first
else { ledcWrite(rpwmPin, 0); ledcWrite(lpwmPin, -duty); }
digitalWrite(enPin, HIGH);
currentDuty = duty;
motorMode = (duty == 0) ? "BRAKE" : "DRIVE";
}
void motorBrake() { motorDrive(0); }
// Soft start: speeding up goes at most rampStep per call, slowing down is immediate.
// A change of direction starts from zero.
void motorRamp(int target) {
int dir = (target >= 0) ? 1 : -1;
int speed = abs(target);
int lastSpeed = (currentDuty * dir > 0) ? abs(currentDuty) : 0; // 0 after a change of direction
if (speed > lastSpeed + rampStep) speed = lastSpeed + rampStep; // speeding up: one step per call
motorDrive(dir * speed);
}
int lastDir = 0; // the direction driven last: 1 forward, -1 reverse
bool braked = false; // braked since then, the first tap of the double tap
unsigned long activeMs = 0; // the last time the throttle was out of neutral
// Throttle from -100 to 100 percent, like the speed controller of a hobby car.
// Call every 20 ms. Neutral coasts. Pushing the other way first brakes; it drives
// that way only after a brake and 300 ms in neutral (the "double tap").
void throttle(int percent) {
percent = constrain(percent, -100, 100);
if (abs(percent) < deadband) { motorCoast(); return; }
int dir = (percent > 0) ? 1 : -1;
bool rested = millis() - activeMs >= 300; // neutral for 300 ms before this call
activeMs = millis();
if (dir == lastDir || lastDir == 0 || (braked && rested)) {
int duty = map(abs(percent), deadband, 100, 0, 255); // 0 at the edge of the deadband
motorRamp(dir * duty);
lastDir = dir;
braked = false;
} else {
motorBrake();
braked = true;
}
}motorDrive() zeroes the idle side before it raises the active side, so a change of direction never passes through a state with both high sides on. A duty of zero holds both motor terminals at ground, so motorBrake() is motorDrive(0). motorRamp() works with the speed, the size of the duty, and the direction separately. lastSpeed is the speed driven last in the same direction, or 0 after a change of direction, and the new speed may exceed it by at most rampStep. A smaller speed goes through at once.
throttle() follows Table 13.18.5. Since activeMs is the last time the throttle was out of neutral, millis() - activeMs at the first call after a pause in neutral is the length of that pause, the same comparison of millis() values as in Waiting without delay(). Figure 13.18.7 shows a throttle sequence run through throttle(): the ramp, the brake when the throttle reverses, and reverse only after the brake and half a second in neutral.
throttle() every 20 ms on the sequence 60 % for 2 s, neutral for 0.1 s, \(-50\) % for 1 s, neutral for 0.5 s, \(-50\) % for 1 s, then 50 %.
The bench program main.cpp reads the potentiometer every 20 ms and passes the throttle to throttle(). It prints the state five times per second and takes the commands pot, coast and brake as the servo bench does:
// main.cpp: bench test of motor.h. The pot on GPIO 35 is the throttle, with neutral in
// the middle. Commands in the Serial Monitor (end with Enter): pot, coast, brake.
#include <Arduino.h>
#include "motor.h"
const int potPin = 35;
enum Mode { POT, COAST, BRAKE };
Mode mode = POT;
const char* modeName[] = {"pot", "coast", "brake"};
unsigned long lastStepMs = 0;
unsigned long lastPrintMs = 0;
String line = ""; // the characters received since the last Enter
void handleLine(String s) {
s.trim();
if (s.length() == 0) return;
if (s == "pot") mode = POT;
else if (s == "coast") mode = COAST;
else if (s == "brake") mode = BRAKE;
else Serial.println("Commands: pot | coast | brake");
Serial.printf("mode=%s\n", modeName[mode]);
}
void setup() {
Serial.begin(115200);
motorBegin();
Serial.println("Motor bench. Commands: pot | coast | brake");
}
void loop() {
// Collect characters until Enter, then handle the line
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') { handleLine(line); line = ""; }
else if (line.length() < 64) { line += c; }
}
// One control step every 20 ms
if (millis() - lastStepMs < 20) return;
lastStepMs = millis();
int percent = map(analogRead(potPin), 0, 4095, -100, 100); // middle of the pot = 0
if (mode == POT) throttle(percent);
else if (mode == COAST) motorCoast();
else motorBrake();
if (millis() - lastPrintMs >= 200) {
lastPrintMs = millis();
Serial.printf("mode=%s throttle=%d %% %s duty=%d\n",
modeName[mode], percent, motorMode, currentDuty);
}
}Exercises
Exercise 1: The motor constants. Lock the motor shaft in a vice (never by hand) and apply 1.0 V from the lab power supply with the current limit at 5 A. Read the current and compute the winding resistance \(R\). Then run the motor without load from 12 V, measure the no-load current with the power supply and the speed with a phone tachometer app or the AS5600 sensor, and compute \(k\) from 13.18.1. What stall current and stall torque do your values predict at 12.6 V, and what current does a full brake from full speed draw?
Exercise 2: Ripple in a simulation. In Falstad, build the motor as a 0.33 Ω resistor in series with a 100 µH inductor, connected between the output of a square-wave source (0 to 12 V, duty cycle 0.5) and ground. The source at 0 V plays the low-side switch during the off-time. Plot the inductor current at 1 kHz and at 20 kHz and compare the peak-to-peak ripple with the formula in PWM speed control and with Figure 13.18.6. The simulation has no back-EMF: how would a back-EMF source in series change the mean current and the ripple?
Exercise 3: Trace the throttle logic. In Figure 13.18.7 the first neutral lasts 0.1 s and the second 0.5 s. Sketch the state and duty when the second neutral lasts only 0.2 s, and when the first lasts 0.5 s. Use Table 13.18.5, then confirm both with the potentiometer and the serial monitor. Why does the duty at 3.6 s start from zero and not from 144?