How to use the L298N Motor Driver

How to use the L298N Motor Driver © GPL3+

A quick tutorial on how the use the L298N Motor Controller with DC motors

  • 12 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

About

The L298N Motor Driver is a controller that uses an H-Bridge to easily control the direction and speed of up to 2 DC motors. This tutorial will show you how to use it.

Video Instructions

Text Instructions

Hardware:

1.) The controller can take up to 2 motors. Plug one motor into the terminal labelled OUT1 and OUT2. Plug the second motor into the terminal labelled OUT3 and OUT4:

2.) The row of pins on the bottom right of the L298N control the speed and direction of the motors. IN1 and IN2 control the direction of the motor connected to OUT1 and OUT2. IN3 and IN4 control the direction of the motor connected to OUT3 and OUT4. Here I plugged them into pins 2, 3, 4, and 5 on the Arduino.

3.) You can power the L298N with up to 12V by plugging your power source into the pin on the L298N labelled "12V". The pin labelled "5V" is a 5V output that you can use to power your Arduino. Here are some ways to wire it depending on your use case:

Powering Arduino with L298N

Powering L298N and Arduino Separately:

Powering L298N with Arduino:

Software:

4.) Write the code. Setting IN1 to HIGH and IN2 to LOW will cause the left motor to turn a direction. Setting IN1 to LOW and IN2 to HIGH will cause the left motor to spin the other direction. The same applies to IN3 and IN4. Here is a short example (You can download the full code in the "Code" section at the bottom of the page):

digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);

digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);

Speed:

5.) You can change the speed with the EN pins using PWM. ENA controls the speed of the left motor and ENB controls the speed of the right motor. Here I plugged them into pins 9 and 10 on the Arduino. This is optional and the motors will still run if you don't do this.

6.) To change the speed in the code, use the analogWrite() function (more info) on the ENA and ENB pins. Here is an example (You can download the full code in the "Code" section at the bottom of the page):

analogWrite(ENA_pin, 50);

Code

Code without speed controlC/C++
int motor1pin1 = 2;
int motor1pin2 = 3;

int motor2pin1 = 4;
int motor2pin2 = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:   
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor1pin2, LOW);

  digitalWrite(motor2pin1, HIGH);
  digitalWrite(motor2pin2, LOW);
  delay(1000);

  digitalWrite(motor1pin1, LOW);
  digitalWrite(motor1pin2, HIGH);

  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH);
  delay(1000);
}
Code with speed controlC/C++
int motor1pin1 = 2;
int motor1pin2 = 3;

int motor2pin1 = 4;
int motor2pin2 = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);

  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:   

  //Controlling speed (0 = off and 255 = max speed):
  analogWrite(9, 100); //ENA pin
  analogWrite(10, 200); //ENB pin

  //Controlling spin direction of motors:
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor1pin2, LOW);

  digitalWrite(motor2pin1, HIGH);
  digitalWrite(motor2pin2, LOW);
  delay(1000);

  digitalWrite(motor1pin1, LOW);
  digitalWrite(motor1pin2, HIGH);

  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH);
  delay(1000);
}

Schematics

9v_a3_ksdDAxLdBz.png
L298N powering Arduino
9v a3 ksddaxldbz

Comments

Similar projects you might like

ESP8266 + L298N Motor Drive + Smartphone

by luciorocha

  • 29,639 views
  • 11 comments
  • 36 respects

Arduino Half-Step Stepper Motor Driver L298N

by JayV

  • 7,060 views
  • 1 comment
  • 5 respects

Power Efficient Motor Driver

Project in progress by Patel Darshil

  • 3,088 views
  • 5 comments
  • 13 respects

Arduino L293D Motor Driver Shield Tutorial

by ElectroPeak

  • 46,456 views
  • 0 comments
  • 20 respects

Check a Stepper Motor From the DVD-ROM

by Ingo Lohs

  • 27,668 views
  • 2 comments
  • 45 respects
Add projectSign up / Login