Last Updated: 09/08/2021

L298N Motor Driver

Components >> L298N Motor Driver

What is a L298N controller board?

The L298N controller board is capable of controlling normal DC motors of stepper motors.

It can handle voltages between 5-35v and can handle power up to 2 amps, way more than an Arduino could handle.


Why use one?

An Arduino cannot handle the power to drive an electric motor so this board acts as a controller for the motor/s. The Arduino sends signals to the L298N to tell it what motors should be powered and what direction that power should be applied allowing motors to be turned backwards or forwards.

The board can drive 2 DC motors, for those doing the robot car project two motors attached to one port is treated as a single motor.

The controller can also power a stepper motor, allowing the motor to move an exact number of steps.

L298 schematic

Below is the schematic for the L298N

Motor control pins connect to Arduino OUTPUT pins.

Leave the 5v Enable connector in place.

The enable pins when connected to 5v turn the controller on for that motor. You will see from the photo that connectors have been fitted between the enable pin and the 5v pin above it to allow the motor to be used. This could have been supplied with a 5v from an Arduino pin if needed.

The 12v input is the power input for the motors and can be between 5-35v depending on the motors you are using.

NOTE: the controller loses about 1.5v internally so if you supply 6v to the board the motors will only get about 4.5v.



The following table shows how to use the control pins.

L298N Control pins

This is an example wiring diagram from the Robot Car project

L298N Example wiring diagram

Controlling the L298N board with an Arduino Uno

Tutorial contents

This first tutorial will just deal with controlling DC motors.

Writing a test sketch for the L298N
Writing Functions to simplify control.

Example 1: L298Nv1BasicControl.ino

For those doing the robot car who just want to check their wiring just download the code and upload it to your Arduino,
The left wheels should go forwards for 5 seconds
Stop for 5 seconds
Left wheels reverse for 5 seconds
Stop for 5 seconds
Right hand wheels forwards for 5 seconds.
Stop for 5 seconds.
Right hand wheels reverse for 5 seconds.
Stop for 5 seconds.
repeat above.
If you open the Serial monitor it will show you the command being sent..

Click to Download code:L298Nv1BasicControl.ino

This code uses lessons learnt in:
Lesson2 "Hello World"
Lesson 7: delay();
Functions 1: digitalWrite();

 
/* 29/07/2021
 *  
 *  L298Nv1BasicControl
 *  
 *  Simple Sketch to control the L298N Motor controller.
 *  
 *  L298N Configuration
 *  Both Enable pin jumpers fitted...
 *  This gives 5v to the enable pins that allows the motor to be controlled.
 *  Simple version the enable pin is the master on off switch, 5v means it's on.
 *  
 *  Wiring from Arduino to LN298N
 *  Arduino pin 11 to L298N pin IN1
 *  Arduino pin 10 to L298N pin IN2
 *  Arduino pin 9 to L298N pin IN3
 *  Arduino pin 8 to L298N pin IN4
 *  
 *  Wire from Arduino Ground (GND) to Ground pin that the battery is attached to (Black)
 * 
 */
  //define some constant vaiariables for the opins used to control the L298N
 const int leftForwardPin = 10;
 const int leftReversePin = 11;
 const int rightForwardPin = 8;
 const int rightReversePin = 9;


void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("L298Nv1BasicControl...");
  //this will print a blank line
  Serial.println(" ");

  //Tell the Arduino what we we will be using the pins for.
  //OUTPUT means that we will be sending data from the pin
  pinMode(leftForwardPin, OUTPUT);
  pinMode(leftReversePin, OUTPUT);
  pinMode(rightForwardPin, OUTPUT);
  pinMode(rightReversePin, OUTPUT);
}

void loop() {
  // Lets start by turning the wheels forwards on the left side
  //Making the pin HIGH means that a 5v signal will be sent
  Serial.println("left motor forwards");
  digitalWrite(leftForwardPin, HIGH); 
  //Making the pin LOW means we turn off the 5v and the pin returns to 0v 
  digitalWrite(leftReversePin, LOW);
  //wait 5 seconds  
  delay(5000);
  //Stop the motor
  Serial.println("Stop left motor");
  digitalWrite(leftForwardPin, LOW);
  delay(5000);
  Serial.println("Reverse left motor");
  digitalWrite(leftReversePin, HIGH);
  delay(5000);
  Serial.println("Stop left motor");
  digitalWrite(leftReversePin, LOW);
  delay(5000);

  Serial.println("right motor forwards");
  digitalWrite(rightForwardPin, HIGH); 
  //Making the pin LOW means we turn off the 5v and the pin returns to 0v 
  digitalWrite(rightReversePin, LOW);
  //wait 5 seconds  
  delay(5000);
  //Stop the motor
  Serial.println("Stop right motor");
  digitalWrite(rightForwardPin, LOW);
  delay(5000);
  Serial.println("Reverse right motor");
  digitalWrite(rightReversePin, HIGH);
  delay(5000);
  Serial.println("Stop right motor");
  digitalWrite(rightReversePin, LOW);
  delay(5000);
}

Example 2: L298Nv2BasicControlFunctions.ino

Click to Download code:L298Nv2BasicControlFunctions.ino

