0
0
AutocadHow-ToBeginner · 4 min read

How to Use L298N Motor Driver with Arduino: Simple Guide

To use the L298N motor driver with an Arduino, connect the motor driver inputs to Arduino digital pins and power the driver properly. Then, control motor direction and speed by setting the input pins HIGH or LOW and using PWM signals on the enable pins.
📐

Syntax

The L298N motor driver uses input pins to control motor direction and enable pins to control speed via PWM signals.

  • IN1, IN2: Control motor 1 direction.
  • IN3, IN4: Control motor 2 direction.
  • ENA, ENB: Enable pins for motors 1 and 2, used for speed control with PWM.
  • +12V, GND: Power supply for motors.
  • 5V, GND: Logic power supply from Arduino.

Set IN1 and IN2 HIGH/LOW to choose motor rotation direction. Use analogWrite(ENA, speed) to control speed.

arduino
const int IN1 = 8;
const int IN2 = 9;
const int ENA = 10; // PWM pin

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

void loop() {
  // Rotate motor forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 200); // Speed from 0 to 255
  delay(2000);

  // Rotate motor backward
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 200);
  delay(2000);

  // Stop motor
  analogWrite(ENA, 0);
  delay(2000);
}
💻

Example

This example shows how to connect one DC motor to the L298N and control its direction and speed using Arduino.

Connect:

  • IN1 to Arduino pin 8
  • IN2 to Arduino pin 9
  • ENA to Arduino pin 10 (PWM)
  • Motor terminals to L298N motor output
  • Power motor supply to +12V and GND on L298N
arduino
const int IN1 = 8;
const int IN2 = 9;
const int ENA = 10; // PWM pin

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

void loop() {
  // Motor forward at half speed
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 128); // 50% speed
  delay(3000);

  // Motor backward at full speed
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 255); // 100% speed
  delay(3000);

  // Stop motor
  analogWrite(ENA, 0);
  delay(2000);
}
Output
Motor runs forward at half speed for 3 seconds, then backward at full speed for 3 seconds, then stops for 2 seconds, repeating.
⚠️

Common Pitfalls

  • Incorrect wiring: Mixing motor power supply and logic power can damage components. Always separate motor power (+12V) and Arduino 5V logic.
  • Not enabling EN pins: Forgetting to set ENA/ENB pins HIGH or use PWM will prevent motor movement.
  • Wrong pin modes: Not setting Arduino pins connected to IN1, IN2 as OUTPUT causes no control.
  • Power supply issues: Using insufficient power for motors causes weak or no movement.
arduino
/* Wrong way: Not setting pinMode and ENA LOW */
const int IN1 = 8;
const int IN2 = 9;
const int ENA = 10;

void setup() {
  // Missing pinMode setup
}

void loop() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 0); // Motor won't run
  delay(2000);
}

/* Right way: */

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

void loop() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 200); // Motor runs
  delay(2000);
}
📊

Quick Reference

PinPurposeDescription
IN1InputControls motor 1 direction (HIGH/LOW)
IN2InputControls motor 1 direction (LOW/HIGH)
ENAEnableMotor 1 speed control via PWM
IN3InputControls motor 2 direction
IN4InputControls motor 2 direction
ENBEnableMotor 2 speed control via PWM
+12VPowerMotor power supply (up to 12V)
5VPowerLogic power supply from Arduino
GNDGroundCommon ground for power and logic

Key Takeaways

Connect L298N input pins to Arduino digital pins and set them as OUTPUT.
Use PWM on enable pins (ENA/ENB) to control motor speed.
Provide separate power supply for motors and logic to avoid damage.
Set motor direction by setting IN1/IN2 pins HIGH or LOW appropriately.
Always check wiring and pin modes to prevent common mistakes.