Skip to content

Motor · ESP32 guide

Controlling L298N Dual H-Bridge Motor Driver Module with ESP32

Dual H-bridge motor driver — the standard for controlling two DC motors or one stepper.

Updated November 22, 2026 Arduino C++NoneAlso forArduino Uno

L298N Driver practical reference

The most widely used motor driver in the hobbyist world — a dual H-bridge board that can independently drive two brushed DC motors (or one bipolar stepper) with direction and PWM speed control, and includes an onboard 5V regulator for powering logic from the motor supply.

Key specifications

  • Driver IC: L298N dual H-bridge
  • Motor supply voltage: 5 V - 35V DC, fed to the board's 12V/VMS terminal
  • Logic voltage: 5 V — from the onboard regulator when the 5V-EN jumper is in place, or from an external 5V source if you've removed it (required if VMS > 12V, to avoid overheating the onboard regulator)
  • Max current: 2A per channel continuous, ~3A peak with a heatsink fitted
  • Channels: Drives 2 independent DC motors, or 1 bipolar stepper motor

Pinout and connection notes

Setting IN1/IN2 (or IN3/IN4) to the same logic level brakes that motor; opposite levels drive it forward or reverse. Remove the 5V-EN jumper and supply logic power separately whenever VMS exceeds 12V.

  • ENA: PWM speed control for motor A — jumper removed lets you drive this from a PWM pin; jumper in place runs motor A at full speed whenever enabled
  • IN1: Direction control, motor A
  • IN2: Direction control, motor A
  • IN3: Direction control, motor B
  • IN4: Direction control, motor B
  • ENB: PWM speed control for motor B
  • OUT1 / OUT2: Motor A output terminals
  • OUT3 / OUT4: Motor B output terminals
  • 12V / VMS: Motor supply voltage, 5V-35V DC
  • GND: Common ground — must be shared between the motor supply, the board, and the MCU
  • 5V: Regulated 5V output (only if the 5V-EN jumper is fitted and VMS ≤ 12V) — can power the MCU logic side

Integration checklist

  1. Confirm the exact part number and breakout-board revision.
  2. Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
  3. Check controller pin assignments and bus address or interface mode against the example code.
  4. Power up with external loads disconnected, then test signals before full integration.
  5. If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.

Troubleshooting and reproducibility

Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.

Realistic current, heat and motor-supply expectations

The L298N is an older bipolar-transistor bridge. Its voltage drop can be substantial, so the motor receives less voltage than the supply and the driver dissipates heat while carrying current. The headline current figures on generic module listings are not a promise of continuous operation at that current in every enclosure. Actual usable current depends on heatsinking, airflow, board copper, ambient temperature, motor duty cycle and the exact module.

Measure the motor-terminal voltage under load and check driver temperature during a controlled test. If the motor is weak or the heatsink becomes very hot, do not simply raise the supply voltage; check the driver's voltage drop, motor stall current, mechanical load and thermal conditions. A current-limited bench supply is useful for first tests. Add suitable suppression and wiring practices for inductive motor loads, and keep motor current paths away from sensitive analog wiring.

The onboard 5 V regulator and jumper arrangement varies between module designs. Verify the board schematic or markings before using its 5 V pin as an output. Do not power a microcontroller from that pin unless the regulator input range, thermal dissipation and available current have been confirmed. For new designs where efficiency and low voltage drop matter, compare a modern MOSFET-based driver with the L298N rather than assuming the older board is the best fit.

Wiring & code

Setting up L298N Driver with ESP32

Library

None

No library needed

The L298N board is a dual H-bridge: it lets a microcontroller pin decide the direction and speed of two DC motors, while the motor current comes from a separate supply. Each motor uses three pins: two direction inputs (IN1/IN2 for motor A, IN3/IN4 for motor B) and an enable pin (ENA, ENB) that takes PWM for speed. This guide wires two motors to an ESP32, runs them both ways, then adds soft starts and the difference between braking and coasting.

The ESP32's 3.3 V outputs drive the L298N's inputs directly: the chip treats anything above 2.3 V as HIGH.

Wiring

Component pinESP32 pinNote
ENAGPIO25Speed of motor A (PWM). Remove the ENA jumper first
IN1GPIO26Motor A direction
IN2GPIO27Motor A direction
ENBGPIO14Speed of motor B (PWM). Remove the ENB jumper
IN3GPIO32Motor B direction
IN4GPIO33Motor B direction
GNDGNDOne ground for the board, the L298N and the motor supply
+12V (VS)Motor supply +7–12 V for 6 V motors; not from the ESP32
OUT1 / OUT2Motor ASwap the two wires to reverse its direction
OUT3 / OUT4Motor B
L298N on an ESP32 in the SolderHub Simulator: ENA, IN1, IN2, ENB, IN3, IN4 to GPIO25, GPIO26, GPIO27, GPIO14, GPIO32, GPIO33; a 9 V supply on +12V and GND, with GND shared; motor A on OUT1/OUT2 and motor B on OUT3/OUT4

Warning

Three things that save boards. (1) Never power the motors from the ESP32's 5V or 3V3 pin: use a battery pack or adapter on +12V. (2) Join the grounds: without a shared GND the inputs float and the motors misbehave. (3) The 5V jumper powers the L298N's logic from its own regulator, which is fine up to 12 V on +12V; above that, remove the jumper and feed 5 V into the 5V terminal.

Why the motors run slower than expected

The L298 uses bipolar transistors, and they lose about 2 V (up to 3–4 V at high current) between the supply and the motor. A 6 V supply leaves roughly 4 V for the motors. Use 7–9 V for 6 V motors, or switch to a MOSFET driver such as the TB6612FNG, which loses only a few tenths of a volt and runs much cooler. The L298N is rated 2 A per channel, but above about 1 A the heatsink gets hot.

