In this Arduino project having main components are Arduino kit, DC motor L298N Motor Drive IC. Here we have different ways how to control a DC Motor and also this is quite popular for many reasons which are Arduino DC Motor control using L298N.
A
DC motor is the simplest motor that beginners and hobbyists know. It is very easy to use: connect the two motor cables to the two terminals of a battery and that's it! Your motor starts spinning.
If you want to control the rotational speed of a simple
DC motor, there is a technique called
PWM DC motor control. The pulse width modulation or
PWM signal generated by this technique allows us to control the average voltage delivered to the
DC motor.
Now, the average voltage applied to the DC motor depends on what is known as the work cycle of the PWM signal. The working cycle of a PWM signal is nothing more than the ratio of the time the signal is activated or is HIGH to the total duration of the signal, that is, the time signal. The sum of the ON and OFF times. The duty cycle is usually expressed as a percentage, and the following figure shows several PWM signals from a 12V supply with different duty cycles of 0%, 25%, 50%, 75% and 100%, respectively.
The PWM Signal from any source like Arduino in this example, can be given to the gate of a MOSFET and depend on the duty cycle of the PWM Signal, the speed of the DC Motor will vary.
The following image shows a simple circuit diagram, where a PWM output from Arduino is given to a MOSFET and the 12V DC Motor is connected through the MOSFET.
The code is given below for above circuit. Using this code, the Arduino will vary the speed of a DC Motor in a fading fashion i.e. gradually increases the speed to peak and then gradually decreases the speed to halt.
Code for Arduino:
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);
}
}
This circuit is good for controlling the speed of the motor, but not for effectively changing the direction of rotation. To change the direction of rotation without inverting the motor cables every time you need to use a special circuit called H-bridge.
DC Motor Control using H-Bridge
An H-Bridge is a simple electronic circuit consisting of four switching elements like transistors (BJT or MOSFET) that can drive a motor in both the directions without switching the leads.
The name “H-Bridge” refers to the look of the connection consisting of four transistors and a motor in the center forming the letter “H”.
A simple H-Bridge connection using four transistors and a motor is shown below. By activating two particular transistors at the same time, we can control the flow of current through the motor and hence the direction of rotation.
The two control inputs A and B in the previous circuit determine the direction of rotation of the motor. When A is LOW and B is HIGH, transistors Q1 and Q4 are on, allowing current to flow through the motor in a particular direction.
When control input A is set high and B low, transistors Q2 and Q3 are activated and the current flow through the motor is reversed and, therefore, the direction of rotation.
By combining the two functions,
PWM Speed Control and H-Link Direction Control, you can have complete control of a DC motor.
It is problematic to use transistors to make an effective H-bridge connection. For this purpose, there is a special H-bridge motor controller IC available in the market, and the two common integrated circuits are L293D and L298N.
We have already seen how to control the speed of a DC motor with L293D in a previous project. In this project, we will focus on the most advanced L298N motor controller and we will look at the Arduino DC motor control with the L298N motor controller with PWM technology.
L298N Motor Driver:
L298N Motor Driver IC is a 15-lead high voltage, high current Motor Driver IC with two full bridge drivers. The logic levels of L298N IC are compatible with standard TTL and IC can be used to drive different inductive loads like DC Motors, Stepper Motors, Relay, etc.
Since the
L298N Motor Driver IC is a dual full bridge driver IC, you can control two motors at the same time with individual inputs. The logic supply voltage is 5V but the motor supply voltage can be as high as 45V. The peak output current per channel is 2A.
Arduino DC Motor Control using L298N:
Components Required
- Arduino UNO
- L298N Motor Driver Module
- 12V DC Motor
- 100KΩ Potentiometer
- Push Button
- 12V Power Supply
- Breadboard
- Connecting Wires
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);
}
}