Interface L298N DC Motor Driver Module with Arduino

If you’re planning on assembling your new robot, you’ll eventually want to learn how to control DC motors. The easiest and affordable way to control DC motors is to interface the L298N motor driver with the Arduino. It can control both the speed and the spinning direction of two DC motors.

And as a bonus, it can also control a bipolar stepper motor like the NEMA 17. If you want to know more about it, check this tutorial out.

Controlling a DC Motor

To have complete control over DC motor we have to control its speed and rotation direction. This can be achieved by combining these two techniques.

  • PWM – to control speed
  • H-Bridge – to control the rotation direction

let’s get to know them.

PWM – to control speed

The speed of a DC motor can be controlled by changing its input voltage. A common technique to do this is to use PWM (Pulse Width Modulation).

PWM is a technique where the average value of the input voltage is adjusted by sending a series of ON-OFF pulses.

The average voltage is proportional to the width of the pulses known as the Duty Cycle.

The higher the duty cycle, the higher the average voltage applied to the DC motor (resulting in higher speed) and the shorter the duty cycle, the lower the average voltage applied to the DC motor (resulting in lower speed).

The image below shows PWM technology with different duty cycles and average voltages.

Pulse Width Modulation PWM Technique with Duty Cycles
Pulse Width Modulation(PWM) Technique

H-Bridge – to control the rotation direction

The spinning direction of a DC motor can be controlled by changing the polarity of its input voltage. A common technique for doing this is to use an H-bridge.

An H-bridge circuit consists of four switches with the motor in the center forming an H-like arrangement.

Closing two specific switches at a time reverses the polarity of the voltage applied to the motor. This causes a change in the spinning direction of the motor.

The following animation shows the working of the H-bridge circuit.

H-Bridge Working Motor Direction Control Animation
Working of H-Bridge

L298N Motor Driver Chip

At the center of the module is a big, black chip with a chunky heat sink – the L298N.

L298N Motor Driver Module - IC

Inside the L298N chip, you’ll find two standard H-bridges capable of driving a pair of DC motors. This means it can drive up to two motors individually which makes it ideal for building a two-wheeled robotic platform.

The L298N motor driver has a supply range of 5V to 35V and is capable of 2A continuous current per channel, so it works very well with most of our DC motors.

Technical Specifications

Here are the specifications:

Motor output voltage5V – 35V
Motor output voltage (Recommended)7V – 12V
Logic input voltage5V – 7V
Continuous current per channel2A
Max Power Dissipation25W

For more details, please refer below datasheet.

L298N Motor Driver Module Pinout

The L298N module has a total of 11 pins that connect it to the outside world. The pins are as follows:

L298N Motor Driver Module Pinout

Let’s get acquainted with all the pins one by one.

Power Pins

The L298N motor driver module is powered through 3-pin 3.5mm-pitch screw terminal.

l298n module power pins

The L298N motor driver actually has two input power pins – VS and VSS.

VS pin gives power to the internal H-Bridge of the IC to drive the motors. You can connect an input voltage anywhere between 5 to 12V to this pin.

VSS is used to drive the logic circuitry inside the L298N IC which can be 5 to 7V.

GND is the common ground pin.

Output Pins

The L298N motor driver’s output channels OUT1 and OUT2 for motor A and OUT3 and OUT4 for motor B are broken out to the edge of the module with two 3.5mm-pitch screw terminals. You can connect two 5-12V DC motors to these terminals.

l298n module motor output connection pins

Each channel of the module can deliver up to 2A to the DC motor. However the amount of current supplied to the motor depends on the power supply of the system.

Direction Control Pins

By using the direction control pins, you can control whether the motor rotates forward or backward. These pins actually control the switches of the H-Bridge circuit inside the L298N chip.

l298n module direction control pins

The module has two direction control pins for each channel. The IN1 and IN2 pins control the spinning direction of motor A; While IN3 and IN4 control the spinning direction of motor B.

The spinning direction of the motor can be controlled by applying logic HIGH (5V) or logic LOW (Ground) to these inputs. The chart below shows how this is done.

