Saturday, August 4, 2018

DC Motor Control using Arduino Uno and L298 Motor Driver

There are different methods of controlling DC motor . In this blog we will see how to control DC Motor using Arduino Programming and L298 Motor Driver.

Brief Overview :

There are different types of motors  eg : AC motors and DC Motors. Among those DC motor is most common and famous for Electrical DIY projects and Hobbyist. As working of  DC motor is very simple .Connecting positive and negative terminals of battery to DC motor would make motor will start rotating.
If we reverse the polarity of motor by switching the leads, the motor will rotate in reverse direction.

Pulse Width Modulation method is used to control the speed of DC Motor. In this method we control the voltage applied to the DC motor . The increase or decrease in voltage will increase or decrease the rotation speed of DC Motor.

Pulse Width Modulation Control of DC motor :

In Pulse Width Modulation ,the average voltage is controlled by varying the switching frequency . Switching Frequency is the rate at which turn on and off the power applied to control the motor. The Switching Frequency is in  order of few kilo Hertz.
So here comes the new term “Duty Cycle”. So duty cycle indicates the “on time” when the  PWM signal is high . Duty Cycle = Signal on time + Signal of time



Duty cycles may be expressed in perentage 10%, 40% ,50% ,90% and 100 % .




Average voltage according to Duty Cycles in Percentages(%):


In this way by controlling the duty cycle we can control the average voltage applied to the DC Motor. So to apply the voltage to motor  we use the application of MOSFET (metal-oxide semiconductor field-effect transistor). MOSFET has high switching frequency. PWM signals are given to Gate terminal of MOSFET using Arduino depending on the PWM Signal duty cycle the average voltage varies which in turn varies the speed of the DC Motor.


The circuit shown below gives detail idea of circuit wiring for Arduino and DC motor:

Arduino is programmed to vary the speed of DC motor by varying duty cycle of PWM signal.
Arduino Code  :
#------------------------------------------------------------------------------------------------#
int PWMPin = 10;
int motorSpeed = 0
void setup()
{
}
void loop()
{
  for (motorSpeed = 0 ; motorSpeed <= 255; motorSpeed += 10)
  {
    analogWrite(PWMPin, motorSpeed);
    delay(30);
  }
  for (motorSpeed = 255 ; motorSpeed >= 0; motorSpeed -= 10)
  {
    analogWrite(PWMPin, motorSpeed);
    delay(30);
  }

}
#------------------------------------------------------------------------------------------------#


Direction control of DC motor using H-Bridge:

An H-Bridge is a simple electronic circuit consisting of 4 switches allowing to select the direction of current flowing through a part. H-Bridge consists of four manually controlled switches (BJT or MOSFET). The name is derived from the circuit diagram which looks like a capital H connection consisting of four transistors and a motor in the center.

H-Bridge connection using four transistors and a motor is shown below. By switching two transistors at the same time, we can control the flow of current i.e  “Current Direction”  through the motor and hence the direction of rotation.




H-Bridge Connection Circuit :

The two control signals A and B in the above circuit will determine the direction of rotation of the motor. If signal A is LOW and signal B is HIGH, transistors Q1 and Q4 will be turned on and allow current to flow through the motor in a clockwise  direction.

If the control signal A is made HIGH and signal B is LOW, then transistors Q2 and Q3 will turn on and the flow of current through the motor is reversed and so the direction of the rotation is counter clockwise.

With the help both the features i.e. PWM technique for DC Motor speed control and H-Bridge Circuit for direction control, we can control speed as well as direction of DC Motor.

It is complicated to use transistors for making an simple H-Bridge connection. For this reason, there are dedicated H-Bridge Motor Driver IC available and common of the  IC’s are L293D and L298N.

For this project, we will use L298N Motor Driver and see DC Motor Control using L298N Motor Driver using PWM technique with Arduino.


L298N Motor Driver :

The L298N driver is a dual H-Bridge motor driver which allows speed  control and direction control of two DC motors at the same time. The module can drive DC motors that have voltages between 5 and 35V, with a peak current up to 2A.

L298N module has two terminal blocks for the motor A and motor B, and another screw terminal block for the Ground pin, the VCC for motor and a 5V pin which can either be an input or output.


This depends on the voltage used at the motors VCC. The module have an on board 5V regulator which is either enabled or disabled using a jumper. If the motor supply voltage is up to 12V we can enable the 5V regulator and the 5V pin can be used as output, for example for powering our Arduino board. But if the motor voltage is greater than 12V we must disconnect the jumper because those voltages will cause damage to the on board 5V regulator. In this case the 5V pin will be used as input as we need connect it to a 5V power supply in order the IC to work properly.


We can note here that this IC makes a voltage drop of about 2V. So for example, if we use a 12V power supply, the voltage at motors terminals will be about 10V, which means that we won’t be able to get the maximum speed out of our 12V DC motor.


DC Motor Control using L298N Motor driver and Arduino :

We will see  the speed control and direction control of a DC Motor using Arduino and L298N IC. We need few additional parts for this project and the list of parts is given below.
We will control the speed as wells as direction of DC Motor using PWM Signal and L298N (H-Bridge).


Circuit Diagram:



Components Required :
•           Arduino UNO Board
•           L298N Motor Driver Module
•           12V DC Motor
•           100KΩ Potentiometer
•           Push Button
•           12V Power Supply
•           Breadboard
•           Connecting Wires

Arduino Code :

#------------------------------------------------------------------------------------------------#

int mot1 = 8;
int mot2 = 9;
int en1 = 10;
int dir = 6;
bool state = true;
int nob = A0;
int val=0;

void setup()
{
  pinMode(mot1,OUTPUT);
  pinMode(mot2,OUTPUT);
  pinMode(en1,OUTPUT);
  pinMode(dir,INPUT_PULLUP);
 
}

void loop()
{
 
  val = analogRead(nob);  
 
  analogWrite(en1, val / 4);
 
  if(digitalRead(dir)==LOW)
  {
     state=!state;
     while(dir==LOW);
     delay(300);
  }
  if(state)
  {
    digitalWrite(mot1,HIGH);
    digitalWrite(mot2,LOW);
  }
  else
  {
    digitalWrite(mot1,LOW);
    digitalWrite(mot2,HIGH);
  }

}

#------------------------------------------------------------------------------------------------#