electroSome

Interfacing L298N Motor Driver with Arduino Uno

L298N Motor Driver Module

L298N Motor Driver Module

In this tutorial we will learn how to interface L298N motror driver with Arduino Uno. You might be thinking why we need L298N for controlling a motor. The answer is very simple, Arduino board or a microcontroller IO pins don’t have enough current/voltage driving capability to drive a motor. For driving the motor in both directions (clockwise and anti-clockwise) we need to use an H-Bridge. Please read our article H-Bridge – DC Motor Driving for more information. L298N is an integrated monolithic circuit with dual H-Bridge. It can be used to rotate the motor in both directions and to control the speed of the motor using PWM technique.

Components Required

L298N Motor Driver Module

L298N Motor Driver Connections Explained

Specifications

Working Principle

Here is a simple explanation of H-Bridge motor driving. For more details please read our article, H-Bridge – DC Motor Driving.

H-Bridge consists of 4 MOSFETs or Transistors wired as switches. For easy understanding refer the below circuit with 4 switches. Now let’s imagine, if the switch S1 and S4 are ON we can see that current will flow from left to right direction of the motor. This will make the motor rotate in particular direction, say clockwise.

Basic H Bridge Working

Similarly if switches S3 and S2 are switched on, the current will flow from right to left, so the motor will rotate in the other direction, say anti-clockwise.

Basic H Bridge Working

L298N module is having 2 H-Bridge like this. One thing to note in H-Bridge is that we should not switch on S1 and S2 together or S3 and S4 together. This condition is called shoot-through and can damage those MOSFETs or transistors.

Circuit Diagram

Interfacing L298N Motor Driver Arduino Uno – Circuit Diagram

Description

Program

// Motor A connections
int enA = 9;
int in1 = 13;
int in2 = 12;
// Motor B connections
int enB = 3;
int in3 = 11;
int in4 = 10;

void setup() {
    pinMode(enA, OUTPUT);
    pinMode(enB, OUTPUT);
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
	
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
}

void loop() {
    directionControl();
    delay(1000);
    speedControl();
    delay(1000);
}
void directionControl() {
    // Set motors to maximum speed
    // PWM value ranges from 0 to 255
    analogWrite(enA, 255);
    analogWrite(enB, 255);

    // Turn on motor A & B
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(2000);

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    delay(2000);
	
    // Turn off motors
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
}
void speedControl() {
    // Turn on motors
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
	
    for (int i = 0; i < 256; i++) {
        analogWrite(enA, i);
        analogWrite(enB, i);
        delay(20);
    }
	
    for (int i = 255; i >= 0; --i) {
        analogWrite(enA, i);
        analogWrite(enB, i);
        delay(20);
    }
	
    // Now turn off motors
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
}
 

Explanation

Output

Conclusion

Hope you understand about driving DC motor using Arduino Uno and H-Bridge. Please feel free to comment below if you have any doubts.