How to Interface DC Motors with Arduino UNO using L298N Motor Driver

shape
shape
shape
shape
shape
shape
shape
shape
blog-details
June, 2021

INTRODUCTION

In this tutorial, we are going to learn how to interface 2 DC Motors to an Arduino UNO using L298N Motor Driver. And we will also learn briefly about its working principles.

THEORY

Let's discuss a DC Motor.

DC(Direct Current) motor converts the electrical energy to mechanical energy.

Basically, it operates on a DC current(5V-12V).

 

 

Working Principle- When DC current passes the coil in a magnetic field, the magnetic force produces a torque that turns the motor.

 

 

 

Now let's discuss the Motor Driver.

If we supply the power to the motor, we need high voltage to operate it.

But microprocessor output is low, and it cannot give enough power to drive a motor.

To supply this voltage/current from the microprocessor to the motor, we need this Motor driver in between the motor and controller.

 

 

It has 4 i/o pins and 7 ports.

Pins

i)IN1

ii)IN2

iii)IN3

iv)IN4

Ports

i)OUT1

ii)OUT2

iii)OUT3

iv)OUT4

v)5V 

vi)GND

vii)12V

 

 

 

REQUIREMENTS

 

Parts Quantity
Arduino UNO 1
A-B cable 1
L293N 1
DC Motor 2
Bread Board 1
Battery(9V) 1
Connecting Wire As per required

 

CIRCUIT DIAGRAM

 

 

NOTE: Without the battery, the motors will rotate slowly.

 

 

PROGRAM

For programming this concept we need to understand some basic syntax of Arduino IDE.

The first thing is we need to install ArduinoAfter installing open it and write the program.

Let's declare the ports of Motor Driver in which DC Motors are connected.

 

#define m1_1 2 //Motor1_1st point
#define m1_2 3 //motor1_2nd point
#define m2_1 4 //Motor2_1st point
#define m2_2 5 //Motor2_1st point

 

Then, we have to define the motors as an output device in-between void setup function.

 

void setup() 
{
    pinMode(m1_1,OUTPUT);
    pinMode(m1_2,OUTPUT);
    pinMode(m2_1,OUTPUT);
    pinMode(m2_2,OUTPUT);
}

 

Then move towards the void loop function to rotate the motor.

 

void loop() 
{
    digitalWrite(m1_1,HIGH); // 1st Motor rotates in one direction
    digitalWrite(m1_2,LOW);
    delay(50);
    digitalWrite(m1_1,LOW);
    digitalWrite(m1_2,HIGH); // 1st Motor rotates in another direction
    delay(50);    
    digitalWrite(m2_1,HIGH); // 2nd Motor rotates in one direction
    digitalWrite(m2_2,LOW);
    delay(50);
    digitalWrite(m2_1,LOW);
    digitalWrite(m2_2,HIGH); // 2nd Motor rotates in another direction
    delay(50); 
}   

 

FINAL CODE

 

/*
 Tested by robogenesis. in
*/


#define m1_1 2 
#define m1_2 3 
#define m2_1 4 
#define m2_2 5

void setup() 
{
    pinMode(m1_1,OUTPUT);
    pinMode(m1_2,OUTPUT);
    pinMode(m2_1,OUTPUT);
    pinMode(m2_2,OUTPUT);
}

void loop() 
{
    digitalWrite(m1_1,HIGH);
    digitalWrite(m1_2,LOW);
    delay(50);
    digitalWrite(m1_1,LOW);
    digitalWrite(m1_2,HIGH); 
    delay(50);    
    digitalWrite(m2_1,HIGH);
    digitalWrite(m2_2,LOW);
    delay(50);
    digitalWrite(m2_1,LOW);
    digitalWrite(m2_2,HIGH); 
    delay(50); 
}   

 

Comments

Leave a Reply