L298N Motor Driver with Arduino: DC Motor Control Guide

This post contains affiliate links, marked with *. As an Amazon Associate I earn from qualifying purchases, at no extra cost to you. It helps keep the tutorials free.

The video loads from YouTube (Google), which may set cookies. Privacy policy

Quick summary

How to use the L298N dual H-bridge motor driver with Arduino. Covers the pinout, wiring for one or two DC motors, direction and speed control with PWM, and power supply setup. Includes two sketches (Arduino programs), one for two motors and one for a single motor. I tested the setup on an Arduino Uno with two DC motors.

DC motors need more current than an Arduino pin can supply, so a motor driver sits between the two. The L298N dual H-bridge module is one of the most common drivers for this job. New to Arduino? Start with What is Arduino and Arduino programming basics.

What Is the L298N Motor Driver?

The L298N is a motor driver chip (IC, integrated circuit) that contains two H-bridges, one per motor (the next section explains how an H-bridge works). The module used here mounts the chip on a small board with screw terminals, a heat sink and a 5 V voltage regulator (a part that turns the motor supply into a steady 5 V). Screw terminals are wire clamps tightened with a screw; the heat sink is a metal block that carries heat away from the chip. With two independent channels, the module sets the direction and speed of two DC motors at the same time from an Arduino or any other microcontroller (a small computer on a single chip). It accepts motor supply voltages from 5 V to 35 V and delivers up to 2 A of continuous current per channel, enough for most small and medium hobby motors.

The module is cheap and widely available. In the video for this post, I wired it to an Arduino Uno and two DC motors and then ran the same test with the Arduino Motor Shield and the L293D Motor Shield.

How an H-bridge Works

An H-bridge is a circuit of four switches arranged in an H shape around a motor. Closing different pairs of switches sends current through the motor in either direction, which decides whether it spins forward or in reverse.

Forward Direction

With S1 and S4 closed and S2 and S3 open, current flows from the supply through the motor in one direction, and the motor spins forward.

H-bridge circuit diagram, forward direction: switches S1 and S4 closed, current flows through the DC motor from left to right.
Forward direction: S1 and S4 closed, current flows left to right through the motor.

Reverse Direction

With S2 and S3 closed and S1 and S4 open, current flows the other way and the motor reverses.

H-bridge circuit diagram, reverse direction: switches S2 and S3 closed, current flows through the DC motor from right to left.
Reverse direction: S2 and S3 closed, current flows right to left through the motor.

Speed Control with PWM

Pulse width modulation (PWM) controls the motor speed. Instead of supplying a constant voltage, the Arduino rapidly switches the enable pin on and off. The ratio of on-time to off-time, the duty cycle, sets the average voltage the motor receives. A 50 percent duty cycle delivers roughly half the supply voltage, so the motor spins slower. A 100 percent duty cycle delivers the full voltage for maximum speed. On the L298N module, the PWM signal goes to the enable pins ENA and ENB.

PWM signal diagram showing square waves with different duty cycles: a higher duty cycle means a higher average voltage.
PWM duty cycle sets the average voltage: a higher duty cycle means faster rotation.

L298N Pinout Explained

L298N module pinout: motor outputs OUT1 to OUT4, power terminals +12 V, GND and +5 V, control pins ENA, IN1 to IN4 and ENB.

Motor output terminals (OUT1, OUT2, OUT3, OUT4): These screw terminals connect to the DC motors. OUT1 and OUT2 drive Motor A, OUT3 and OUT4 drive Motor B.

Power terminals (+12 V, GND, +5 V): The terminal marked +12 V is the motor supply input. The label is only a name: it accepts 5 V to 35 V. GND is the common ground shared by the motor supply, the L298N control circuit and the Arduino. The +5 V terminal works in two ways. With the 5 V regulator jumper installed (a jumper cap is a small plastic plug that connects two pins), it outputs 5 V. With the 5 V regulator jumper removed (required above 12 V), it needs 5 V logic power from outside, for example from the Arduino 5 V pin. Logic power is the 5 V that runs the L298N’s control circuit.

