← All projects
Automation Intermediate

A DC Motor on an L298N Driver: Speed, Direction and the Module's Traps

The L298N H-bridge turns a motor both ways, and PWM on ENA sets the speed. We cover the ENA and 5V-EN jumpers, why the L298N loses 2 V, how to reverse safely, and when a TB6612FNG is the better choice.

Difficulty
Intermediate
Time
45 min
Category
Automation
Updated

About the project

With a single MOSFET a motor turns one way only. To change direction you need an H-bridge: four switches that feed the motor in one polarity or the other. The red L298N module has two such bridges (two motors, up to 2 A each), a 5 V regulator and protection diodes. We set the speed with a potentiometer and the direction with a button.

What you need

  • ESP32 DevKit
  • L298N module
  • 6–12 V DC motor
  • 12 V power supply (or a 7–12 V battery pack)
  • 10 kΩ potentiometer and a button

Wiring

L298N Connection
+12V PSU "+"
GND PSU "−" and the ESP32 GND
OUT1 / OUT2 the motor
ENA GPIO4 (PWM, speed)
IN1 / IN2 GPIO16 / GPIO17 (direction)
  • Potentiometer: outer pins to 3V3 and GND, wiper to GPIO34.
  • Button: between GPIO32 and GND.

Two jumpers people forget

  1. The ENA jumper (fitted at the factory) keeps motor A always on at full speed. To control the speed you must remove it. Otherwise the GPIO output is shorted to 5 V, and the simulator shows an error.
  2. The 5V-EN jumper enables the on-board regulator that powers the L298N logic from +12V. Above 12 V, remove it and feed 5 V into the +5V pin separately, or the regulator overheats.

How to drive it

IN1 IN2 Motor
HIGH LOW forward
LOW HIGH reverse
equal equal brake (winding shorted)
— ENA = LOW coast

Speed is the PWM duty on ENA: analogWrite(ENA, 0…255).

Code

// DC motor on an L298N: potentiometer = speed (PWM on ENA), button = direction.
// Remove the ENA jumper on the board, otherwise the GPIO is shorted to 5 V.
const int ENA = 4, IN1 = 16, IN2 = 17;
const int POT = 34, BTN = 32;

bool forward = true;
int lastBtn = HIGH;

void setup() {
  Serial.begin(115200);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
}

void loop() {
  int b = digitalRead(BTN);
  if (b == LOW && lastBtn == HIGH) {
    analogWrite(ENA, 0);   // stop before reversing: saves the gearbox and the driver
    delay(300);
    forward = !forward;
    Serial.println(forward ? "Forward" : "Reverse");
  }
  lastBtn = b;

  digitalWrite(IN1, forward ? HIGH : LOW);
  digitalWrite(IN2, forward ? LOW : HIGH);
  int speed = map(analogRead(POT), 0, 4095, 0, 255);
  analogWrite(ENA, speed);
  delay(20);
}

The honest truth about the L298N

  • A ~2 V drop. The L298 is built from bipolar transistors, and each switch loses about 1 V. From 12 V the motor gets ~10 V; from 6 V, only ~4 V. For low-voltage motors that is a disaster.
  • Heat. At 1 A the chip dissipates ~2 W, which is why it carries such a big heatsink.
  • The modern replacement is the TB6612FNG, built on MOSFETs: a 0.1–0.3 V drop, up to 1.2 A per channel (3.2 A peak), 3.3 V logic and a small board. For battery-powered robots it is the obvious choice.

Good to know

  • Reversing on the fly causes a current surge twice the start-up current. A 200–300 ms pause with ENA off saves both the driver and the gears.
  • The motor supply ground and the ESP32 ground must be connected, or the IN/ENA signals float.
  • PWM on ENA runs at ~1 kHz by default, and the motor whines quietly. analogWriteFrequency(ENA, 20000) gets rid of the whine.