Test both motors

sketch.ino
/*
  L298N on an ESP32: drive two DC motors forward, backward and stop
  ENA -> GPIO25, IN1 -> GPIO26, IN2 -> GPIO27   (motor A on OUT1/OUT2)
  ENB -> GPIO14, IN3 -> GPIO32, IN4 -> GPIO33   (motor B on OUT3/OUT4)
  Remove both ENA/ENB jumpers. Motor supply to +12V and GND, GND shared with the ESP32.
  Arduino-ESP32 core 3.x LEDC: ledcAttach(pin, freq, bits), ledcWrite(pin, duty)
*/
const int ENA = 25, IN1 = 26, IN2 = 27;
const int ENB = 14, IN3 = 32, IN4 = 33;
const int PWM_FREQ = 1000;   // the L298's bipolar transistors switch slowly: stay around 1 kHz
const int PWM_BITS = 8;      // duty 0..255

// speed -255..255: sign sets direction, 0 brakes
void motor(int en, int inA, int inB, int speed) {
  digitalWrite(inA, speed > 0);
  digitalWrite(inB, speed < 0);
  ledcWrite(en, speed == 0 ? 255 : abs(speed));   // full enable with both inputs equal = brake
}

void setup() {
  Serial.begin(115200);
  for (int p : {IN1, IN2, IN3, IN4}) pinMode(p, OUTPUT);
  ledcAttach(ENA, PWM_FREQ, PWM_BITS);
  ledcAttach(ENB, PWM_FREQ, PWM_BITS);
}

void loop() {
  Serial.println("Forward");  motor(ENA, IN1, IN2, 200);  motor(ENB, IN3, IN4, 200);  delay(2000);
  Serial.println("Stop");     motor(ENA, IN1, IN2, 0);    motor(ENB, IN3, IN4, 0);    delay(1000);
  Serial.println("Backward"); motor(ENA, IN1, IN2, -200); motor(ENB, IN3, IN4, -200); delay(2000);
  Serial.println("Spin");     motor(ENA, IN1, IN2, 200);  motor(ENB, IN3, IN4, -200); delay(1500);
  motor(ENA, IN1, IN2, 0);    motor(ENB, IN3, IN4, 0);    delay(1500);
}

How the inputs work. IN1 HIGH and IN2 LOW drives one way, the reverse drives the other. With both inputs the same and the enable HIGH, the bridge shorts the motor's terminals together, which brakes it quickly. With the enable LOW the bridge lets go and the motor coasts to a stop. The PWM on the enable pin switches the motor on and off many times a second: a duty of 200 out of 255 gives about 78 % of the available voltage. On the ESP32 the sketch uses ledcAttach()/ledcWrite(), the Arduino-ESP32 3.x API; the old ledcSetup() and ledcAttachPin() no longer exist. Keep the frequency near 1 kHz: the L298 switches too slowly for 20 kHz.

Try it in the simulator

This is the two-motor circuit with the first sketch. Press Run and watch both motors turn forward, stop, reverse and then spin in opposite directions, the move a two-wheeled robot uses to turn on the spot.

Try this circuit in the SolderHub Simulator

Run this exact circuit in your browserReady-wired, code loaded

Confirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.

Soft start, brake and coast

Jumping straight to full speed pulls a large current spike and jerks a robot. Ramping the duty up over half a second is gentler on the gears and the supply. Most small motors don't turn at all below a certain duty and just hum, so the ramp starts at MIN_DUTY.

sketch.ino
/*
  Soft start and stop on motor A, then brake versus coast
  Same wiring as the first sketch.
*/
const int ENA = 25, IN1 = 26, IN2 = 27;
const int MIN_DUTY = 90;     // below this most small motors just hum: raise it if yours stalls

void setup() {
  Serial.begin(115200);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  ledcAttach(ENA, 1000, 8);
  digitalWrite(IN1, HIGH);   // forward
  digitalWrite(IN2, LOW);
}

void loop() {
  Serial.println("Ramp up");
  for (int d = MIN_DUTY; d <= 255; d += 5) { ledcWrite(ENA, d); delay(30); }
  delay(1000);
  Serial.println("Ramp down");
  for (int d = 255; d >= MIN_DUTY; d -= 5) { ledcWrite(ENA, d); delay(30); }

  Serial.println("Brake: both inputs equal, enable HIGH");
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); ledcWrite(ENA, 255);
  delay(1500);

  Serial.println("Coast: enable LOW, the motor spins down freely");
  digitalWrite(IN1, HIGH); ledcWrite(ENA, 255); delay(1000);
  ledcWrite(ENA, 0);
  delay(2000);
  digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
}

Troubleshooting

SymptomFix
Motors don't move, the L298N's LED is onThe ENA/ENB jumpers are still on with the enable wires connected, or the enables get no PWM. Remove the jumpers and check the pins
Motors hum but don't turnDuty too low or the supply too weak after the ~2 V drop: raise the duty or the supply voltage
ESP32 resets when the motors startMotor noise and supply dips: separate supplies, shared GND, and a 100 nF capacitor across each motor's terminals
One motor runs backwardsSwap its two wires on OUT1/OUT2 (or OUT3/OUT4), or swap IN1/IN2 in code
The board gets very hotOver about 1 A per channel. Use a larger heatsink, smaller motors, or a MOSFET driver
ledcAttach was not declaredUpdate the ESP32 boards package to 3.x, or use ledcSetup() and ledcAttachPin() on 2.x

Keep learning

Related guides & projects