Enable and Input Pins

Enable pins (ENA, ENB): These pins switch each motor channel on and set its speed. ENA and ENB each have their own jumper cap, the ENA jumper and the ENB jumper, which connects the enable pin to the 5 V pin next to it. With the ENA or ENB jumper installed, that motor runs at full speed whenever its direction pins select forward or reverse. If you remove that jumper and connect the enable pin to an Arduino PWM pin instead, you get variable speed control from 0 to 255 through analogWrite(). Despite its name, analogWrite() does not output an analog voltage. It outputs the PWM signal described above, with a value from 0 (always off) to 255 (always on).

Input pins (IN1, IN2, IN3, IN4): These four digital inputs set the direction. IN1 and IN2 belong to Motor A, IN3 and IN4 to Motor B. The table lists every combination for Motor A; Motor B works the same way with IN3, IN4 and ENB.

IN1IN2ENAMotor A
HIGHLOWHIGH or PWMSpins forward
LOWHIGHHIGH or PWMSpins in reverse
LOWLOWHIGH or PWMBrakes (fast stop)
HIGHHIGHHIGH or PWMBrakes (fast stop)
anyanyLOW (analogWrite 0)Coasts to a stop

Brake: the driver connects both motor wires to the same supply rail (both to the positive supply or both to ground), so the motor stops quickly. Coast: the driver switches its outputs off, and the motor spins down on its own.

Components Needed (BOM)

ComponentQuantityNotes
Arduino Uno (or compatible board)1Any Arduino with PWM-capable digital pins works
L298N dual H-bridge motor driver module1The red board with screw terminals
DC motor (3 V to 12 V hobby motor)1 or 2TT gear motors (small plastic gear motors for robot cars) work well
Power supply for the motors1Battery pack or lab power supply (a mains-powered unit with adjustable output voltage). Choose about 2 V above the motor’s rated voltage, because the L298N drops 1.4 V to 2 V. For 6 V TT motors: a 7.4 V 2S LiPo (a two-cell lithium-polymer battery)
Male-to-female jumper wires (a pin on one end, a socket on the other)about 10Connect the L298N pins to the Arduino headers (the socket rows along the board edges)
Breadboard (optional)1A board with spring contacts for solderless wiring; keeps the connections tidy
Robot chassis and wheels (optional)1 setFor a robot car build

L298N Wiring with Arduino

The table shows the wiring between the L298N module and the Arduino Uno for two DC motors with independent speed and direction.

L298N PinFunctionArduino PinNotes
ENAMotor A speed (PWM)D10 (PWM)Remove the ENA jumper for PWM speed control
IN1Motor A direction input 1D9HIGH or LOW sets the direction
IN2Motor A direction input 2D8HIGH or LOW sets the direction
IN3Motor B direction input 1D7HIGH or LOW sets the direction
IN4Motor B direction input 2D6HIGH or LOW sets the direction
ENBMotor B speed (PWM)D5 (PWM)Remove the ENB jumper for PWM speed control
+12 V (VCC)Motor supply positivenoneConnect to the positive terminal of the motor battery or supply
GNDCommon groundGNDCritical: Connect the L298N GND to both the motor supply negative and the Arduino GND
+5 V5 V regulator outputnoneOutputs 5 V with the 5 V regulator jumper installed; see Power Supply and Safety below
Wiring diagram: L298N motor driver connected to an Arduino Uno, two DC motors and a 9 V supply on a DC barrel jack adapter; the Arduino runs on USB.
Wiring diagram: L298N connected to an Arduino Uno with two DC motors and an external power supply.

Power Supply and Safety

Most L298N problems come from the power setup.

Power the motors from their own supply. DC motors draw far more current than the Arduino 5 V pin can safely provide. Powered from the Arduino, they cause voltage drops, erratic behavior and possibly permanent damage to the board. Connect a separate battery pack or power adapter to the L298N +12 V terminal.

