Introduction: How to Use L298n to Control Dc Motor With Arduino

About: Hi, I'm an electronics and Arduino amateur, I try to share with you my experience and little projects, hope you like it. visit my youtube channel for more projects goo.gl/KSZVRG

Hello everybody,

Welcome to this tutorial, check the video first it contains a little bit of explanation, we are here using a L298n Dual H-bridge driver to control a DC motor using Arduino, you can check more on the internet if you need further information but here we are to make things work so you can adapt it to your projects easily, Hope you like it.

Step 1: Simple Control, and Changing Direction

In this step you can wire the driver like in the picture or change it to adapt it to your project, don't forget to wire the GND from the driver with the GND from Arduino, for the powering here I used a 9v battery, you can power it from a power source or battery, also the +5v pin from the driver can power your Arduino if you put it in Vin from Arduino board,

Here I only used one motor, this module can run 2 DC motors or 1 stepper motor.

Test 1: First we try to turn on the motor to a one direction then turn it off, so you wire it like in the picture and here's the code.

//This code is to use with L298n Dual H-bridge motor driver<br>//It just turns on a DC motor for a certain time and turn it off
//refer to surtrtech.blogspot.com for more information
int in1 = 9; //Declaring the pins where in1 in2 from the driver are wired 
int in2 = 8; //here they are wired with D9 and D8 from Arduino
void setup() {
  pinMode(in1, OUTPUT); //Declaring the pin modes, obviously they're outputs
  pinMode(in2, OUTPUT);
}
//Before starting the loop you should create functions type "void" to control the driver's pins
//Here I created two functions, the first one turns a motor to a direction (you can change it by switching LOW and HIGH
//and the second one to stop the motor
void TurnMotorA(){              
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
}
void TurnOFFA(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
void loop() {
  TurnMotorA(); //in the loop we use the function to turn the motor for 3s and stop it for 2s
  delay(3000);
  TurnOFFA();
  delay(2000);
}

Test 2: Turing on/off, changing direction then turn on/off (you keep the same wiring as above)

//This code is to use with L298n Dual H-bridge motor driver<br>//It just turns on a DC motor for a certain time in a direction, turn it off, turn in the other direction and turn it off again
//refer to surtrtech.blogspot.com for more information
int in1 = 9; //Declaring the pins where in1 in2 from the driver are wired 
int in2 = 8; //here they are wired with D9 and D8 from Arduino
void setup() {
  pinMode(in1, OUTPUT); //Declaring the pin modes, obviously they're outputs
  pinMode(in2, OUTPUT);
}
//Before starting the loop you should create functions type "void" to control the driver's pins
//Here I created three functions, one to turn the motor in a direction "#1", the other one to the other direction "#3"
//and the second one to stop the motor
//For changing directions you switch the HIGH with LOW and vice-versa
void TurnMotorA(){
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
}
void TurnOFFA(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
void TurnMotorA2(){
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
}
void loop() {
  TurnMotorA(); // We turn to direction 1 for 3s then stop for 2s
  delay(3000);
  TurnOFFA();
  delay(2000);
  TurnMotorA2(); // We turn to direction 2 for 3s then stop for 2s
  delay(3000);
  TurnOFFA();
  delay(2000);
}

Step 2: Speed Control

In this step we will remove the jumper that'on the ENA pin, and we will control this pin by a PWM signal delivred from Arduino board, and it will permit us to control the speed of the DC motor.

Test 3: Turning on/off with low speed then turning on/off with high speed ( the wiring is like the previous but now you remove the jumper and connect the enA pin with pwm pin from Arduino, here I used D10)

//This code is to use with L298n Dual H-bridge motor driver
//It just turns on a DC motor for a certain time with a low speed and turn it off then turn on with high speed //refer to surtrtech.blogspot.com for more information
int in1 = 9; //Declaring the pins where in1 in2 from the driver are wired 
int in2 = 8; //here they are wired with D9 and D8 from Arduino
int ConA = 10; //And we add the pin to control the speed after we remove its jumper 
               //Make sure it's connected to a pin that can deliver a PWM signal
void setup() {
  pinMode(in1, OUTPUT); //Declaring the pin modes, obviously they're outputs
  pinMode(in2, OUTPUT);
  pinMode(ConA, OUTPUT);
}
//Before starting the loop you should create functions type "void" to control the driver's pins
//here I created three functions, the first one is to turn the motor to a direction with speed(100)
//The second one to turn it off
//And the last one to turn it in the same direction as the first but higher speed(250)
//Speed range (0-255)
void TurnMotorA(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ConA,100);
}
void TurnOFFA(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(ConA,0);
}
void TurnMotorA2(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ConA,250);
}
void loop() {
  TurnMotorA();  //Sequence: turning on low speed, stop, turning again in high speed and stop
  delay(2000);
  TurnOFFA();
  delay(2000);
  TurnMotorA2();
  delay(4000);
  TurnOFFA();
  delay(2000);
}