11:07:49 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

L298N Motor Driver with Arduino: Control DC Motor Speed and Direction

Control a DC motor's speed and direction with an L298N driver and Arduino Uno. Pin table, 5V jumper rules, PWM code and troubleshooting.

On this page
L298N Motor Driver with Arduino: Control DC Motor Speed and Direction

An Arduino pin can only supply a few tens of milliamps, while even a small DC motor needs hundreds. A motor driver solves this: the Arduino sends small control signals and the driver switches the motor's own power. The L298N red board is the most common driver in student robot cars. In this tutorial you will connect one DC motor, make it spin forward and backward, change its speed with PWM, and learn the jumper rules that confuse almost every beginner.

Safety note

Use a battery pack of 6 V to 9 V for a small hobby motor. Do not touch the heatsink after running the driver for a while, because it gets hot. Never connect mains voltage to any part of this circuit.

What you need

Scroll sideways to see all columns.

Item Qty Notes
Arduino Uno (or clone) + USB cable 1
L298N motor driver module 1 Red board with a black heatsink
DC gear motor (the yellow TT type works) 1 Rated for 3 V to 6 V or 6 V to 9 V; check the motor label
Battery pack 1 4 x AA (about 6 V) or two 18650 cells (about 7.4 V) in a holder
Jumper wires 8
Digital multimeter 1

How the L298N works

The L298N contains two H-bridges. An H-bridge is a set of four switches that lets current flow through the motor in either direction. Each bridge controls one motor, so one module can drive two.

For motor A the control pins are:

  • IN1 and IN2: set the direction.
  • ENA (enable A): turns the bridge on. Feeding it a PWM signal controls the speed.

Scroll sideways to see all columns.

IN1 IN2 ENA Motor A
HIGH LOW HIGH / PWM Forward
LOW HIGH HIGH / PWM Backward
LOW LOW any Stops (coasts)
HIGH HIGH any Brakes
any any LOW Stops (coasts)

PWM (pulse-width modulation) switches the power on and off very fast. A higher duty cycle (analogWrite value up to 255) gives the motor more average power and more speed.

The power terminals and the 5V jumper

The module has three power terminals: 12V (motor supply input, labelled VS), GND and 5V. Follow these rules:

  • 5V-EN jumper on, motor supply within the onboard regulator’s usable range (often about 7–12 V): an onboard regulator makes 5 V for the chip's logic. The 5V terminal becomes an output. Do not connect an external 5 V source to it.
  • 5V-EN jumper removed: feed regulated 5 V into the 5V terminal. Use this setup for the 6 V battery example, where the onboard regulator may lack headroom.
  • The bridge drops voltage across its output transistors. Around 2 V is possible at moderate current, and the drop grows with current. Check the motor voltage under load.
  • Common ground: the battery negative, the L298N GND and the Arduino GND must be joined, otherwise the control signals have no reference.

Keep the ENA jumper (small pin cap near the ENA pins) removed when you want PWM speed control from the Arduino. With the cap on, ENA is held HIGH. Remove it before connecting the Arduino PWM output.

Connection table

Scroll sideways to see all columns.

L298N terminal Connects to
ENA (cap removed) Arduino D5 (PWM pin)
IN1 Arduino D7
IN2 Arduino D8
OUT1 and OUT2 The two motor wires
12V (VS) Battery pack +
GND Battery pack − and Arduino GND
5V Arduino 5V from USB, with the 5V-EN jumper removed

Power the Arduino from USB while testing.

Signal path in one line:

Arduino D5, D7, D8 → ENA, IN1, IN2 → H-bridge → OUT1, OUT2 → motor; battery → 12V terminal

Step-by-step build

  1. Remove the ENA and 5V-EN jumper caps. Connect Arduino 5V to the driver’s 5V terminal.
  2. Connect the motor to OUT1 and OUT2.
  3. Connect the battery pack + to 12V and − to GND. Link the L298N GND to an Arduino GND pin.
  4. Connect ENA to D5, IN1 to D7 and IN2 to D8.
  5. Upload the sketch, then connect the battery. The motor should speed up, slow down, then reverse.

The code

sketch-1.inoDownload code
// ElectroDai - L298N: DC motor speed and direction
// ENA -> D5 (PWM), IN1 -> D7, IN2 -> D8

const int ENA = 5;
const int IN1 = 7;
const int IN2 = 8;

void forward(int speed) {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, speed);     // 0 to 255
}

void backward(int speed) {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, speed);
}

void stopMotor() {
  analogWrite(ENA, 0);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
}

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

void loop() {
  Serial.println("Forward, speeding up");
  for (int s = 100; s <= 255; s += 5) {   // many small motors need ~100 to start
    forward(s);
    delay(40);
  }
  delay(1000);

  Serial.println("Stop");
  stopMotor();
  delay(1000);                // pause before reversing protects the driver and gears

  Serial.println("Backward, half speed");
  backward(128);
  delay(2000);

  stopMotor();
  delay(2000);
}

Testing with a multimeter

  • Battery voltage: measure battery + to − before connecting. A weak pack (below about 5 V for four AA cells) gives poor motor torque.
  • 12V terminal: should match the battery voltage.
  • 5V terminal: with the 5V-EN jumper off and the Arduino powered by USB, you should read about 5 V.
  • Motor voltage: meter across OUT1 and OUT2 while running at full speed. It will be about 2 V less than the battery voltage. At lower PWM values the reading falls, and the meter shows an average.
  • Control pins: IN1 to GND shows about 5 V when HIGH and 0 V when LOW. A PWM pin on ENA shows an average voltage proportional to the analogWrite value.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Motor does not move No common ground, logic supply or enable signal Join the grounds and check 5V and D5
Motor hums but will not turn PWM value too low Start from about 100 to 150
Always full speed ENA jumper cap on Remove the cap
Direction is backwards Motor wires swapped Swap OUT1 and OUT2, or swap the logic
Driver very hot Stalled motor or long full-power use Remove the load; use a heatsink and a lower voltage
Arduino resets when the motor starts Motor draws current from the USB Power the motor from the battery pack only
5V terminal smoking or very hot Supply above 12 V with the jumper on Remove the jumper and supply 5 V externally
Weak motor Voltage drop of about 2 V Use a slightly higher battery voltage, within the motor rating

FAQ

Can I connect two motors?

Yes. Use OUT3 and OUT4 with IN3, IN4 and ENB for the second motor. That is how a two-wheel robot car is driven.

Why does the motor get less voltage than my battery?

The L298N uses bipolar transistors that drop about 2 V. For better efficiency, modern MOSFET drivers (such as the TB6612FNG) waste less.

Can I power the Arduino from the same battery?

Yes, via the Vin pin or a USB power bank, but keep the grounds common. When testing, USB is simpler.

Which PWM pins can I use?

On an Uno: 3, 5, 6, 9, 10 and 11.

Next step: switch heavier loads in Arduino Relay Module Guide: Switch Loads Safely.

Related tutorials

See all Robotics →