PID Library doubts

[PID] Hi everyone. I’m using Arduino to do my graduation conclusion work, which consists in projecting a controller to control the temperature of a bowl full of water. I have already designed the control, calculated the gains(Kp and Ki, only because the system response is slow and hence the Kp should be 0). To do so, I’m feeding a 1000w resistance with 127VAC, but controlling it with relays. With 5V(or digital 1) the relay change states and then it flows current. With 0V(or digital 0), it doesn’t flow current.

The main idea is to have a setpoint of temperature(that can be changed at any times, for Ex: At one time it is 50°C and then i want it to reach 90°C), which by Kp and Ki(already calculated), the controller reaches that point and maintain it. I’m using a LM35 temperature measuring sensor and already know how to collect the instant temperatures to show on Serial Monitor.

I have found a Arduino library which has a code for relay control, but i quite doesn’t understand it. Would appreciate if someone has any information(i’ll copy the code on comments) or any ideia or understands the PID code at all.

/********************************************************
 * PID RelayOutput Example
 * Same as basic example, except that this time, the output
 * is going to a digital pin which (we presume) is controlling
 * a relay.  the pid is designed to Output an analog value,
 * but the relay can only be On/Off.
 *
 *   to connect them together we use "time proportioning
 * control"  it's essentially a really slow version of PWM.
 * first we decide on a window size (5000mS say.) we then
 * set the pid to adjust its output between 0 and that window
 * size.  lastly, we add some logic that translates the PID
 * output into "Relay On Time" with the remainder of the
 * window being "Relay Off Time"
 ********************************************************/

#include <PID_v1.h>

#define PIN_INPUT 0
#define RELAY_PIN 6

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;

void setup()
{
  windowStartTime = millis();

  //initialize the variables we're linked to
  Setpoint = 100;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(PIN_INPUT);
  myPID.Compute();

  /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
  if (millis() - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
  else digitalWrite(RELAY_PIN, LOW);

}

Thanks!

Seems to me it would be much easier to call myPID.Compute() at a frequency that reflects the ability of your system to react and then just turn the relay on or off depending on the output of the calculation.

…R

PID is intended for use in a situation where you have proportional control of something e.g. the throttle on an engine or a water tap. Your situation is different in that you can only turn the heat on or off.

The code you have found is intended to address that by having the PID's output control how much of a window of time your relay is on and how much of the window is off. It's ersatz pulse width modulation, but with a longer cycle time so you don't fry your relay.

What are your temperature stability requirements? Did you consider bang-bang control with some hysteresis (i.e. like your home HVAC system works)?

Robin2:
Seems to me it would be much easier to call myPID.Compute() at a frequency that reflects the ability of your system to react and then just turn the relay on or off depending on the output of the calculation.

...R

So, actually that is the problem. What is myPid.Compute()?? How does it work?

wildbill:
PID is intended for use in a situation where you have proportional control of something e.g. the throttle on an engine or a water tap. Your situation is different in that you can only turn the heat on or off.

The code you have found is intended to address that by having the PID's output control how much of a window of time your relay is on and how much of the window is off. It's ersatz pulse width modulation, but with a longer cycle time so you don't fry your relay.

Ok so by that you mean this code runs a similar function to PWM? Therefore i just would have to adjust the K's parameters and then it would calculate by itself the on/off? I just don't get the code!

gfvalvo:
What are your temperature stability requirements? Did you consider bang-bang control with some hysteresis (i.e. like your home HVAC system works)?

It was asked by my professor to do a PID control. It means that the systems response should be fast enough to keep the temperature on the setpoint, not passing it, so it would save some energy and other stuff. Hence, the system stability requirements are: Do not pass over the setpoint of temperature. An on/off controller is out of question(i have already proposed it).

Thank you all for your answers. It is the last part of my project, but i'm finding myself out of options. I have already designed this power drive module(which was hard to do) and I have no more time to spend on another project. This PID + Arduino relay control is my last hope(before i have to run a new project, which i have no idea of what!) Any help will be welcome. Thanks!

This solution is based on what I understand of your project:

You would need to calculate the rate of change of the temperature. Then, calculate the cut-off value based on the rate of change and turn on/off the system at this value.

It was asked by my professor to do a PID control.

The ENTIRE point of the assignment being that YOU should LEARN HOW and WHY a PID works! The Arduino PID is VERY simple, and the author has several very nice article he's written describing in detail how it works. Now YOU need to do some work, and learn what you're supposed to learn, and earn your grade....

Regards,
Ray L.

luisbaixo:
So, actually that is the problem. What is myPid.Compute()?? How does it work?

Ok so by that you mean this code runs a similar function to PWM? Therefore i just would have to adjust the K's parameters and then it would calculate by itself the on/off? I just don't get the code!

It was asked by my professor to do a PID control. It means that the systems response should be fast enough to keep the temperature on the setpoint, not passing it, so it would save some energy and other stuff. Hence, the system stability requirements are: Do not pass over the setpoint of temperature. An on/off controller is out of question(i have already proposed it).

Thank you all for your answers. It is the last part of my project, but i'm finding myself out of options. I have already designed this power drive module(which was hard to do) and I have no more time to spend on another project. This PID + Arduino relay control is my last hope(before i have to run a new project, which i have no idea of what!) Any help will be welcome. Thanks!

You should be able to control temperature of a liquid with a relay easily
Your PID needs to control the relay properly
How I would control the relay with a 120Vac source is to cycle every 2 seconds.
The frequency of the Pulse with is 2 seconds 0.2 Hz the relay will be on for a duration from 0 to 2 seconds depending upon how much heat you need to supply the water. at a demand for 2 seconds the relay actually never turns off. but anything less than a 2 second demand the relay is off for the remainder of the 2 seconds.

now your PID needs to maintain a temperature and so the output will need to be somewhere between 0 and 2000 milliseconds (2 Seconds) so have your PID range be 2000
since your output will never be zero when setpoint is achieved you will need to have an integral value
Integral is additive. in other words it adds the error form setpoint multiplied by Ki to increase/decrease the output incrementally to achieve a proper power level to maintain setpoint.
Proportional will be added after you have achieved a stable setpoint with integral. integral is slow so be patient when starting your tuning process. too much integral the temperature will occellate and never reach setpoint.
only add proportional once integral is tuned. Proportional will assist in achieving and landing setpoint.
Remember when you have achieved setpoint Proportional will always have zero influence on the output. Kp * Zero Error from setpoint is always Zero :slight_smile:
Forget about Kd until you are happy with the first two.

Z