Connect a common ground. The Arduino GND, the L298N GND and the negative terminal of the motor supply must be connected together. Without a common ground, the L298N cannot read the Arduino’s logic signals (the HIGH and LOW levels on the control pins) correctly, and the motors behave unpredictably or do not move at all.

Voltage Regulator, Drop, and Noise

The onboard 5 V regulator. With the 5 V regulator jumper installed and 7 V to 12 V at the +12 V terminal, the +5 V terminal outputs a regulated 5 V. This terminal can power the Arduino: connect it to the Arduino 5 V pin, not VIN (the pin that feeds the Arduino’s own voltage regulator). Above 12 V, remove the 5 V regulator jumper to protect the regulator and feed 5 V logic power into the +5 V terminal from outside. For first tests, power the Arduino over USB and the motors from a separate supply. Powering the Arduino from the L298N +5 V terminal also works, but motor noise then reaches the Arduino.

Voltage drop. The four switches in each L298N H-bridge are bipolar junction transistors (BJTs). Each conducting transistor drops some voltage, so about 1.4 V to 2 V of the supply is lost as heat in the driver. With a 6 V supply, the motors receive about 4 V to 4.6 V. Pick a supply about 2 V above the motor’s rated voltage. For TT gear motors (usually rated 3 V to 6 V), a 7.4 V 2S LiPo works well: after the L298N’s voltage drop, about 5.4 V to 6 V reach the motors. A 6 V pack of four AA batteries also runs the motors, but below about 7 V the onboard regulator cannot deliver a stable 5 V. With such a pack, remove the 5 V regulator jumper and connect the Arduino 5 V pin to the L298N +5 V terminal.

Motor noise. DC motors generate electrical noise (short voltage spikes), especially when they start or reverse. Keep the power connections tight. Solder a 100 nF ceramic capacitor between the two terminals of each motor. A 100 µF to 470 µF electrolytic capacitor across the motor supply terminals reduces the voltage dip when a motor starts.

Arduino Code: Controlling Two DC Motors

Upload the sketch below with the Arduino IDE (the program you write and upload sketches with). It drives both motors forward at a PWM value of 128, stops, reverses and stops again.

Full Sketch: Two-Motor Control

/**
 * L298N Motor Driver - Two DC Motor Control
 * Author: Omar Draidrya
 * Website: omartronics.com
 *
 * Controls forward, stop, and reverse for two DC motors
 * using the L298N dual H-bridge with PWM speed control.
 */

#define IN1 9    // Motor A direction pin 1
#define IN2 8    // Motor A direction pin 2
#define IN3 7    // Motor B direction pin 1
#define IN4 6    // Motor B direction pin 2
#define ENA 10   // Motor A speed control (PWM)
#define ENB 5    // Motor B speed control (PWM)

void setup() {
  // Set all motor control pins as outputs
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  // Start with motors stopped
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void loop() {
  // --- Move both motors FORWARD at half speed ---
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 128);   // Motor A at ~50% speed
  analogWrite(ENB, 128);   // Motor B at ~50% speed
  delay(2000);

  // --- STOP both motors ---
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(2000);

  // --- Move both motors BACKWARD at half speed ---
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, 128);
  analogWrite(ENB, 128);
  delay(2000);

  // --- STOP both motors ---
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  delay(1000);
}

Code Walkthrough

Pin definitions (lines 10 to 15): Six pin names are defined with #define. Before compiling (translating the code into instructions for the board), the Arduino IDE replaces each name with its number, so IN1 in the code means pin 9. IN1 and IN2 set the direction of Motor A, IN3 and IN4 the direction of Motor B. ENA and ENB set the speed of each motor through PWM. I chose pins 10 and 5 for ENA and ENB because they support PWM on the Arduino Uno.

setup(): The code configures all six pins as outputs with pinMode() and sets the four direction pins LOW. Setting the direction pins LOW right away makes sure both motors start stopped.

