By Kong Wai Weng

RH2T Mag, Vol.4, Mar 2010

**PID control system is one of the most mature and commonly used control strategies in the industrial for decades thanks to its simple but effective algorithm. In this article, we will discuss the basic concept of PID controller and how to implement it in the embedded system.**

**Introduction**

Closed loop control system is an essential topic for embedded systems, bringing together actuators and sensors with the control algorithm in software. For example, motor speed can be controlled by varying the PWM duty cycle used to drive the motor. This is the open-loop control. However, due to the lack of feedback, the actual motor speed could not be verified. This is important because driving the motor with the same PWM duty cycle does not guarantee the motor to run at the same speed under all circumstances. A motor will run faster in load free condition than under load with the same PWM signal. In order to control the motor speed, we need to close the loop by adding feedback of the actual motor speed to the controller.

Besides speed control, PID control system can also be used to control other parameters such as position, temperature, water level, stability, *etc*. In this article, we will discuss how to implement a PID controller for position control based on PR24.

**The Problem – DC Motor Position Control.**

Before we begin to design a PID controller, we need to understand the problem. In this example, we want to move the shaft of the motor from its current position to the target position.

*We want to move the output shaft of the motor from current position to target position*

There are a few terms commonly used to describe the PID control loops, such as:

*Control Variable (CV)*– This is the output of the control loop. In this case, the CV is the duty cycle of the PWM signal that drives the motor.*Process Variable (PV)*– This is the feedback value returned by the system to the controller. In this example, the PV is the current angle of the motor shaft.*Set Point (SP)*– Set point is the value that we desire for the system. In our case, the SP is the target position of the motor shaft in angle.*Error (E)*– Error refers to the difference between the set point and the process variable. In another words, it means how far the current position of the motor shaft from the target position.

**The Hardware – PR24**

*Note: PR24 is already discontinued. Sample code can be download here.*

The Cytron’s DIY Project Set PR24 (PID Motor Controller) is the best platform for beginner to learn the PID algorithm. It has the following features:

- PIC16F876A as the main controller.
- Geared DC motor as the output.
- Multi-turn variable resistor connected to the motor shaft for position feedback.
- 2×16 Character LCD for tuning and troubleshooting.
- Presets for PID tuning.

*DIY Project Set PR24 – PID Motor Controller*

The sample source code for the PR24 (PID Motor Controller) can be downloaded from ~~Cytron’s website under the PR24 product page ~~(Github CytronTechnologies).

**The Implementation of PID Controller**

The PID controller, just like its name, comprises a proportional (P), an integral (I) and a derivative (D) part. The controller parts are introduced in the following sections individually and in combined operation.

**Proportional Controller**

When the current position of the motor shaft is still far away from the target position, we want to apply more power to drive the motor towards the target position so that we can reach there faster. When the shaft is getting nearer to the target position, we will reduce the power to slow it down. At the time the shaft reaches the target position, the motor needs to be stopped. If the shaft position has overshot, we need to apply negative power to the motor (reverse the motor) to bring it back to the target position.

In short, this is called proportional controller because the power we apply to the motor is proportional to the error of the system.

*The block diagram of proportional controller*

From the block diagram of proportional controller, we can see that the PWM duty cycle (output) is the result of multiplying the error with a constant, Kp.

*Error = Set Point – Process Variable*

*Control Variable = Kp * Error*

Figure below shows the example of proportional loop implemented in C language.

*Implementation of proportional loop in C language*

The value of Kp needs to be chosen carefully in order to get the optimum system response. Lower values for Kp will tend to give smoother but slower responses.

*System response for proportional controller with low Kp*

Higher values of Kp will yield much quicker response but may cause overshoot, where the output oscillates before settling.

*System response for proportional controller with high Kp*

Excessively high values of Kp may even throw the loop into an unstable state where the output oscillates without ever settling at the set point.

*System response for proportional controller with excessively high Kp*

**Integral Controller**

As can be seen from the graph of P controller, the actual position of the motor shaft, when settles down will not reach the target position. This is because when the current position is near to the target position, the error becomes very small and the computed PWM duty cycle is too small for the motor to overcome the friction and gravity. The small error that exists when the system has settled down is called the steady state error.

*Steady state error due to the friction and gravity*

To overcome the problem of steady state error for the P controller, I controller is being introduced. As its name suggests, the integral is merely an accumulated error signals encountered since startup.

*Integral = ∑(Error)*

