PID output is 0

hello,

i am using a PLC to control an immersion heater and am having issues with the PID control. it works well until it reaches the temperature setpoint, then it will output zero and the heater stays on. the code I am using is just copied from the example on github. below is relevant code. any suggestions would be appreciated

cheers

#include <P1AM.h>
#include <PID_v1.h>

double hot_temp = 200; //target hot temp
double hot_max = 250; //hot tank max temp

double hot_tank_temp; //hot tank temp. slot 4 channel 2
bool heater; //contactor to control heater. slot 3 channel 7

double Setpoint, Input, Output;
double Ki=1, Kp=0, Kd=0;
PID PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
unsigned long pid_time;
int pid_delay = 15000; //pid loop time delay, ms

void setup() {

    pid_time = millis(); //pid start time
    setpoint = hot_temp; //hot pid setpoint
    PID.SetOutputLimits(0, pid_delay);
    PID.SetMode(AUTOMATIC);

}

void loop() {

    pid_control();

}

void pid_control() {

    input = hot_tank_temp;
    setpoint = hot_temp;

    PID.Compute();

    if (millis() - pid_time > pid_delay) {
        pid_time += pid_delay;
    }
    if (hot_tank_temp < (0.85 * hot_temp)) { //ramp up to heat tank on start
        heater = true;
    } else {
        if (output < (millis() - pid_time)) { //only turn heater on if there is no estop active
            heater = true;
        } else {
            heater = false;
        }
    }
}

Makes sense given a Ki value of 0:

1 Like

that was a typo, in the actual code it set by the hmi, but I think found an error in that code that may have been causing the issue. thanks

Is there a sign/sense error here? If output = 0, heater = true for 14999ms out of 15000ms and only be false when millis() - pid_time == 0. I'd expect the heater duty cycle to decrease as it approaches the setpoint temperature.

Also with a 0.85*200°=170° tub and those settings, the maximum duty cycle would be (200-170)*1/15000=0.2% If you want the heater duty cycle to taper off between the full-on at 170° and 200°, then Kp should be something like Kp=15000/(200-170)=500ms/°.

You don’t need pid control on /off will surfice for a water heater .
Water sits in layers unless stirred, so heats from the top downwards , so difficult to get an exact temperature .

With some hysteresis to prevent excessive cycling.

1 Like

you're probably right. the tank the heater is stirred so temp is mostly uniform and will be cycled with cold water once an hour, but i can account for that in other ways instead of trying to get this to work

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.