top of page





PID controller, proportional–integral–derivative controller, is a control loop feedback mechanism widely used in industrial control systems and a variety of other applications requiring continuously modulated control. A PID controller continuously calculates an error value as the difference between a desired setpoint and a measured process variable and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively) which give the controller its name. When working with Nova, we will be using PID controllers in our sketch in order to achieve smooth movements in a variety of different applications.

The basic idea behind a PID controller is to read a sensor, then compute the desired actuator output by calculating proportional, integral, and derivative responses and summing those three components to compute the output. Before we start to define the parameters of a PID controller, we shall see a simple example explaining what a closed loop system is and some of the terminologies associated with it.

First of all, the setpoint (SP) is the value that we want the process to be. For example, the temperature control system in a house may have a SP of 22°C. This means that the heating and cooling process in this house is expected to achieve a steady temperature of as close to 22°C as possible. The PID controller looks at the setpoint and compares it with the actual value of the Process Variable (PV). Back in the house, the box of electronics that is the PID controller in the Heating and Cooling system looks at the value of the temperature sensor in the room and sees how close it is to 22°C.


If the SP and the PV are the same – this the ideal case for the controller. It doesn’t have to do anything, it will set its output to zero.

However, if there is a difference between the SP and the PV we have an error and corrective action is needed. In the house, this will either be cooling or heating depending on whether the PV is higher or lower than the SP respectively.

Let’s imagine the temperature PV in the house is higher than the SP. It is too hot. The air-con is switched on and the temperature drops. The sensor picks up the lower temperature, feeds that back to the controller, the controller sees that the “temperature error” is not as great because the PV (temperature) has dropped and the air con is turned down a little. This process is repeated until the house has cooled down to 22°C and there is no error. Then a disturbance hits the system and the controller has to kick in again. In this example, the disturbance may be the sun beating down on the roof, raising the temperature of the air inside. This is a simple feedback control system.

Another everyday example is the cruise control system on a road vehicle; where external influences such as gradients would cause speed changes, and the driver has the ability to alter the desired set speed. The PID algorithm restores the actual speed to the desired speed in the optimum way, without delay or overshoot, by controlling the power output of the vehicle's engine.

Below is the classic block diagram of a process under PID control:

To understand this block diagram better, let's use another example, an analogy of changing lanes on a freeway on a windy day. We are the driver, and therefore the controller of the process of changing the car’s position. Below is the block diagram we used above, with the labels changed to represent the car-on-windy-freeway control loop.

Notice how important closing the feedback loop is. If we removed the feedback loop we would have to control the car’s position with our eyes closed, as there would not be any feedback coming from the outcome of the process. In this example, our eyes are the sensors which measures the position of the car (PV) continuously and feeds the data back to the input, which is the steering wheel in order to keep the car in the desired position (SP).

As we saw in the house-temperature example and the car changing lanes analogy, the controller takes both the PV and SP signals, which it then puts through a black box to calculate a controller output. That controller output is sent to an actuator which moves to actually control the process.

We are interested here in what the black box actually does, which is that it applies 1, 2 or 3 calculations to the SP and Measured PV signals. These calculations, called the “Modes of Control” include:

- Proportional (P)
- Integral (I)
- Derivative (D)

The block diagram of the PID Controller might look complex but actually it is a simple operation. The PV is subtracted from the SP to create the Error. The error is simply multiplied by one, two or all of the calculated P, I and D actions (depending which ones are turned on). Then the resulting “error x control actions” are added together and sent to the controller output.

These 3 modes are used in different combinations:

- P | Sometimes used
- PI | Most often used
- PID | Sometimes used

Term "P" is proportional to the current value of the SP − PV error e(t). For example, if the error is large and positive, the control output will be proportionately large and positive, taking into account the gain factor "K". Using proportional control alone in a process with compensation such as temperature control, will result in an error between the setpoint and the actual process value, because it requires an error to generate the proportional response. If there is no error, there is no corrective response.

Term "I" accounts for past values of the SP − PV error and integrates them over time to produce the I term. For example, if there is a residual SP − PV error after the application of proportional control, the integral term seeks to eliminate the residual error by adding a control effect due to the historic cumulative value of the error. When the error is eliminated, the integral term will cease to grow. This will result in the proportional effect diminishing as the error decreases, but this is compensated for by the growing integral effect.

Term "D" is a best estimate of the future trend of the SP − PV error, based on its current rate of change. It is sometimes called "anticipatory control", as it is effectively seeking to reduce the effect of the SP − PV error by exerting a control influence generated by the rate of error change. The more rapid the change, the greater the controlling or dampening effect.

