Hi Guys Welcome to another Exciting tutorial of beginnertopro.in, So in this tutorial we will learn how to use LM298 properly so that your Robot will Work Perfectly, I am writing this blog because i am getting lots of Query that sir our Robot is not working, then after getting information from the users i always end up with the conclusion that they are not connecting the Motor driver properly. So here we will Discuss every single point that you should know so that you will have the complete knowledge before building your robot. So Lets Start.

## Features and Specifications

• Operating voltage range: +5 to +46V
• Maximum supply voltage:50V
• Maximum Input and Enable Voltage:+7V
• Maximum current allowed to draw through each output: 3A
• TTL control inputs
• Total power dissipation:25W
• Operating temperature: -23°C  to 130°C
• Storage Temperature: -40°C  to 150°C

So Here we have in total 8 points these are:

1. Motor pin 1

2. Motor pin 2

3. Motor pin 3

4. Motor pin 4

5. 12V input pin 5

6. Ground pin

7. 5V Out Put pin

8. Data Pins

So Now Lets Describe Each and Every Pin Of LM298 motor driver.

## Motor Pin 1 & Motor Pin 2

So Motor Pin 1 and 2 Connect with a single Motor's positive and negative pins. It always recommended that you always connect your motors Positive pin to the even  motor pin of the Lm298 Motor Driver. For Example if you want to connect a 12v Motor to the LM298 motor driver you should connect the -ve pin of the motor to the pin 1 of the lm298 motor drive ans +ve pin of the motor to the motor pin 2 of the motor driver. Its always recommend you to use 12V gear motor. Because LM298 motor driver gives 12v output in motor pin 1 & 2.

## Motor pin 3 & 4:

Similarly we will connect Motor pin 3 to negative pin of the 2nd motor and Motor pin 4 of Lm298 to +Ve pin of the 2nd motor.

## Power Management Pins: [5,6,7]

So here pin 5 is the 12v input power supply for LM298, if your power supply is more then 12V then just Remove the Jumper wire shown in the Below Picture.

So pin 6 is your Ground pin for both Input and out put power supply. Pin 7 is your 5V output if you need any 5v power supply for project then you can use it.

So Now Lets now go to the most important pin or you can say combination of pins that is the signal pins of LM298.

## Signal Pin's Of Motor Driver LM298:

So we have 4 pins of Motor Driver LM 298 from where we can control the Motor pin out puts Current Direction so that motor can rotate in forward or reverse direction.

Pins of LM298 Motor Driver are IN1,IN2,IN3,IN4

=>if motor driver will provide DC current in forward direction then DC Gear Motor will rotate in Clock wise direction

=>if motor driver will provide DC current in backward direction then DC Gear Motor will rotate in Anti-Clock wise direction.

## Motor Driver Truth Tables

Here are some handy tables to show the various modes of operation.

Motor 1 truth table
ENA IN1 IN2 Description
0 N/A N/A Motor 1 is off
1 0 0 Motor 1 is stopped (brakes)
1 0 1 Motor 1 is on and turning backwards
1 1 0 Motor 1 is on and turning forwards
1 1 1 Motor 1 is stopped (brakes)

Motor 2 truth table

ENB IN3 IN4 Description
0 N/A N/A Motor 2 is off
1 0 0 Motor 2 is stopped (brakes)
1 0 1 Motor 2 is on and turning backwards
1 1 0 Motor 2 is on and turning forwards
1 1 1 Motor 2 is stopped (brakes)

## Now Let's See Some Basic Concept Of DC Gear Motor:

DC Current means Direct Current so alternating in the current direction DC motor also change in the direction of Rotation. Lets have a look.

If we connect +ve pin of DC Motor to the +ve pin of dc power supply and -ve pin of the motor to the -ve pin of the power supply it will rotate in clock wise direction.

If we connect +ve pin of DC Motor to the -ve pin of dc power supply and -ve pin of the motor to the +ve pin of the power supply it will rotate in anti clock wise direction.

## Lets Now Combine Motor Rotation Concept with Motor Driver LM298