Input1Input2Spinning Direction
Low(0)Low(0)Motor OFF
High(1)Low(0)Forward
Low(0)High(1)Backward
High(1)High(1)Motor OFF

Speed Control Pins

The speed control pins ENA and ENB are used to turn on/off the motors and control its speed.

l298n module speed control pins

Pulling these pins HIGH will cause the motors to spin, while pulling it LOW will stop them. But, with Pulse Width Modulation (PWM), you can actually control the speed of the motors.

The module usually comes with a jumper on these pins. When this jumper is in place, the motor spins at maximum speed. If you want to control the speed of the motors programmatically, you need to remove the jumpers and connect them to the PWM-enabled pins on the Arduino.

On-board 5V Regulator and Jumper

The module has an on-board 5V regulator – 78M05. It can be enabled or disabled via a jumper.

l298n module 5v regulator and enable jumper

When this jumper is in place, the 5V regulator is enabled, which derives the logic power supply (VSS) from the motor power supply (VS). In this case, the 5V input terminal acts as the output pin and delivers 5V 0.5A. You can use it to power an Arduino or other circuitry that requires a 5V power supply.

When the jumper is removed, the 5V regulator is disabled and we have to separately supply 5V through the VSS pin.

Warning:

You can leave the jumper in place if the motor power supply is less than 12V. If it is higher than 12V you must remove the jumper to prevent damage to the onboard 5V regulator.

Also DO NOT supply power to both the VSS and VS pins while the jumper is in place.

Voltage Drop of L298N

The voltage drop of the L298N is about 2V. This is due to the internal voltage drop across the switching transistors in the H-bridge circuit.

So if you connect 12V to the motor power supply terminal, the motors will receive a voltage of about 10V. This means that a 12V DC motor will never spin at its maximum speed.

L298N Motor Driver Module Internal Voltage Drop

In order to get maximum speed from the motor, the motor power supply should have a voltage slightly higher (+2V) than the actual voltage requirement of the motor.

Considering a voltage drop of 2V, if you are using 5V motors, you will need to provide 7V at the motor power supply terminal. If you have 12V motors then your motor supply voltage should be 14V.

Wiring an L298N Motor Driver Module to an Arduino

Now that we know everything about the module, we can start connecting it to our Arduino!

Let’s start by connecting the power supply to the motors. In our experiment we are using DC gearbox motors (also known as ‘TT’ motors) commonly found in two-wheel-drive robots. They are rated for 3 to 12V. Therefore, we will connect the external 12V power supply to the VS terminal. Considering the internal voltage drop of the L298N IC, the motors will receive 10V and spin at a slightly lower RPM. But that’s fine.

Next, we need to supply 5V to the logic circuitry of the L298N. We will be using the on-board 5V regulator to get 5V from the motor power supply, so leave the 5V-EN jumper in place.

Now connect the L298N module’s Input and Enable pins (ENA, IN1, IN2, IN3, IN4 and ENB) to the six Arduino digital output pins (9, 8, 7, 5, 4 and 3). Note that Arduino output pins 9 and 3 are both PWM-enabled.

Finally connect one motor to terminal A (OUT1 and OUT2) and the other motor to terminal B (OUT3 and OUT4). You can interchange the connections of your motor. There is technically no right or wrong way.

When you are done you should have something that looks similar to the illustration shown below.

Wiring L298N Motor Driver Module with DC TT motors and Arduino UNO
Wiring DC motors to L298N motor driver & Arduino

Arduino Example Code

The following sketch will give you a complete understanding of how to control the speed and spinning direction of a DC motor with the L298N Motor Driver and will serve as the basis for more practical experiments and projects.

// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;

void setup() {
	// Set all the motor control pins to outputs
	pinMode(enA, OUTPUT);
	pinMode(enB, OUTPUT);
	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);
	
	// Turn off motors - Initial state
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
}

void loop() {
	directionControl();
	delay(1000);
	speedControl();
	delay(1000);
}

