Step by step on how to use the L298n dual H-bridge driver with Arduino

 

Hello everybody,

Welcome to this tutorial, check the video first it contains a little bit of explanation, we are here using a L298n 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.

 

Wirings: (no speed control)

Wiring 1: Power the module

Wiring 2: Wiring the module with Arduino (without speed control)

Codes: (no speed control)

Code 1: Direct turn on

//This code is to use with L298n Dual H-bridge motor driver

//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);

}

 

Code 2: Turn on and change the direction

//This code is to use with L298n Dual H-bridge motor driver

//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);

}

 

Wiring: For speed control

Don’t forget to remove the jumper (obviously)

Code: Speed control

//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);

}

One thought on “Step by step on how to use the L298n dual H-bridge driver with Arduino

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: