Real Time Speed control of DC motor with
unknown transfer function through PID
Hasnain Irshad Bhatti1 , Hafiz Muhammad Faisal2 , Fatima Abbas3 and Eman Ahmed4
Department of Electrical Engineering, University of Engineering and Technology, Lahore
Email address:
[email protected] ,
[email protected] ,
[email protected] ,
[email protected] Abstract—Speed control of DC motor with known transfer A. Open loop Control System
function is not an arduous task but it becomes a challenging
situation when you dont have enough information about your DC One type of control system in which the output has no
machine especially when you have to deal with the cheap DC mo- influence on the control action of the input signal is known
tors used in simple robot cars. This paper not only proposes a way as an Open-loop system. An open-loop system is defined by
to control the speed through PID controller without knowing the the fact that the output signal or condition is neither measured
transfer function but also shows the real time variations in speed nor fed back for comparison with the input signal. They are
by changing the speed through Bluetooth mobile application for
better understanding. Moreover it compares different types of easier to construct and hence are economical and stable due to
control systems through calculations and proper simulations. their simplicity. Since these systems do not have a feedback
Index Terms—PID Controller, speed control, PWM, desired mechanism as shown in Figure 1, they are very inaccurate in
speed, PID tuning, transfer function, counter, timer. terms of result and hence unreliable too. Moreover due to the
absence of a feedback mechanism, they are unable to remove
I. I NTRODUCTION the disturbances occurring from external sources. To overcome
this limitation we use closed loop control systems.
The first requirement of any system which we design is
Stability. And Control system can help us to achieve that.
The purpose of this process is to reduce men’s involvement
thereby, reducing the error and increasing the efficiency of that
particular system. DC motor speed control is one of the most
useful applications of control system. By controlling the speed
of the motor, you can vary the speed of the motor according Fig. 1. Open loop System
to the required operation. The basic principle of the DC motor
is to convert DC energy into mechanical energy. It has wide
applications in industries, though its maintenance costs are B. Closed loop Control System
higher than the induction motor, due to its primary advantages A Closed-loop Control System, also known as a feedback
i.e speed variation and constant torque development. Moreover control system is a control system which uses the concept of
they are also used in toy cars and small scale robots. an open loop system as its forward path but has one or more
Control systems can either be open loop systems or closed feedback loops or paths between its output and input as shown
loop systems. Speed of a DC motor can be controlled ef- in Figure 2. They are more accurate than open loop system
ficiently through closed loop with PID. PID controllers are due to their complex construction. Since they are composed
found in a wide range of applications for industrial process of a feedback mechanism, so they clear out the errors between
control. Nearly 95 percent of closed loop operations use PID input and output signals, and hence remain unaffected to the
controllers. external noise sources. But being relatively more complex
If we know the transfer function or the datasheet of our dc in construction, adds up to the cost making it costlier than
motor it will be very easy for us to tune the PID. There are open loop system. Since it consists of feedback loop, it may
softwares available like MATLAB and LABVIEW on which create oscillatory response of the system and it also reduces
we can analyze our transfer function thus plotting the bode the overall gain of the system. For this purpose, we use PID
plots or pole zero maps. There we can easily tune our PID Controller with closed loop system. [1]
parameters Kp (proportional gain), Ki (Integral gain) and Kd
(derivative gain).
So our area of research is to control the speed of DC motor
with PID controller using STM32F401VCTx microcontroller
when the transfer function of DC motor is unknown
II. CONTROL SYSTEMS
There are two types of control system:
1. Open loop Control System Fig. 2. Closed loop System
2. Closed loop Control System
III. PID (P ROPORTIONAL , I NTEGRAL AND D ERIVATIVE 3. Zeigler-Nichols table
C ONTROLLER ) Here we are using the first method i.e Trial and Error
A proportional-integral-derivative controller (PID con- Method to calculate the Kp, Ki, Kd values as the transfer
troller) is a control loop feedback mechanism widely used function is unknown. In this method, first we have to set
in industrial control systems. In a PID Algorithm the current Ki and Kd values to zero and increase Kp until system
speed of motor is measured and compared with our desired reaches to oscillating behavior. Once it is oscillating, adjust
speed. Then by calculating the error and performing calcula- Ki so that oscillations stops and finally adjust Kd to get
tions it restores the actual speed to the desired speed in the fast response. PID is easier to implement and gives a better
optimum way i.e here the speed is continuously checked and response to unmeasured disturbances. Once its designed for a
controlled.PID uses three basic control behaviors as shown in given system, it does not require further tuning. Moreover it
Figure 3. is not heavy on hardware.
The effect of PID controller on the response of the system
is shown in Table I.
TABLE I
E FFECT OF PID ON S YSTEM
PID Tr Maximum Ts Steady state
controllers (sec) Overshoot (sec) error
Increase Kp descrease increase small change decrease
Increase Ki decrease increase increase eliminate
Increase Kd small change decrease decrease small change
IV. SYSTEM ELEMENTS
• Microcontroller STM32F401VCTx
Fig. 3. PID • Optocoupler 4N35
• Motor Driver L298N
• IR Speed Sensor
A. Kp
• Bluetooth hc05
Proportional controller gives output which is proportional • DC Motor
to current error. It compares desired point with actual value.
Speed of the response is increased when the proportional A. Microcontroller STM32F401VCTx
constant Kp increases. It reduces the rise time, increases the The STM32F401 microcontrollers are part of the STM32
overshoot, and reduces the steady-state error. Dynamic Efficiency device range. These devices offer the best
B. Ki balance of dynamic power consumption (in run mode) and
processing performance.
I-controller provides necessary action to eliminate the
steady state error. It integrates the error over a period of time B. Optocoupler 4N35
until error value reaches to zero. It limits the speed of response
It has a LED that produces infra-red light and it shorts
and affects stability of the system. Speed of the response is
the collector and emitter of its transistor, when given a high
increased by decreasing integral gain Ki. It decreases the rise
pulse at the input terminals. Basically it is used for electrical
time while increases both overshoot and settling time.
isolation of two circuits. Pin configuration of Optocoupler is
C. Kd shown in Figure 4.
Its output depends on rate of change of error with respect
to time, multiplied by derivative constant. It improves the
stability of system by compensating phase lag caused by I-
controller. Increasing the derivative gain increases speed of
response. Kd has no effect on rise time or steady state error
but it increases settling time and decreases overshoot of the
system.
D. Tuning of PID:
There are three different types of tuning methods which are
developed to tune the PID controllers which are as follow:
1. Trial and Error Method Fig. 4. Pin Configuration
2. Process reaction curve
C. Motor Driver L298N signal will be. For example if the signal is on for half of the
The L298N is a dual H-Bridge motor driver which allows total period, the duty cycle is 50 percent. Duty cycle can be
speed and direction control of two DC motors at the same calculated as:
time. The module can drive DC motors that have voltages D = (P W/T ) ∗ 100 (1)
between 5 and 35V, with a peak current up to 2A. Where D is the duty cycle, PW is the pulse width (pulse active
D. IR Speed Sensor time), and T is the total period of the signal. The reason why
we used the PWM to control the speed is due to its greater
This IR Speed Sensor be used to count RPM or detect
efficiency and less heat dissipation.
objects passing through it. It is used with a disc encoder to
However the STM is electrically isolated with the remaining
make it a RPM counter. There is an on board LED that will
circuit using Optocoupler 4N35.This is for the safety purpose
show the trigger status of the sensor. When sensor is blocked,
of the STM as sometimes due to inductive effect there is back
digital output will become high and LED will turn off and
rush current which can pass through ground to STM and can
when sensor is not blocked, digital output will be low and
burn it. The PWM signal is applied at the input terminals of
LED will turn on.
Optocoupler through a resistor of a particular value to limit the
E. Bluetooth HC05 input current going to LED. At the output we are using a pull
The HC-05 is a module which can add two-way (full- down resistor so that when the terminals are short, the voltage
duplex) wireless functionality to projects. This module is used develops across it and the pin attached to upper terminal of
to communicate between two microcontrollers like Arduino or resistor gets a HIGH state. In this way the grounds of both
communicate with any device with Bluetooth functionality like half circuits are separated.
a Phone or Laptop. The output of the Optocoupler is then connected to the
L298N motor driver. As we know that STM output pin gives
F. DC Motor 3.3 volts maximum but our motor needs atleast 5 volts to
A small DC motor is used which has wider applications all run so we use motor driver. It will take signal from the
over the world ranging from small toys to large industries. It microcontroller and will run the motor as commanded. It is
converts DC energy into mechanical energy. given external source of 12 volts.
The last part of circuit is the sensor which will give us the
V. SYSTEM IMPLEMENTATION speed of our motor. When a solid body passes through the
In this section the hardware and software implementation u shaped slit the sensor goes to low state. Normally it is at
will be discussed. High state. Using these states we can find the speed by the
following formula:
A. HARDWARE IMPLEMENTATION
Speed = (counter/rotations) ∗ 60rpm (2)
Following the circuit diagram as shown in Figure 5 and
using the above described system elements, the circuit was Moreover for getting real time variations in speed we used
implemented a HC05 Bluetooth module which is connected with pins PA9
and PA10 of the microcontroller and it is controlled by an
android application which is designed in MIT App Inventor.
For the calculation of counter and rotations we will focus
on our software part.
B. SOFTWARE IMPLEMENTATION
The code has been implemented on Keil uVision5 using
STM32CubeMX. This section is further divided into parts to
depict a clear idea of working of the algorithm.
PWM:
Fig. 5. Circuit Diagram PWM is generated using a single timer on STM pin PE9
with reload value of 1000 and prescaler of 16 with internal
First of all we will discuss PWM. We are providing the clock frequency of 16MHz.
motor with PWM using STM32F401VCTx PIN number PE9. Speed Measurement:
Pulse width modulation (PWM) is a method to control the The major part is to measure the speed of the motor, for
amount of power delivered to a load without dissipating any which we had given the output of the sensor to the timer
wasted power. Its literal mean is to vary the width of pulse. (with INPUT CAPTURE MODE) generated at pin PA0 of the
PWM does this by changing the pulse width, which in turn, microcontroller, so that it can measure the number of rotations
changes the duty Cycle. Duty cycle is the percentage or the per second. For this purpose the timer reload value is set to
fraction during which the input signal is on in an interval. 100k and prescaler is set to 16k with internal clock frequency
So the larger the duty cycle percentage, the larger the output of 16MHz and it has interrupt activated.
The speed can be measured by considering just a single slit Kd then reduces overshoot and improves the overall transient
or by 20 slits. As all the 20 slits approximately give same data, response of the system and makes the system more stable and
so what we have done here is that we have calculated the time steady. [2]
for five consecutive slits to make our system fast, we could Bluetooth module was configured at 9600 baud rate. The
have use a single slit but that will produce more chattering in data to be received was stored in a buffer named Rx-data of
the wave as all the slits sizes are not the exactly same so to character data type. The data sent using android application
avoid chattering, we calculated the time for five consecutive was arranged according to its name and value. By using ”if
slits. else clauses”, name of the data receiving was compared and
Implementation of PID Controller: corresponding values of PWM in case of ”PWM controlled
Closed loop implementation for DC motor speed control is operation” and desired value in case of ”Speed control oper-
shown in Figure 6. ation ” was set.
VI. RESULTS AND DISCUSSION
Before moving on to the results and discussion first take a
look on the parameters to observe for a better understanding.
Parameters to Observe:
• Rise Time: Time it takes for the response to rise from 10
to 90 percent of the steady-state response.
• Delay Time: The delay time is the time required for the
response to reach half the final value the very first time.
• Peak Time: Time at which the first peak value occurs.
• Maximum Overshoot: It is the maximum peak value of
the response curve measured from unity.
• Settling Time: The time required for the response curve
to reach and stay within a range about the final value
of size specified by absolute percent of the final value
(usually 2 or 5 percent).
• Steady State Error: Steady-state error is defined as the
difference between the input (command) and the output
of a system in the limit as time goes to infinity. [3]
Now moving to results and discussion:
Fig. 6. Implementation of PID
A. Open Loop Response
As seen from the figure 6, PID controller is fed with data
The result of implementing open loop system for the speed
as the error signal which is calculated by the difference in the
control of the motor is shown in figure 7.
desired speed and actual speed. PID controller then calculates
the output which is directly mapped to PWM which is fed
to motor as input. A saturation filter saturates the value of
output calculated by the PID to the maximum and minimum
values of PWM possible. Rpm sensor generates the interrupts
according to its position and a parallel running timer captures
corresponding time to calculate the actual speed of the motor.
This actual speed is given to the system as the feedback with
unity gain.
The motor that we are using is a cheap DC motor installed in
toy cars and small scale robots. Controlling this type of motor
is a very arduous since its transfer function and parameters
are unknown. So the values of PID parameters cannot be Fig. 7. Output speed response of DC Motor (open loop system)
calculated easily. To address this problem we arbitrarily select
the PID parameters and change the values to observe the re- It is obvious from the figure 7 that when the PWM according
sponse of the system. Since Kp, Ki , Kd values simultaneously to the datasheet is given to the Motor as input, the desired
effect the response and help each other eliminate all the errors output is not achieved.
together. These values are set by observing the effect of these As derived from figure , the parameters of this control
parameters alone. Kp helps reducing rise time but steady state system are given in table II.
error doesnt get affected. Here, Ki does its job and reduces It is clearly shown from the table that motor never ap-
this steady state error compromising the stability of the system. proaches its desired value. Instead, it settles at a different speed
TABLE II controller is also intended to make the response faster
PARAMETERS OF THE DC M OTOR ( OPEN LOOP SYSTEM ) by decreases the parameters of the system i.e. rise time,
Parameter Unit Value settling time etc. with minimum error possible.
Delay Time msec 27 As derived from figure 9, the parameters of the control
Settling Time msec 400 system are given in table III.
Time Constant msec 34
Steady State error percent 20
TABLE III
PARAMETERS OF THE MOTOR SPEED CONTROL SYSTEM
B. Closed Loop Response Parameter Unit Value
Rise Time msec 299.56
• Without PID: Overshoot Time percent 4
The result of implementing closed loop system with gain Settling Time msec 594.03
of 10 is shown in figure 8. Peak Time msec 367
Delay Time msec 93.612
Steady State error percent 2
It is clearly shown from the table that motor settles at
its desired speed; that is 400 in this case, with 2 percent
steady error in just 594 milliseconds. By adjusting the
PID values the system response can be changed according
to our requirement. Here, the PID values are adjusted to
make the motor reach its desired speed with maximum
stability.
VII. CONCLUSION
This paper describes a practical speed control system for a
Fig. 8. Output speed response with Closed loop gain 10
dc motor using PID whose transfer function is unknown but
the experimental results obtained are quite good. In open loop
It is obvious from the figure 8 that speed control of the
system the desired speed of dc motor is not achieved because
motor with the closed loop gain = 10 leads to continuous
we dont have any feedback. In closed loop dc motor system
bouncing error about mean position.
the feedback is applied but the response of the system is very
Thus controlling motor speed with just closed loop is not
unstable due to errors. But by applying PID to the closed loop
enough.
dc motor system the motor achieved the desired speed in an
• With PID:
efficient manner and the response of system is quite good.
The result of implementing closed loop system for the
PID reduces the errors of closed loop system and we get the
speed control of the motor with PID implemented is
response faster than the system without PID.
shown in figure 9.
VIII. FUTURE WORK
With the rapid development of control system, the realiza-
tion of PID control systems also became much easier than
before. The notable future research is to develop the tuning
rules for PID based dc motor closed loop system whose
Transfer Function is unknown.
ACKNOWLEDGMENT
The authors are most grateful to their teacher for introducing
them to the field of control systems and sharing their pearls
of wisdom during the course of this research. We thank our
colleagues who provided insight and expertise that greatly
Fig. 9. Output speed response of Motor with PID implemented assisted the research.
We are grateful to our Department of Electrical Engineering
It is obvious from the Figure 9 that when the desired value for helping us to complete our research and collect the results.
of speed is changed from 200rpm to 400rpm, the error
signal is changed. PID controller then using this error R EFERENCES
signal generates the output which is meant to settle the
[1] Bayt (2018) ”What are the advantages and disadvantages of an open loop
motor at 400rpm. Thus, motor speed changes dynamically control system?” [Online] Available: https://www.bayt.com [Accessed
following the change in the desired rpm value. PID 25 Nov. 2018].
[2] Loucif, Fatiha. ”DC MOTOR SPEED CONTROL USING PID CON-
TROLLER,” in Conf. ICCAS2005 June 2-5, KINTEX, Gyeonggi-Do,
Korea [Online]. Available: Research Gate, http://www.researchgate.net.
[Accessed: 18 Nov. 2018].
[3] Ogata, K. (2010). ”Modern control engineering” , 5th ed. New Delhi:
PHI Learning Private Limited.