Tuning: The balance of these effects is achieved by "loop tuning" to produce the optimal control function. The tuning constants are shown below as "Kp, Ki and Kd" and must be derived for each control application, as they depend on the response characteristics of the complete loop external to the controller. These are dependent on the behaviour of the measuring sensor, the final control element, any control signal delays and the process itself. Approximate values of constants can usually be initially entered knowing the type of application, but they are normally tuned, by testing the process in practice by such as introducing a setpoint change and observing the system response. We will go over these Kp, Ki and Kd constants and the tuning process later when we would be creating a PID controller for Nova on Arduino Software (IDE).

After a brief look on what control theory is, let's see why would we need it for Nova in a couple of examples before getting into coding.

For example, let's assume we are programming Nova to track an object with a specific colour, a red ball as an example. The main purpose of tracking for this example is to keep the object always in the center of the frame by moving the camera. If the resolution of the video frame that we are using to track the red ball is 320 x 240 pixels, the exact central position of the red ball should be 160 pixels horizontal and 120 pixels vertical, ideally. These coordinates (160, 120) are your setpoint, the target values you want Nova to achieve. Imagine you move the ball to the left relative to the camera, let's say 40 pixels far away. We would have an error of 40 pixels in the x axis. This error should be feedback to the motors in such a way that Nova moves and gets back to the setpoint as quick as possible without overshooting the target value. Without a PID controller, it is very challenging to achieve a smooth tracking in computer vision applications.

Another application of PID controller with Nova will be covered in the next chapters with ultrasonic sound sensors to measure and keep a specific distance between the sensor and an object. We will be providing a setpoint such as 15 cm, as the target distance we want Nova to keep between with an object. PV will be the current value that the ultrasonic sound sensor measures. The difference between them, the error, will be feedback to the motors in order for Nova to keep the same distance even if the object moves. We will see the details in the next chapters.

Now, let's open Arduino Software (IDE) and we can start getting familiar with PID library and functions.   



Just as we discussed before, PID library in Arduino Software(IDE) has PV, SP and output generated. Process Value, PV, is referred as "Input" in PID library as it is the data we are getting from sensors. PID controller requires 3 variables:

- What to measure, referred as the "Input",
- Where you want that measurement to be, referred as the "Setpoint",
- The variable to adjust that can make that happen, referred as the "Output".

The PID then adjusts the output trying to make the input equal the setpoint.

Let's start by downloading the PID library through the link below:

Unzip the folder and make sure the name of the folder is PID_v1. Then, copy this folder to the Libraries folder under Arduino folder on your PC. Then, restart the Arduino Software (IDE). If you have done it correctly, you should see the PID library appear under File > Examples.

The black magic of PID comes in when we talk about how it adjusts the Output to drive the Input towards Setpoint. There are 3 tuning parameters: Kp, Ki and Kd. Adjusting these values will change the way the output is adjusted. All of these can be achieved depending on the values of Kp, Ki, and Kd.

There isn't one right answer for the right tuning values to use. The values that work for one application may not work for another, just as the driving style that works for a truck may not work for a race car. With each new application you will need to try several tuning values until you find a set that gives you what you want.


Let's start by investigating the functions that are provided with PID library:

PID(): Creates a PID controller linked to the specified Input, Output, and Setpoint. The PID algorithm is in parallel form. Syntax of the function is as shown below.

PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction)

Input: The variable we're trying to control (double).
Output: The variable that will be adjusted by the PID (double).
Setpoint: The value we want to Input to maintain (double).
Kp, Ki, Kd: These tuning parameters affect how the PID will change the output(double>=0).
Direction: Either DIRECT or REVERSE. This determines which direction the output will move when faced with a given error. DIRECT is most common.

Compute(): Contains the pid algorithm. it should be called once every loop(). Most of the time it will just return without doing anything. At a frequency specified by SetSampleTime() it will calculate a new Output.

SetOutputLimits(): The PID controller is designed to vary its output within a given range. By default this range is 0-255: the arduino PWM range. There's no use sending 300, 400, or 500 to the PWM. Depending on the application though, a different range may be desired. Syntax is as following: SetOutputLimits(min, max).

SetSampleTime(): Determines how often the PID algorithm evaluates. The default is 200mS. For robotics applications this may need to be faster, but for the most part 200mS is plenty fast. Syntax is as following: SetSampleTime(SampleTime).

We will understand these functions and the programming structure better in the next chapter when we will be working with the Ultrasonic Sound Sensor.

bottom of page