# Logic behind a PID controller?

I'm having trouble understanding the logic behind a PID controller used for temperature control. If I were to implement it with a SSR and an Arduino, for example, my aim is to have the PID value be zero, is that correct? Anything different than zero and the relay would activate and make the heating element get hot until the error reaches zero? Is it possible to calculate how much time the relay needs to be activated to reach the temperature or is it better to activate it on really small intervals and check the error?

• Logic behind a PID controller is usually explained in a college in a semester-long sophomore class called "Control Theory", with three or four prerequisite classes of math and basic engineering. web.stanford.edu/class/archive/ee/ee392m/ee392m.1034/… – Ale..chenski Nov 4 '18 at 18:35
• why do you want to use PID? simple on/off with hysteresis is not enough? – Juraj Nov 4 '18 at 18:49
• recommended reading: PID without a Ph.D. – Nick Alexeev Nov 5 '18 at 19:45
• You have identified the control variable as the amount of time that the relay is activated. You should also choose a period over which you update related to the time it takes for your system to heat up or cool down. Control Engineers aim for an update period about 5-10% of the system "time constant" (the time it takes from 0 to 37% of the full-on temperature.) For small, fast heating systems the time constant and update period could be seconds or less, but for large scale systems it could be minutes. The output of the PID is the fraction or time in the "on" state during the update period. – Dave X Aug 15 '20 at 13:48

... my aim is to have the PID value be zero, is that correct?

Not quite. You are hoping that the error will be zero.

Anything different than zero and the relay would activate and make the heating element get hot until the error reaches zero?

Not always. The PID controller may suppress the output if the output is increasing at a rate that would cause it to overshoot - even though it is below setpoint at the moment.

Is it possible to calculate how much time the relay needs to be activated to reach the temperature or is it better to activate it on really small intervals and check the error?

Generally you will be limited by the number of switching cycles the relay can tolerate. You might decide that switching once every 5 s to 10 s is slow enough to prolong the life of the relay but fast enough that thermal response of the load doesn't allow the temperature to vary too much.

Having done that, you then apply proportional time control to the relay. Let's say you decide on a 5 s cycle then if the PID output is 20% the output is on for 1 s out of 5 s. For 60% it's 3 s out of 5 s, etc.

Figure 3. The classic PID control function. Source: Wikipedia - PID controller.

As explained in one of my linked answers below, as the integral action corrects the output the error falls to zero so the contribution of the proportional control also falls to zero. The output when the error is zero is maintained purely by the integral action.

See my answers to the questions below for more on the topic.

When you say for example "if the PID output is 20% the output is on for 1 s out of 5 s", the 20% is in relation to what? The setpoint? Because I thought the output was a numeric value.

The 20% will be the percentage of full-scale (or maximum) PID output. For the cruise control on your car this maximum would be accelerator pedal pressed to the floor; for a refrigeration system it would be cooling valve fully open; for a ship's steering control it would be 100% rudder; for your heater it is 100% output.

The output of your PID will be a number between 0 and a maximum. If you're working with an 8-bit micro then that might be 0 to 255 (28 -1) while for a 16-bit micro it might be 216 -1 = 65535. Meanwhile, for a micro with floating point capability you might set it as 0 to 1.0.

What ever value the PID outputs has to be converted to a percentage of your heater duty-cycle, 5 s in our example. This part is just simple mathematics.

• I see, that makes sense. A little question though. When you say for example "if the PID output is 20% the output is on for 1 s out of 5 s", the 20% is in relation to what? The setpoint? Because I thought the output was a numeric value. – Lucas S. Nov 4 '18 at 15:56
• See the update. – Transistor Nov 4 '18 at 16:08
• "The output of your PID will be a number between 0 and a maximum." For temperature control, or anything else where the drive only goes one direction. For things (like motors) where the actuator can pull as well as push, it'll be -maximum to +maximum (i.e., -100% to +100%, -32787 to +32767, -1.0 to +1.0, etc.). – TimWescott Nov 4 '18 at 16:20

Anything different than zero and the relay would activate and make the heating element get hot until the error reaches zero?

This is not proportional control, it is bang-bang or on-off control.

You would normally want to have a small error band around your target temperature to avoid excessively toggling your actuator, and you'd use hysteresis in the control.

For example, if your target is 50 C, then you might turn on the heater only if the temperature drops below 47 C, and turn it off only when the temperature goes above 53 C.

Is it possible to calculate how much time the relay needs to be activated to reach the temperature or is it better to activate it on really small intervals and check the error?

If you try to calculate the time needed, that's not even really a control system, it's just "dead reckoning". Depending how well your thermal environment is controlled and characterized, it might even work.

But normally you'd turn the heater on, and continue to monitor the temperature while leaving it on, until your control law tells you to turn it off.

In a proportional controller (P), you'd vary the power of the heater depending on how far the actual temperature is from the target. The exact relationship between temperature difference and heater power is a proportionality constant you'd need to choose to optimize your design.

In a PID controller you'd also consider the history of past error measurements, and how quickly the error is changing when determining the heater power. Now you have 3 proportionality constants you can adjust to optimize your control system.

As Jeroen3's answer says, if you want to vary the heater power proportionally using only an SSR to control it, you'd have to use a PWM scheme of some kind. You'd want to have the object's thermal time constant much longer than the minimum interval you can turn the heater on and off for.

• why do you want to use PID? simple on/off with hysteresis is not enough? – Juraj Nov 4 '18 at 18:19
• @Juraj, maybe when you don't want the dead band from hysteresis, or when the plant is prone to overshoot. – The Photon Nov 4 '18 at 18:40
• you want implement phase control to regulate power to heater? – Juraj Nov 4 '18 at 18:42
• sorry, I don't know how it happen that I commented your Answer and not the Question – Juraj Nov 4 '18 at 18:49

A PID controller with an on/off actuator can still provide more than only on/off control.
View the output of the PID loop as duty cycle. Thus 0.0 to 1.0 means 0 to 100% duty cycle of the relay within a configurable time period.

For example, an output of 0.5 could mean the relay should activate 50% of a 60 seconds period, 30 seconds on, 30 seconds off.

As Felthry correctly warns, you may not make the period too short. For example, if you are controlling a 10 kW heater, you must not use a 1 second period. It will be the death to any mechanical relay within a few hours, and it will cause havoc on your electrical network. But you also must not make it too long, otherwise the feature it is of no use at all.

This means you must consider the minimum on/off time of the output bit.

• Careful using a PID controller with a relay like this, though. If your time period is too short, your circuit will actuate the relay frequently and shorten the relay lifespan. But a sixty-second period as suggested here should be fine; that means no more than one actuation cycle of the relay every minute. – Hearth Nov 4 '18 at 15:21
• I don't quite understand how to do that. Let's say I have my Kp = 1 and the other constants are zero. The temperature right now is 30 ºC and I want 50 ºC. The error being 20 and the total PID value also being 20, how would I translate it to what you're saying? – Lucas S. Nov 4 '18 at 15:32
• @LucasS., it depends on your proportionality constant. You have a 20 degree difference. Maybe that means you want 50% duty cycle, so you close the relay for 30 s and then open it for 30s. If there were only 10 degree difference, you'd close the relay for 15 s and open it for 45 s. (Of course this is just P control, for PID control you'd also consider the sum of all your previous error measurements and the difference between the latest error measurement and the previous one) – The Photon Nov 4 '18 at 15:39