This code uses lessons learnt in:
Lesson 9: Basics of creating and using functions

 
/* 29/07/2021
 *  
 *  L298Nv2ControlFunctions
 *  
 *  This sketch puts the basic commands into functions
 *  
 *  L298N Configuration
 *  Both Enable pin jumpers fitted...
 *  This gives 5v to the enable pins that allows the motor to be controlled.
 *  Simple version the enable pin is the master on off switch, 5v means it's on.
 *  
 *  Wiring from Arduino to LN298N
 *  Arduino pin 11 to L298N pin IN1
 *  Arduino pin 10 to L298N pin IN2
 *  Arduino pin 9 to L298N pin IN3
 *  Arduino pin 8 to L298N pin IN4
 *  
 *  Wire from Arduino Ground (GND) to Ground pin that the battery is attached to (Black)
 * 
 */

 const int leftForwardPin = 10;
 const int leftReversePin = 11;
 const int rightForwardPin = 8;
 const int rightReversePin = 9;

//Best practise is to make sure both pins are set correctly for each direction 
//as we will not know what state the pin had been in before the function was called

//function to make the left motors run forwards
void forwardRight(){
  Serial.println("left motor forwards, turn right");
  digitalWrite(leftForwardPin, HIGH); 
  //Making the pin LOW means we turn off the 5v and the pin returns to 0v 
  digitalWrite(leftReversePin, LOW);
  //Right motor stopped, both pins low
  digitalWrite(rightForwardPin, LOW); 
  digitalWrite(rightReversePin, LOW);  
}
//function to make the left motors run in reverse
void reverseRight(){
  Serial.println("Reverse left motor, reverse right");
  digitalWrite(leftForwardPin, LOW); 
  digitalWrite(leftReversePin, HIGH);
  //Right motor stopped, both pins low
  digitalWrite(rightForwardPin, LOW); 
  digitalWrite(rightReversePin, LOW);  
}
//function to make the right motors run forwards
void forwardLeft(){
  Serial.println("right motor forwards, forwards left");
  digitalWrite(rightForwardPin, HIGH); 
  //Making the pin LOW means we turn off the 5v and the pin returns to 0v 
  digitalWrite(rightReversePin, LOW);
  //left motor stopped, both pins low
  digitalWrite(leftForwardPin, LOW); 
  digitalWrite(leftReversePin, LOW);   
}
//function to make the right motors run in reverse
void reverseLeft(){
  Serial.println("Reverse right motor, reverse left");
  digitalWrite(rightForwardPin, LOW); 
  digitalWrite(rightReversePin, HIGH);
  //left motor stopped, both pins low
  digitalWrite(leftForwardPin, LOW); 
  digitalWrite(leftReversePin, LOW);   
}

//this function calls both stop functions together
void stopMotors(){
  Serial.println("Stop motors");
  digitalWrite(rightForwardPin, LOW); 
  digitalWrite(rightReversePin, LOW); 
  digitalWrite(leftForwardPin, LOW); 
  digitalWrite(leftReversePin, LOW); 
}
//calls both forward functions
void driveForward(){
  Serial.println("Both motors forwards");
  //Both motors forwards pins HIGH
  digitalWrite(rightForwardPin, HIGH); 
  digitalWrite(rightReversePin, LOW); 
  digitalWrite(leftForwardPin, HIGH); 
  digitalWrite(leftReversePin, LOW); 
  
}
//calls both reverse functions
void driveReverse(){
  Serial.println("Both motors reverse");
  //Both motors reverse pins HIGH
  digitalWrite(rightForwardPin, LOW); 
  digitalWrite(rightReversePin, HIGH); 
  digitalWrite(leftForwardPin, LOW); 
  digitalWrite(leftReversePin, HIGH); 
}

void setup() {
  // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
  Serial.begin(9600);
  //Send script name
  Serial.println("L298Nv2ControlFunctions...");
  //this will print a blank line
  Serial.println(" ");

  //Tell the Arduino what we we will be using the pins for.
  //OUTPUT means that we will be sending data from the pin
  pinMode(leftForwardPin, OUTPUT);
  pinMode(leftReversePin, OUTPUT);
  pinMode(rightForwardPin, OUTPUT);
  pinMode(rightReversePin, OUTPUT);
}



void loop() {
  //start driving forwards
  driveForward(); 
  //keep going for 3 seconds (3000 milliseconds) 
  delay(3000);
  //stop the left motor
  stopMotors();
  delay(3000);
  //turn right
  forwardRight();
   
  delay(3000);
  //turn left
  forwardLeft();
   
  delay(3000);
  //Stop the motors
  stopMotors();
  
  delay(3000);
  //Reverse car
  driveReverse();
  
  delay(3000);
  //stop the left motor
  stopMotors();
  
  delay(3000);
  //reverse left
  reverseRight();
  
  delay(3000);
  //reverse left
  reverseLeft();
  
  delay(3000);
}

Additional Resource Links

Don't forget to use the Reference in you Arduino IDE and look at the Control Structures and Comparison Operators for more help on this lesson.

Comments


This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site.
To add a comment or ask a question please email the address in this image: and use L298N Motor Driver as a reference.