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 about H-bridges if you want to understand or even make your own one.
If you’re a bit familiar with DC motors (motors widely used in toys and robots) you’ll notice that if you switch polarity the rotation direction will change too, so in order to go forward and backward you need to switch the wires… but that’s not practical, you need something to do it electronically without modifying any wires: This is where the H-bridge comes handy.
The module I used is a L298n Dual H-bridge, it’s often used with Arduino, it can control 2 DC motors at the same time, and you can control the direction and the speed as well. This module can control a Stepper motor as well ( but that’s for another day), in case you need to control your stepper motor, here’s an alternative: How to use a stepper motor with Arduino and its driver ULN2003.
Here are some circuit schematics that I took from wikipedia:
Just in the practical form, the switches are replaced by “Transistors” and every two (verticales) are control by a single pin, that’s why on the module you’ll notice 4 controlling pins, 2 for each motor.
Here’s a picture of the module pins that I took from https://www.navestar.com/

N.B:
- The “Logic Input” pins control the directions: Forward, Backward and Stop, each two of them control A motor.
- 12V power is not always 12V it can be 9V, or it can be powered using up to 47VDC but you have to remove the regulator jumper or you’ll burn it, the regulator can support only up to 12V.
- Enable A/B are for controlling the speed, if their jumpers are kept, the speed will be the maximum, they can handle up to 5V
- The module can be powered using Arduino but that’s not recommended at all, it’s better to use external power, and you can power the Arduino through the module too via the 5V/Gnd pins
- Don’t forget that the GND pin should always be wired with Arduino.
Wirings: (no speed control)
The one that permits speed control is below.
Wiring 1: Power the module
Wiring 2: Wiring the module with Arduino (without speed control)
Codes: (no speed control)
Code 1: Direct turn on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//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.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//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.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 enA/B jumper
Code: Speed control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
//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.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); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
4 thoughts on “Step by step on how to use the L298n dual H-bridge driver with Arduino” Leave a comment ›