# PID finetuning for central heating

I'm currently building & programming a controller for an electrical central heating. The system has the following hardware components:

• heater
• pump
• temperature sensors

To control the temperature in the heating system, the heater and the pump have to be turned on. Both are controlled by PWM.

I decided to use a PID (Proportional Integral Derivative) controller because it's commonly used in many solutions.

The first test I did was already successful with the following settings:

• SetPoint 55°C
• P 10
• I 2
• D 10

The target temperature of 55°C is never reached, but I'm very happy with the stable heater results. The heater is using around 80% power which results in a stable temperature output.

I performed more tests to see if I can tune the settings to reach the SetPoint, but I discovered that the heater control becomes more unstable (fluctuates between 50% and 100%) with these settings:

• SetPoint 55°C
• P 82
• I 25
• D 70

This can be explained because I'm using the output of the PID to control the heater. So the closer it gets to the setpoint, the smaller the PID output. But to keep the temperature at 55°C, the heater probably needs to be at 90% power. So how do I finetune the system? I'm thinking about:

• put the SetPoint to 60°C and try if the PID stabilize around 55°C
• use a bias (for example heater = 50% + PID output)

My PID controller uses the following formula:

heater = kp_gain * pid_error + (kd_gain * (pid_error - pid_error_old) / 55) + (ki_gain * (pid_error + pid_error_old) / 55);


What I'm looking for is to reach the setpoint of 55°C with a stable heater output (my guess it would be around 90%). How can I achieve this?

• Did you follow a particular tuning procedure to arrive at these values? Jul 13 '21 at 7:40
• Also your integral term looks suspicious -- it's supposed to integrate errors over the entire time, not just the last two samples. Jul 13 '21 at 7:44
• @DamienD I have no tuning procedure. I just make a wild guess and perform a new test.
– spf
Jul 13 '21 at 7:54
• wild guesses cause more confusion. Jul 13 '21 at 7:55
• Look up PID tuning after fixing the mistake in your controller code. There are some more rigorous methods :) Jul 13 '21 at 8:21