Mastering Motor Intelligence: ESP32 and L298N Motor Driver

The L298N Dual H-Bridge Motor Driver is one of the most popular modules for controlling DC motors with the ESP32. It allows precise control of speed and direction of one or two DC motors. This guide covers H-Bridge topology, PWM speed regulation, current handling, and building powerful robotic and automation projects.

How the H-Bridge Works

An H-Bridge is an electronic circuit that enables a voltage to be applied across a load (motor) in either direction. This allows the motor to spin clockwise or counterclockwise. The L298N contains two such bridges, supporting up to 2A per channel (peak) and operating voltage from 5V to 35V for the motor supply.

Key Features of L298N

  • Controls 2 DC motors independently
  • Direction and speed control via PWM
  • Built-in 5V regulator (can power ESP32)
  • Thermal shutdown and over-current protection
  • Logic voltage: 5V (compatible with ESP32 3.3V outputs)

Wiring the L298N Motor Driver to ESP32

The L298N requires a separate high-current power supply for the motors. Never power motors directly from the ESP32.

L298N PinFunctionESP32 GPIO Pin / Connection
+12VMotor Power Supply (5-35V)External Power Supply +
GNDCommon GroundGND (connect to ESP32 GND)
5VLogic Power / Regulator OutputESP32 5V (optional)
ENAEnable/Speed Motor A (PWM)GPIO 27
IN1Motor A DirectionGPIO 26
IN2Motor A DirectionGPIO 25
ENBEnable/Speed Motor B (PWM)GPIO 14 (optional)
IN3Motor B DirectionGPIO 12 (optional)
IN4Motor B DirectionGPIO 13 (optional)

Important Notes

  • Remove the 5V jumper if motor voltage > 12V to avoid damaging the regulator.
  • Always connect grounds together.
  • Use external power supply rated for your motor current requirements.

Programming: DC Motor Speed and Direction Control

Using PWM on the Enable pins allows smooth speed control from 0 to 100%.

#define ENA 27
#define IN1 26
#define IN2 25

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

void motorControl(int speed, int direction) {
  analogWrite(ENA, speed);  // 0-255
  if (direction == 1) {     // Forward
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
  } else if (direction == -1) { // Backward
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
  } else {                  // Stop
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
}

void loop() {
  Serial.println("Motor Forward");
  motorControl(200, 1);
  delay(2000);

  Serial.println("Motor Stop");
  motorControl(0, 0);
  delay(1000);

  Serial.println("Motor Backward");
  motorControl(150, -1);
  delay(2000);

  Serial.println("Motor Stop");
  motorControl(0, 0);
  delay(1000);
}

Advanced IoT Applications

Combine motor control with WiFi, MQTT, or sensors to build smart automated systems.

Real-World Use Cases

  • Robotics: Wheeled robots, robotic arms, and line-follower bots.
  • Smart Home Automation: Motorized curtains, garage doors, or window blinds.
  • Remote-Controlled Vehicles: WiFi/Bluetooth RC cars or boats.
  • Industrial Automation: Conveyor belts, valve control, or positioning systems.
  • Camera Pan-Tilt: Precise movement using two motors.

Common Pitfalls & Troubleshooting

  • Motor Not Moving: Check motor power supply, ensure grounds are connected, and verify PWM on ENA/ENB.
  • Motor Runs Only One Direction: Swap IN1/IN2 wiring or check logic signals.
  • Overheating: Add heat sink to L298N. It is not efficient for high-power motors (consider DRV8871 or TB6612 for better efficiency).
  • Noise/Interference: Add 0.1uF capacitors across motor terminals and a flyback diode if needed.
  • ESP32 PWM Frequency: Default PWM is fine, but you can change frequency for smoother motor operation using ledcSetup().
  • Current Limit: L298N drops ~2V. For high-current motors (>1A continuous), use modern drivers like MX1508 or DRV8833.

Final Summary

The L298N Motor Driver paired with ESP32 opens the door to powerful motion control in robotics and automation projects. By mastering H-Bridge operation, PWM speed control, and proper power management, you can build responsive, WiFi-enabled motorized IoT systems with ease.