L298N dual H-bridge: speed and direction for two motors
The red L298N module is the most common motor driver in robot kits. It has two H-bridges, so it can control the speed and the direction of two DC motors (or one bipolar stepper motor). Each motor uses three pins: EN sets the speed with PWM, and IN1/IN2 set the direction.
The board also has a 5 V regulator, flyback diodes and a large heatsink. The heatsink is necessary because the L298 chip uses old bipolar transistors. They lose about 2 V or more, and this voltage changes into heat. For small motors with a low voltage, a modern driver such as the TB6612FNG or DRV8833 (on the DC motors page) is more efficient.

Photo: Luiz Augusto Souza de Mesquita, CC0
Terminals, pins and jumpers
Specifications
| Item | Value |
|---|---|
| Motor supply (+12V terminal) | 5–35 V |
| Current per motor | 2 A (3 A peak) |
| Voltage loss | ~2 V at 1 A, more at higher currents |
| Logic inputs | HIGH > 2.3 V, so 3.3 V is OK |
| PWM frequency | 1–5 kHz is best (max ≈ 25 kHz) |
| On-board regulator | 78M05, 5 V |
What each connection does
| Label | Purpose |
|---|---|
| OUT1 / OUT2 | Motor A terminals |
| OUT3 / OUT4 | Motor B terminals |
| +12V | Motor supply + (5–35 V; the label is not the only voltage you can use) |
| GND | Motor supply − and ESP32 GND |
| +5V | Output of the regulator when the 5V jumper is installed. Input for logic power when the jumper is removed. |
| ENA / ENB | Speed (PWM) for motor A / B. Remove the jumper to use PWM. |
| IN1 / IN2 | Direction of motor A |
| IN3 / IN4 | Direction of motor B |
ENA and ENB jumpers
With a jumper installed, EN is connected to 5 V, so the motor always turns at full speed. You can control only the direction. Remove the jumpers and connect ENA and ENB to PWM pins to control the speed.
The 5V jumper
With the jumper installed, the on-board regulator makes 5 V from the motor supply. Use this only if the motor supply is 12 V or less. With more than 12 V, the regulator becomes too hot: remove the jumper and connect 5 V from a different source to the +5V terminal.
Can the +5V terminal supply the ESP32?
Yes, with the jumper installed and a motor supply of 7–12 V, you can connect +5V to the ESP32 5V/VIN pin (not 3V3). But the motors put noise on this rail. If the ESP32 resets when the motors start, supply it from USB or its own regulator. Refer to Power supplies.
Wiring two motors
Wiring table
| L298N | Connect to | Purpose |
|---|---|---|
| +12V | Motor battery + | Motor power |
| GND | Battery − and ESP32 GND | Common ground |
| ENA / ENB | GPIO 25 / GPIO 32 | Speed (PWM) |
| IN1 / IN2 | GPIO 26 / GPIO 27 | Direction of motor A |
| IN3 / IN4 | GPIO 33 / GPIO 13 | Direction of motor B |
| OUT1 / OUT2 | Motor A | Interchange them to reverse “forward” |
| OUT3 / OUT4 | Motor B | Interchange them to reverse “forward” |
Truth table (motor A; motor B is the same with IN3/IN4/ENB)
| ENA | IN1 | IN2 | Result |
|---|---|---|---|
| PWM | H | L | Forward at that speed |
| PWM | L | H | Reverse at that speed |
| H | same | same | Fast stop (brake): both motor terminals are connected together |
| L | any | any | Free-running stop (coast) |
The voltage drop and how to choose a supply
Add ~2–3 V to the motor voltage
Current goes through two transistors in the L298 (one high, one low). Together they lose about 2 V at 1 A, and more at higher currents. A 6 V motor on a 6 V supply gets only ~4 V and turns slowly. For a 6 V motor, use a 7.4–9 V supply (for example, a 2S LiPo or 6 AA cells).
The heat
Power lost in the chip ≈ voltage drop × current. At 1.5 A on each motor: 2.5 V × 1.5 A × 2 ≈ 7.5 W of heat. The heatsink becomes very hot. If the chip becomes too hot, it turns off for a short time. Then the motors stop and start again.
Stall current
A motor uses its stall current each time it starts. The L298N can supply ~3 A for a short time. Measure the coil resistance of your motor: stall current ≈ supply voltage ÷ resistance. The supply calculator on the DC motors page compares drivers for your motor.
Code
Two motors: drive, turn, brake, reverse
l298n_two_motors.ino
// Motor A: ENA = speed (PWM), IN1/IN2 = direction
const int ENA = 25, IN1 = 26, IN2 = 27;
// Motor B: ENB = speed (PWM), IN3/IN4 = direction
const int ENB = 32, IN3 = 33, IN4 = 13;
const int FREQ = 1000; // the L298N operates best at 1–5 kHz
const int BITS = 8; // speed 0..255
void setup() {
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
ledcAttach(ENA, FREQ, BITS);
ledcAttach(ENB, FREQ, BITS);
}
// speed: -255 (full reverse) .. 0 (coast) .. 255 (full forward)
void motor(int en, int inA, int inB, int speed) {
speed = constrain(speed, -255, 255);
digitalWrite(inA, speed > 0 ? HIGH : LOW);
digitalWrite(inB, speed < 0 ? HIGH : LOW);
ledcWrite(en, abs(speed)); // 0 = EN LOW = coast
}
// Fast stop: the two inputs are the same and EN is HIGH
void brake(int en, int inA, int inB) {
digitalWrite(inA, LOW);
digitalWrite(inB, LOW);
ledcWrite(en, 255);
}
void drive(int left, int right) {
motor(ENA, IN1, IN2, left);
motor(ENB, IN3, IN4, right);
}
void loop() {
drive(200, 200); delay(2000); // forward
drive(200, -200); delay(800); // turn on the spot
brake(ENA, IN1, IN2);
brake(ENB, IN3, IN4);
delay(300);
drive(-200, -200); delay(2000); // reverse
drive(0, 0); delay(1000); // coast
}
Control from the Serial Monitor
l298n_serial.ino
// Type two numbers in the Serial Monitor, for example: 150 -150
// left and right speed, -255..255. Type 0 0 to stop.
const int ENA = 25, IN1 = 26, IN2 = 27;
const int ENB = 32, IN3 = 33, IN4 = 13;
const int FREQ = 1000, BITS = 8;
int curL = 0, curR = 0; // speeds now
int tgtL = 0, tgtR = 0; // speeds requested
void motor(int en, int inA, int inB, int speed) {
digitalWrite(inA, speed > 0 ? HIGH : LOW);
digitalWrite(inB, speed < 0 ? HIGH : LOW);
ledcWrite(en, abs(speed));
}
// Move a value toward its target by at most 'step'
int approach(int now, int target, int step) {
if (now < target) return min(now + step, target);
if (now > target) return max(now - step, target);
return now;
}
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
ledcAttach(ENA, FREQ, BITS);
ledcAttach(ENB, FREQ, BITS);
Serial.println("Type: left right (each -255..255)");
}
void loop() {
if (Serial.available()) {
String line = Serial.readStringUntil('\n');
int l, r;
if (sscanf(line.c_str(), "%d %d", &l, &r) == 2) {
tgtL = constrain(l, -255, 255);
tgtR = constrain(r, -255, 255);
Serial.printf("left %d right %d\n", tgtL, tgtR);
}
}
// Ramp: a sudden change from forward to reverse causes a current spike
curL = approach(curL, tgtL, 5);
curR = approach(curR, tgtR, 5);
motor(ENA, IN1, IN2, curL);
motor(ENB, IN3, IN4, curR);
delay(10);
}
Set the Serial Monitor to 115200 baud and “Newline”. The speed ramp goes through 0 when the direction changes, so the motor slows down before it reverses.
Troubleshooting
The speed does not change
The ENA/ENB jumpers are still installed, so EN is always HIGH. Remove them and connect EN to the PWM pins.
The motors are slow or weak
The ~2 V loss in the L298 leaves too little voltage for the motor. Use a supply 2–3 V higher than the motor voltage, or use a MOSFET driver such as the TB6612FNG.
Nothing happens
Examine the grounds first: the ESP32 GND must connect to the L298N GND. Make sure the board LED is on. With the 5V jumper removed, the logic needs 5 V on the +5V terminal.
One motor turns the wrong way
Interchange the two wires of that motor at the OUT terminals, or change the sign of its speed in the code.
The motors stop and start again
The chip is too hot and turns off for a short time, or the battery voltage decreases under load and the ESP32 resets. Decrease the load, add a fan, or use a larger driver. Add a 470 µF capacitor at the +12V terminal.
Safety
The heatsink can become too hot to touch. Lift the wheels of a robot off the bench during tests. Install a fuse in projects that use a battery.
Resources
- ST L298 datasheetVoltage drop (saturation voltage), current limits and the internal H-bridges.
- Last Minute Engineers: L298N tutorialPinout, jumpers and the voltage drop, with pictures.
- DC motors · HW-517 MOSFET module · Stepper motors · Power suppliesRelated pages.