## Simulating a PID control

#### Published 10/3/2017

One thing that I have always wanted to do but never found the time for is simulating a PID controller with a heat source. In the following I describe how I do this in a small Excel spreadsheet.

The first nuisance related to this problem is that in order to simulate a PID controller, we need an enviroment to simulate.

In our case we assume we have a medium which is divided into 5 compartments labeled T_{n,} where n is a number between 1 and 5. T_{1} is heated by a heat source, and the heat propagates through each of the compartments till it reaches compartment T_{5,} which contains our sensor. Each heat compartment dissipates some heat along the way. This setup provides us with a non-linear, time delayed and exponential setup. A figure is shown below.

We first define a constant for the room temperature, which we call T_{env} and we set it to 20 degrees Celcius. Then we define T loss, which is the amount of degrees per minute per degrees Celcius which are lost.

This allows us to define the loss using the following equation:

This allows us to define the equation of each compartment in the next timestep. The new temperature for compartments 2 to 5 are simply the average of compartment n and compartment n-1 (this could be weighted however) minus the losses due to heat dissipation. For the first compartment, we set it to the old temperature minus heat losses and add the heat depending on the duty cycle d and assuming that the heating element becomes less effective as the heat rises.

Finally we can define our PID duty equation, which has a target temperature of 70 degrees celcius, and makes use of the proportional integral and differential constants (K_{p}, K_{d} and K_{i}).

Given these equations, it is possible to pick some values, which allow for a fairly optimal graph. In this case the following values were chosen:

The effects of the different parts of a PID can be seen in the three figures below, the first being proportiona only, the second being proportional + differential and the last being all three:

The excel spreadsheet is available for download from here.