This total is multiplied by a constant, Ki, and is added into the loop output. Unlike the P controller, the I controller is rarely used alone, but mostly in combination with the P or PD controller. When the system has already settled down with a small steady state error, the integral still continues to accumulate until the CV is large enough to bring the PV inline with SP. The equations for PI controller are as follow:

*Error = Set Point – Process Variable*

*Integral = Integral + Error*

*Control Variable = (Kp * Error) + (Ki * Integral)*

Figure below shows the example of PI controller implemented in C language.

*Implementation of PI loop in C language*

Just like the P controller, the value of Ki needs to be chosen carefully. Too low the value, the steady state error is corrected very slowly; too high the value, the system becomes unstable and oscillates.

*System response for PI controller with no steady state error*

Because the integral can grow quite large when the set point cannot be reached, some applications stop accumulating the error when the CV is saturated.

**Derivative Controller**

Let’s say you are driving a car, and you need to stop your car exactly 100m from your current position as soon as possible. If you are travelling at 10km/h, you would want to accelerate your car so that you can reach the target sooner. In contrast, if you are cruising at 100km/h, you need to start braking so that you can stop at 100m and will not overshoot. This is where the derivative controller comes into play.

The derivative of any variable describes how that variable changes over time. In a PID controller, the derivative is the rate of change of the error. In digital form, it can be described as:

*Derivative = Error – Last Error*

where Error is the current error value and Last Error is the error value for the previous iteration.

Negative values of derivative indicate an improvement (reduction) in the error signal. For example, if the last error was 20 and the current error is 10, the derivative will be -10. When these negative values are multiplied with a constant, Kd, and are added to the output of the loop, it can slow down the system when approaching the target.

Just like the I controller, the D controller is rarely used alone, but mostly in combination with the P or PI controller. The equations for the PD controller are as follow:

*Last Error = Error*

*Error = Set Point – Process Variable*

*Derivative = Error – Last Error*

*Control Variable = (Kp * Error) + (Kd * Derivative)*

Figure below shows the example of PD controller implemented in C language.

*Implementation of PD loop in C language*

The damping effect of the D controller allows the system to have a higher value of Kp and/or Ki without overshooting. In consequent, this will give the system a better response time to set point changes. However, too high the value of Kd will also have negative effect. The D controller tense to amplify the noise exists in the feedback loop. If the Kd is too high, the system will become jerky if the feedback loop is noisy.

*System response for PD controller*

**Joining Them Together – PID Controller**

By joining the P, I and D controller, we can take the advantages of the combined benefits from each controller. We have the P controller for fast system response, I controller to correct the steady state error and D controller to dampen the system and reduce overshoot.

*The block diagram of PID controller*

From the block diagram of PID controller, we can see that the output of the loop is merely the sum of output from P, I and D controller. The equations for the PID loop are illustrated below:

*Last Error = Error*

*Error = Set Point – Process Variable*

*Integral = Integral + Error*

*Derivative = Error – Last Error*

*Control Variable = (Kp * Error) + (Ki * Integral) + (Kd * Derivative)*

Figure below shows the example of PID controller implemented in C language.

*Implementation of PID loop in C language*

*System response for the correctly tuned PID controller*

**Summary**

PID controller is a simple yet effective control system widely used in industrial. However, to implement the PID controller is simple, but not the tuning. The process of tuning the PID parameters (Kp, Ki and Kd) is a continuous trial and error process. There is no exact way to calculate the value for the parameters unless the whole system is mathematically modeled and simulated. Experience is an important factor to get the optimum PID parameters based on the observation of the system behavior during the tuning process. If you have inquiry, feel free to discuss in our technical forum.

**References:**

Dennis Clark and Michael Owings: Building Robot Drive Trains.

Thomas Braunl: Embedded Robotics – Mobile Robot Design and Applications with Embedded Systems.

http://en.wikipedia.org/wiki/PID_controller

http://www.expertune.com/tutor.html

nice tutorial . is there any tutorial for implementing the fuzzy logic in microcontroller for motor control

can u tell me how much we will generate in relate to get a fixed or set point frequency

PLEASE SEND PID BASICS IN MY EMAIL

What a great write-up. I’ve been looking into PIDs for quite a while and this is the best write-up I have found.

Great tutorial! Simple and efficient.

Simple and effective explanation.. thanks for sharing this

Good one..Thanks for simple and clear write up on PID

Clear and Crisp Write up PID controller… Thanks much…

This is an amazingly clear and concise explanation of a PID controller. Your teaching skills are excellent! Now, can you explain how a PIV controller is done, and/or how it differs?