Forward: IN1 HIGH and IN2 LOW drives Motor A forward, IN3 HIGH and IN4 LOW drives Motor B forward. analogWrite(ENA, 128) sends a PWM signal with a duty cycle of about 50 percent (128 out of 255), so the motor receives about half the supply voltage on average and runs slower. The code then waits 2 seconds.

Stop: The code sets all four direction pins LOW. The enable pins keep their PWM signal. With both inputs LOW and the enable pin active, the driver connects both motor wires to ground, so the motor brakes. To let the motor coast instead, write analogWrite(ENA, 0).

Reverse: IN1 LOW and IN2 HIGH reverses Motor A, IN3 LOW and IN4 HIGH reverses Motor B. The PWM value stays at 128.

Single Motor Example

For one motor, use Motor A only. Wiring: IN1 to pin 9, IN2 to pin 8, ENA (ENA jumper removed) to pin 10. Leave IN3, IN4 and ENB unconnected. The sketch below drives the motor forward at a 75 percent duty cycle (191), stops, reverses at 50 percent (128), and stops again.

/**
 * L298N - Single DC Motor Control
 * Author: Omar Draidrya
 * Website: omartronics.com
 *
 * Drives one DC motor forward, stops, reverses, and stops.
 * Uses Motor A channel only (IN1, IN2, ENA).
 */

#define IN1  9   // Motor A direction pin 1
#define IN2  8   // Motor A direction pin 2
#define ENA  10  // Motor A speed (PWM)

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

void loop() {
  // Forward at 75 % speed
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 191);
  delay(2000);

  // Stop
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  delay(1000);

  // Reverse at 50 % speed
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 128);
  delay(2000);

  // Stop
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  delay(1000);
}

Test and Tune Your Setup

Start with one motor. Disconnect Motor B and test Motor A first; wiring and code problems are easier to spot with one motor. Once Motor A works, connect Motor B.

Verify the direction. Upload the code and check whether the motor spins forward during the forward section. If it spins the wrong way, swap the two motor wires in the screw terminal, or swap the IN1 and IN2 pin numbers in the code.

Start with low PWM values. Try a value like 80 or 100 before analogWrite(ENA, 255). A slow motor is safer to test and easier to observe. Below about 50 to 70, many motors only hum or stall (stand still although powered), because the average voltage is too low to overcome friction. When I tested at 64, the right-hand motor struggled to turn against friction, so I raised the value to 128.

Common Problems and Troubleshooting

SymptomLikely causeFix
Motor does not spinNo motor supply; ENA or ENB with neither its jumper nor a PWM wire; PWM value too low; both direction pins at the same levelCheck that the supply is connected to +12 V and GND and switched on. Check that the ENA and ENB jumpers are installed, or that ENA and ENB are wired to PWM pins (without either, the channel may stay off). Raise the analogWrite value (try 200 or 255). Set IN1 and IN2 to different levels
Motor hums but does not spinPWM value too low, or the motor is mechanically jammedRaise the PWM value or clear the jam
Spins in one direction onlyOne direction pin is not connected, or the pin number in the code is wrongTest each direction pin on its own with digitalWrite() HIGH and LOW and watch the motor
Motor is weak or slowSupply voltage too low. The L298N drops about 1.4 V to 2 V: a 5 V supply leaves the motor 3 V to 3.6 V, a 6 V supply 4 V to 4.6 VUse a supply about 2 V above the motor’s rated voltage; check the PWM value
Arduino resets when a motor startsThe motor’s inrush current (a short current spike at start) pulls the supply voltage down, and motor noise adds voltage spikesPower the Arduino over USB or its own supply, share only GND, and add the two capacitors from the Motor noise paragraph
Erratic or no motor responseGND not sharedConnect Arduino GND, L298N GND and the motor supply negative
No speed control, motor fully on or fully offENA or ENB on a pin without PWMOn the Arduino Uno, only pins 3, 5, 6, 9, 10 and 11 support analogWrite(); look for the ~ symbol printed next to the pin number
Works intermittentlyLoose screw terminals, often from motor vibrationTighten all screw terminals; strip the wire ends and insert them firmly
Driver gets hotThe L298N turns its voltage drop into heat: at 1 A and a 2 V drop, each channel produces about 2 W, so the heat sink gets hot well below the 2 A ratingIf it is too hot to touch, reduce the load, lower the PWM value, or switch to a high-current driver module such as the BTS7960

