7-DC Motor Control (Open Loop)
7-DC Motor Control (Open Loop)
________________________________________________________________________
Experiment # 07 Page 1 of 9
Control Systems Lab
_________________________________________________________________________
Marks Obtained
Group Member Group Member Group Member
1 2 3
NAME
REGISTRATION NUMBER
LAB REPORT
PERFORMANCE
TOTAL MARKS
DEADLINE:
DATE OF SUBMISSION:
Introduction
________________________________________________________________________
Experiment # 07 Page 2 of 9
Control Systems Lab
_________________________________________________________________________
In this Arduino Tutorial we will learn how to control DC motors using Arduino. We
well take a look at some basic techniques for controlling DC motors using the L298N
motor driver and the Arduino board.
We can control the speed of the DC motor by simply controlling the input voltage to
the motor and the most common method of doing that is by using PWM signal.
So depending on the size of the motor, we can simply connect an Arduino PWM
output to the base of transistor or the gate of a MOSFET and control the speed of the
motor by controlling the PWM output. The low power Arduino PWM signal switches
on and off the gate at the MOSFET through which the high power motor is driven.
________________________________________________________________________
Experiment # 07 Page 3 of 9
Control Systems Lab
_________________________________________________________________________
On the other hand, for controlling the rotation direction, we just need to inverse the
direction of the current flow through the motor, and the most common method of
doing that is by using an H-Bridge. An H-Bridge circuit contains four switching
elements, transistors or MOSFETs, with the motor at the center forming an H-like
configuration. By activating two particular switches at the same time we can change
the direction of the current flow, thus change the rotation direction of the motor.
So if we combine these two methods, the PWM and the H-Bridge, we can have a
complete control over the DC motor. There are many DC motor drivers that have
these features and the L298N is one of them.
L298N Driver
________________________________________________________________________
Experiment # 07 Page 4 of 9
Control Systems Lab
_________________________________________________________________________
The L298N is a dual H-Bridge motor driver which allows speed 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.
Let’s take a closer look at the pinout of L298N module and explain how it works. The
module has two screw terminal blocks for the motor A and 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 onboard
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
________________________________________________________________________
Experiment # 07 Page 5 of 9
Control Systems Lab
_________________________________________________________________________
damage to the onboard 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.
Next are the logic control inputs. The Enable A and Enable B pins are used for
enabling and controlling the speed of the motor. If a jumper is present on this pin,
the motor will be enabled and work at maximum speed, and if we remove the
jumper we can connect a PWM input to this pin and in that way control the speed of
the motor. If we connect this pin to a Ground the motor will be disabled.
Next, the Input 1 and Input 2 pins are used for controlling the rotation direction of
the motor A, and the inputs 3 and 4 for the motor B. Using these pins we actually
control the switches of the H-Bridge inside the L298N IC. If input 1 is LOW and input
2 is HIGH the motor will move forward, and vice versa, if input 1 is HIGH and input 2
is LOW the motor will move backward. In case both inputs are same, either LOW or
HIGH the motor will stop. The same applies for the inputs 3 and 4 and the motor B.
________________________________________________________________________
Experiment # 07 Page 6 of 9
Control Systems Lab
_________________________________________________________________________
Now let’s make some practical applications. We will control the speed of the motor
using a potentiometer and change the rotation direction using a push button. Here’s
the circuit schematics.
________________________________________________________________________
Experiment # 07 Page 7 of 9
Control Systems Lab
_________________________________________________________________________
Arduino Code
#define enA 9
#define in1 6
#define in2 7
#define button 4
int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void loop() {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value
from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
________________________________________________________________________
Experiment # 07 Page 8 of 9
Control Systems Lab
_________________________________________________________________________
Description: So first we need to define the pins and some variables needed for the
program. In the setup section we need to set the pin modes and the initial rotation
direction of the motor. In the loop section we start by reading the potentiometer
value and then map the value that we get from it which is from 0 to 1023, to a value
from 0 to 255 for the PWM signal, or that’s 0 to 100% duty cycle of the PWM signal.
Then using the analogWrite() function we send the PWM signal to the Enable pin of
the L298N board, which actually drives the motor.
Next, we check whether we have pressed the button, and if that’s true, we will
change the rotation direction of the motor by setting the Input 1 and Input 2 states
inversely. The push button will work as toggle button and each time we press it, it
will change the rotation direction of the motor.
________________________________________________________________________
Experiment # 07 Page 9 of 9
Control Systems Lab