// This function lets you control spinning direction of motors
void directionControl() {
	// Set motors to maximum speed
	// For PWM maximum possible values are 0 to 255
	analogWrite(enA, 255);
	analogWrite(enB, 255);

	// Turn on motor A & B
	digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, HIGH);
	digitalWrite(in4, LOW);
	delay(2000);
	
	// Now change motor directions
	digitalWrite(in1, LOW);
	digitalWrite(in2, HIGH);
	digitalWrite(in3, LOW);
	digitalWrite(in4, HIGH);
	delay(2000);
	
	// Turn off motors
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
}

// This function lets you control speed of the motors
void speedControl() {
	// Turn on motors
	digitalWrite(in1, LOW);
	digitalWrite(in2, HIGH);
	digitalWrite(in3, LOW);
	digitalWrite(in4, HIGH);
	
	// Accelerate from zero to maximum speed
	for (int i = 0; i < 256; i++) {
		analogWrite(enA, i);
		analogWrite(enB, i);
		delay(20);
	}
	
	// Decelerate from maximum speed to zero
	for (int i = 255; i >= 0; --i) {
		analogWrite(enA, i);
		analogWrite(enB, i);
		delay(20);
	}
	
	// Now turn off motors
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
}

Code Explanation:

The Arduino code is pretty straightforward. It doesn’t require any libraries to work. The sketch begins with declaring the Arduino pins to which the L298N’s control pins are connected.

// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;

In the setup section of the code all the motor control pins (both direction and speed control pins) are declared as digital OUTPUT and the direction control pins are pulled LOW to turn off both the motors.

void setup() {
	// Set all the motor control pins to outputs
	pinMode(enA, OUTPUT);
	pinMode(enB, OUTPUT);
	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);
	
	// Turn off motors - Initial state
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
}

In the loop section of the code we call two user defined functions at an interval of one second.

void loop() {
	directionControl();
	delay(1000);
	speedControl();
	delay(1000);
}

These functions are:

  • directionControl() – This function makes both motors spin forward at maximum speed for two seconds. It then reverses the motor’s spinning direction and spins for two seconds. Finally it turns off the motors.

    void directionControl() {
    	// Set motors to maximum speed
    	// For PWM maximum possible values are 0 to 255
    	analogWrite(enA, 255);
    	analogWrite(enB, 255);
    
    	// Turn on motor A & B
    	digitalWrite(in1, HIGH);
    	digitalWrite(in2, LOW);
    	digitalWrite(in3, HIGH);
    	digitalWrite(in4, LOW);
    	delay(2000);
    	
    	// Now change motor directions
    	digitalWrite(in1, LOW);
    	digitalWrite(in2, HIGH);
    	digitalWrite(in3, LOW);
    	digitalWrite(in4, HIGH);
    	delay(2000);
    	
    	// Turn off motors
    	digitalWrite(in1, LOW);
    	digitalWrite(in2, LOW);
    	digitalWrite(in3, LOW);
    	digitalWrite(in4, LOW);
    }
  • speedControl() – This function accelerates both motors from zero to maximum speed by producing a PWM signal using the analogWrite() function, then it decelerates them back to zero. Finally it turns off the motors.

    void speedControl() {
    	// Turn on motors
    	digitalWrite(in1, LOW);
    	digitalWrite(in2, HIGH);
    	digitalWrite(in3, LOW);
    	digitalWrite(in4, HIGH);
    	
    	// Accelerate from zero to maximum speed
    	for (int i = 0; i < 256; i++) {
    		analogWrite(enA, i);
    		analogWrite(enB, i);
    		delay(20);
    	}
    	
    	// Decelerate from maximum speed to zero
    	for (int i = 255; i >= 0; --i) {
    		analogWrite(enA, i);
    		analogWrite(enB, i);
    		delay(20);
    	}
    	
    	// Now turn off motors
    	digitalWrite(in1, LOW);
    	digitalWrite(in2, LOW);
    	digitalWrite(in3, LOW);
    	digitalWrite(in4, LOW);
    }