You are on page 1of 7

PID speed control of DC motor using

microcontroller
http://www.help2educate.com/pid-speed-control-of-dc-motor-using-microcontroller/
Syed Noman ud din Microcontrollers 4 Comments 966 Views
In this article we will look at the implementation of PID speed control of DC motor using
microcontroller. The design procedure, the schematic, code and simulation of PID speed
control of DC motor using microcontroller will also be highlighted.
Contents [hide]

1 Brief introduction to PID controller


2 PID Speed control of DC motor using microcontroller

3 Components and Schematic

4 Simulation results

5 Code

Brief introduction to PID controller


PID stands for Proportional integral derivative. It (PID control) is a method of feedback
control that uses the PID controller as the main tool. The basic structure of conventional
feedback control systems is shown in the following figure using a block diagram
representation.

In this figure, the process (plant) is the object to be controlled. The purpose of PID controller
is that the desired output (y) is obtained according to the predetermined set values of the input
(r). To achieve this goal, the error signal is calculated from closed loop feedback and input.
The controller generates the manipulated signal which is given as input to the plant (process).
As an example, consider a heating tank in which some liquid is heated to a desired
temperature by firing natural gas. The process variable y is the temperature of the liquid. The
input r is set point (temperature) and the difference between input (r) and feedback (y) is
error.
e=ry

The manipulated variable u is the flow of the fuel gas. The disturbance is any factor, other
than the manipulated variable, that influences the process variable. For simplicity we ignore
any disturbance in this case.
The compensator C(s) is the computational rule that determines the manipulated variable u
based on its input data, which is the error e in the case of figure shown above. It must be
noted that the process variable y is assumed to be measured by the detector with sufficient
accuracy such that feedback to the system can be regarded as equal to y. The output sensing
device is not shown in the figure.

Three variables through which the error is manipulated to generate control signal (u) are
proportional gain (kp), integral gain (Ki) and differential gain (Kd). The control signal (U)
to the plant is equal to the proportional gain (Kp) times the magnitude of the error plus the
integral gain (Ki ) times the integral of the error plus the derivative gain (Kd ) times the
derivative of the error.

PID Speed control of DC motor using microcontroller


Speed of DC motor can be controlled by varying the applied voltage. However using this
method, output will follow the inputsharply. The transition will neither be smooth not will be
in user control. For any abrupt changes in the input, the response will also be sharp which is
undesired in many cases. So make the output response in user control, stable and smooth, PID
controller is used. Special PID controller as well as Controllers like Microcontroller, Arduino
etc can also be employed for this purpose. This article discusses the PID speed control of DC
motor using microcontroller.
To understand this method, it is very important to have clear concept of speed control of DC
motors. Refer to my article for details on speed control in DC shunt motors
To understand how to control DC motor with microcontroller (with out PID) please refer to
my article speed and direction control of DC motor using microcontroller.
In the present case, the output is the speed of motor which will be given as a feedback to the
controller. The speed will be converted to the voltage to compare with input. The input in this
case is voltage at RA1 from the potentiometer which is used to set the speed of DC motor. So
the error is calculated as
e=r (input voltage at RA1)-y (feedback voltage at RA2 depends upon speed)
The manipulated signal in this case is PWM whose duty cycle is varied by the controller
based on the error to achieve the desired speed. The duty cycle of the PWM signal which is
the controlling signal is determined by using three important variables Kp, Ki and Kd.
Kp = It system response time (present value)
Ki = It controls overshoot, disturbance rejection (Past value)

Kd= steady state error (predicts future value)


To calculate these three gains and PWM width for control, following equations should be
considered.

Components and Schematic


Following components are required for the implementation of PID speed control of DC motor
using microcontroller:
1. PIC-18F452
2. L298
3. Motor-encoder
4. LM2907-8
5. Resistor
6. Capacitor
7. Voltage source
Proteus Schematic

The discrete PID controller is being implemented using PIC-18F452. The driver utilized for
controlling the motor id dual bridge IC 298. The purpose of using dual bridge IC 298 is that
motor cannot be controlled directly from microcontroller. Motor needs high current as well as
it produces back emf due to which it can damage the controller.
To generate feedback, motor encoder is used which generates PWM (pulses) of variable
frequency based on speed of dc motor. The output of the motor encoder is being fed to the
LM2907-8 which translates frequency to voltage. The capacitor is being utilized to remove
the dc component in the encoder output because LM2907-8 require the input in the range -2.5
to +2.5. The LM2907 converts the frequency of the encoder output to voltage which is being
fed to the Analog Channel 0 RA0 of the microcontroller. Analogue input voltage is given to
RA1 (analog channel) input which provides the set-point for the speed of the motor. The
potentiometer provide the value for the set-point. The two voltages are compared to find the
error voltage. Using the error voltage, the PID implanted in microcontroller determines the
control signal which is the PWM waves with certain duty cycle.

Simulation results
The circuit is simulated in Proteus the results are shown in the following figure. Initially the
potentiometer settings is at zero resistance applying full voltage to provide set point for the
speed of DC motor. The width of PWM waves will show the PID response.
PWM provided by the controller when the potentiometer is at the 100%:

Now the potentiometer setting is at 92% to provide new set point for the speed of DC motor.
The PWM output is shown. It can be seen that the width (duty cycle) is increased.

Code
The code is written in C language and complied in MIKRO C compiler
double drive=0;
double Duty=0;
double error=0;
double i_error=0;
double error_old=0;
double e_dot=0;

//integral error
//keeping the track of previous error
//derivative error

double pid_loop(double kp, double ki, double


kd, double setpoint, double Input)
{
error=setpoint-Input;
if (error<0)
{ error=0-error; }
//delay_ms(3000);
if ( error > 255)
{ Duty=255; }
else
{ i_error+=error; }
if (i_error>255)
{Duty=255;}
else if (i_error<0)
{Duty=0;}

//Calculating the error


//Checking the direction of the id controller

// Calculating the integral part of the PID


//anti integral windup

// Calculating the derivative part of the PID


future error
drive= kp*error + ki*i_error kd*e_dot; //PID equation
if (drive > 255 )
{ Duty=255; }
else
{ Duty=drive; }
error_old=error;
//Keeping the track of the error
return drive;
}
//Returning the value for Duty-cycle
void main()
{ double setpoint=0;
double Input=0;
double Kp=1.0;
//Proportional Gain
double Ki=0.0;
//Integral Gain
double Kd=0.0;
//Derivative Gain
double duty_cycle=0;
ADCON0 = 0xC1;
//Setting up the ADC of PIC MCU
ADCON1 = 0x04;
PORTA = 0xFF;
TRISA = 0x03;
// RA0 i RA1 are analog inputs
PWM1_Init(1000);
//Initializing the PWM1 module at 1kHz
ADC_Init();
while(1)
{
setpoint=ADC_Read(1);
//Value for the Setpoint
//Value of the feedback from V-to-F
Input= ADC_Read(0);
converter lm2907-8
duty_cycle=pid_loop(Kp,Ki,Kd,setpoint,Input); //Value for the Duty_cycle
PWM1_Set_Duty(duty_cycle);
//Setting up the Duty cycle
PWM1_Start();
//Starting the PWM
e_dot=error-error_old;

} }
If you have any questions please ask in the comments. If you like it please share it with your
friends.

You might also like