L298N vs Other Arduino Motor Driver Options

Two common alternatives to the L298N module are the Arduino Motor Shield and the L293D Motor Shield. A shield is a board that plugs onto the Arduino headers.

Arduino Motor Shield

Arduino Motor Shield wiring diagram: the shield on an Arduino Uno connected to two DC motors.
Arduino Motor Shield: plugs directly onto the Arduino, so the wiring is simpler.
Arduino Motor Shield Rev3 top view with screw terminals for motor A, motor B and Vin, the L298P driver chip and the header pins for the Uno.
Arduino Motor Shield Rev3: screw terminals for motors A and B and the Vin motor power input on the left, L298P driver chip in the middle.

The official Arduino Motor Shield Rev3 plugs onto the Uno headers, so no jumper wires run between driver and Arduino. It drives two DC motors (or one stepper motor, a motor that turns in fixed steps) at up to 2 A per channel. Direction is set with the pins DIR A and DIR B, speed with PWM A and PWM B. It fixes which Arduino pins you use and usually costs more than a standalone L298N module. The shield’s L298P chip is the same driver as the L298N in a different chip housing. The obstacle-avoiding robot car on this site uses this shield.

L293D Motor Shield

L293D Motor Shield wiring diagram: the shield on an Arduino connected to two DC motors and a 9 V supply.
L293D Motor Shield: drives up to four DC motors and is programmed with the Adafruit Motor Shield library.

The L293D Motor Shield (often sold as a compatible copy of the Adafruit Motor Shield V1) uses the L293D chip and drives up to four DC motors or two stepper motors. Each channel delivers only 0.6 A continuous (1.2 A peak), less than the L298N. You program it with the Adafruit Motor Shield library (AFMotor.h), a ready-made code collection with functions such as motor.run(FORWARD) and motor.setSpeed(200). With four channels, the shield suits projects with three or four small motors that each draw less than 0.6 A. The Bluetooth-controlled robot car on this site uses this shield, in the Adafruit Motor Shield V1.2 version.

Quick Comparison Table

FeatureL298N ModuleArduino Motor ShieldL293D Shield
DC motors supported22Up to 4
Current per channel2 A continuous2 A continuous0.6 A continuous
Motor voltage range5 V to 35 V5 V to 12 V4.5 V to 25 V
Form factorStandalone module with wiresShield (plugs onto Arduino)Shield (plugs onto Arduino)
PWM speed controlENA/ENB pins (PWM)PWM A/B pinsVia AFMotor library
Pin flexibilityHigh (you choose the pins)Low (fixed pin mapping)Low (fixed pin mapping)
CostVery lowModerateLow
Best forCustom wiring, robot cars, learning H-bridge fundamentalsQuick prototyping with minimal wiringMulti-motor projects needing simple code

For a first robot car, the L298N is a good choice: it is cheap and handles up to 2 A per channel. Its main drawback is the voltage drop of 1.4 V to 2 V, which wastes power and heats the module. For low-voltage motors, a driver with MOSFET switches (transistors that drop far less voltage than BJTs), such as the TB6612FNG, loses less.

Frequently Asked Questions

Can I use the L298N with an ESP32 or another 3.3 V board?

Yes. The L298N inputs read about 2.3 V and above as HIGH, so 3.3 V logic works. Connect the grounds and make sure the module gets its 5 V logic power, from the onboard 5 V regulator or from outside (see Power Supply and Safety).

Can the L298N drive a stepper motor?

Yes, one bipolar stepper motor (a stepper with two coils, one per H-bridge), using both channels.

Resources and Next Projects

The two robot car builds linked above use the same H-bridge principle on a motor shield.

3 thoughts on “L298N Motor Driver with Arduino: DC Motor Control Guide”

Leave a Comment