### 1. Logic for forward motion of robot:

According to truth table if we provide 5v to IN1, IN3 then Motor driver will provide DC current in forward direction which will rotate both motor 1 and motor 2 in clock wise direction which in return move the robot in forward direction.

Arduino Function For Forward Motion:

``````void goForward(){
digitalWrite(12,HIGH);
digitalWrite(11,LOW);
digitalWrite(10,HIGH);
digitalWrite(12,LOW);
}``````

2. Logic for backward motion of robot:

According to truth table if we provide 5v to IN2, IN4 then Motor driver will provide DC current in backward direction which will rotate both motor 1 and motor 2 in anti clock wise direction which in return move the robot in backward direction.

Arduino Code For Backward Motion

``````void goBackward(){

//Anti Clock Wise Rotation Of Motor 1
digitalWrite(12,LOW);
digitalWrite(11,HIGH);

//Anti Clock Wise Rotation Of Motor 2
digitalWrite(10,LOW);
digitalWrite(12,HIGH);

}``````

3.Logic for Right Movement of the Robot:

According to truth table if we provide 5v to IN1, IN4 then Motor driver will provide DC current in backward direction which will rotate motor 1 in clock wise direction and motor 2 in anti clock wise direction which in return move the robot in right direction.

Arduino Code For Right Direction Movement:

``````​
void goRight(){

//Clock Wise Rotation Of Motor 1
digitalWrite(12,HIGH);
digitalWrite(11,LOW);

//Anti Clock Wise Rotation Of Motor 2
digitalWrite(10,LOW);
digitalWrite(12,HIGH);

}
​``````

4. Logic for left movement of the Robot:

According to truth table if we provide 5v to IN2, IN3 then Motor driver will provide DC current in backward direction which will rotate motor 1 in anti clock wise direction and motor 2 in clock wise direction which in return move the robot in left direction.

Arduino Code For Lef Side Movement:

``````​
void goLeft(){

//Anti Clock Wise Rotation Of Motor 1
digitalWrite(12,LOW);
digitalWrite(11,HIGH);

//Clock Wise Rotation Of Motor 2
digitalWrite(10,HIGH);
digitalWrite(12,LOW);

}``````

Lets Go for the Next Topic

## How to Control the Speed Of the Motor using LM 298 motor driver:

Yes we can control the speed of the motors using LM 298 motor driver, there two enable pins are there, See the picture below:

What does enable pins do in LM298 Motor Driver??

Enable pins are by default provided with 5v. According to the voltage given to the Enable pins, it proportionally provides the out put current to the motors. So if  we can think like that as if we provide 5v in Motor out put we will get 12v, which means 100% speed of the motor. Mathematically we can write it as

Enable Pin Voltage / 12v = required speed / 100

So if you want that your motor should run in 80% of the total speed , then we have to provide voltage to enable pin is :80 * 5 / 100 = 4 , i.e 4V

Using this formula you can calculate the amount of voltage should be provided to the enable pins so that we can archive the required speed of the motor.

In Arduino 5v is considered as 1024, so for 4v we can consider the number is 819.2 ~= 820.

So what you have to do is remove the enable pins jumpers and connect them to the arduino, so that programmatically we can control the speed of the Motors.

Now lets see the arduino connection and code:

``````void setup(){
//Enable Pins
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
}

void loop(){

analogWrite(13,820); // Giving 4 volt to EN1
analogWrite(8,820); // Giving 4 volt to EN2
}``````

## Frequently Asked Question by beginnertopro users[FAQ]:

1. Which DC Motor should we use with LM298??

A: 12 V DC Gear Motor.

2.Minimum power supply needed for the LM298??

A: for proper functionality minimum power supply required is 9v.

3. Some time one of my motors rotating slowly??

A: yes this is a very common problem always we are facing, because of wrong connection, over voltage IC of LM298 driver partially got non functional that why such type of problems arises.

4.Always my robot moving to left direction??

A. Please check that your motor driver is properly connected with arduino, and in coding you have correctly assign the pin numbers and you should have properly provide voltage to the logic pins according to the Truth table given above.