Problems with regulate a coffee machine boiler (PID - SSR Relais)

Hello,

i have Problems to regulate a coffee machine boiler width the PID_v1.h library.

Here my Code:

#include <PID_v1.h>
#define RelayPin 15

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

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 8, 2, 3, DIRECT);

int WindowSize = 2000;
unsigned long windowStartTime;

/********************************************************
*DALLAS TEMP
******************************************************/
 

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 13
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);




void setup()
{

  // start serial port
  Serial.begin(9600);

  
  pinMode(RelayPin, OUTPUT);

  windowStartTime = millis();

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

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

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

  sensors.begin();
  sensors.requestTemperatures();
}

void loop()
{
  sensors.requestTemperatures();
  Input = sensors.getTempCByIndex(0);
  myPID.Compute();

  /************************************************
     turn the output pin on/off based on pid output
   ************************************************/
  unsigned long now = millis();
  if (now - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if (Output > now - windowStartTime){
    Serial.print(Setpoint);
    Serial.print(",");
    Serial.print(Input);
    Serial.print(",");
    Serial.print(Output);
    Serial.print(",");
    digitalWrite(RelayPin, HIGH);
    Serial.println(Setpoint+50);
  }else{
    Serial.print(Setpoint);
    Serial.print(",");
    Serial.print(Input);
    Serial.print(",");
    Serial.print(Output);
    Serial.print(",");
    digitalWrite(RelayPin, LOW);
    Serial.println(Setpoint);
  }
}

my current values do not fit yet.
how should i tune the PID values?
Is Autotune an option?



PID_RelayOutput.ino (2.13 KB)

Is Autotune an option?

Of course it is.

Of course it is.

How it works? :smiley:

The autotune lib. is 6 Years old?
Does anyone have any experience with it?

https://playground.arduino.cc/Code/PIDAutotuneLibrary

i found a alternative:
http://ryandowning.net/AutoPID/

But the example for Relais is missing ;(

Try initial settings Kp = 2.0, Ki = 0.2, Kd = 0.1

DKWatson:
Try initial settings Kp = 2.0, Ki = 0.2, Kd = 0.1

ok - the data is based on any empirical values?

If you have ever simulated the performance of various PID algorithms, you will find that 100% of the time they will converge. The problem is that if your coefficients are too large to begin with, that time-to-converge could be months for a slow-to-react system such as temperature control. In a responsive loop (cruise control for example) the hysteresis dampens in due course quite quickly, but if your PID won't adjust the coefficients automatically, you'll be playing ping-pong forever.

DKWatson:
but if your PID won't adjust the coefficients automatically, you'll be playing ping-pong forever.

thats my big problem, finaly i need an "autotuned" PID.

But i don´t understand the Autotune Lib with relay...

Every time I see someone struggling to use a PID for simple temperature control, my first question is always: “what makes you so sure you need a PID?”. Simple Bang-Bang control with a degree of hysteresis on either side of the set point works wonderfully for your home’s HVAC system. What makes you think you need anything more complex for your application?

In this particular case, there’s another question based on basic physics. Are you really trying to boil the water? If so, then remember that water always boils at 100 degC (assuming 1 atm pressure). It never rises above that as it boils away. The resulting steam can be heated hotter, but not the water itself. So, I’m not sure what value temperature control will add.

If instead you’re just trying to heat the water to some temperature (below 100 degC) then I’ll go back to my first point. Use the Bang-Bang technique and make your life simpler.

Neither do I.
I don't generally use libraries (not allowed to), so having written PID algorithms I have some familiarity with how they work.

Since you are using a relay, the heater is either on or off. PID is useful when you can vary the output of the heater. It is (relatively) useless if all you can do is turn it on or off.

gfvalvo:
In this particular case, there’s another question based on basic physics. Are you really trying to boil the water? If so, then remember that water always boils at 100 degC (assuming 1 atm pressure). It never rises above that as it boils away. The resulting steam can be heated hotter, but not the water itself. So, I’m not sure what value temperature control will add.

If instead you’re just trying to heat the water to some temperature (below 100 degC) then I’ll go back to my first point. Use the Bang-Bang technique and make your life simpler.

Aha!
A key point! In fact, unless you are making instant, boiling water should never come in contact with fresh ground coffee. It scorches the bean and makes it bitter. Just an aside from a coffee connoisseur.

i know ;D

we need a temperature of 94-96°C the temperature is also dependent on the beans:
is measured at the boiler.

  1. the temperature must be kept here
  2. If cold water flows in, it must be warm again quickly

I also saw Bang Bang.

But how can i use this sample Code with an SSR Relais (on/off).
I can´t use an PWM Signal…
I must use the on and off time to regulate the temperature.

Thanks!

/*
   AutoPID BasicTempControl Example Sketch

   This program reads a dallas temperature probe as input, potentiometer as setpoint, drives an analog output.
   It lights an LED when the temperature has reached the setpoint.
*/
#include <AutoPID.h>
#include <DallasTemperature.h>
#include <OneWire.h>

//pins
#define POT_PIN A0
#define OUTPUT_PIN A1
#define TEMP_PROBE_PIN 5
#define LED_PIN 6

#define TEMP_READ_DELAY 800 //can only read digital temp sensor every ~750ms

//pid settings and gains
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP .12
#define KI .0003
#define KD 0

double temperature, setPoint, outputVal;

OneWire oneWire(TEMP_PROBE_PIN);
DallasTemperature temperatureSensors(&oneWire);

//input/output variables passed by reference, so they are updated automatically
AutoPID myPID(&temperature, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);

unsigned long lastTempUpdate; //tracks clock time of last temp update

//call repeatedly in loop, only updates after a certain time interval
//returns true if update happened
bool updateTemperature() {
  if ((millis() - lastTempUpdate) > TEMP_READ_DELAY) {
    temperature = temperatureSensors.getTempFByIndex(0); //get temp reading
    lastTempUpdate = millis();
    temperatureSensors.requestTemperatures(); //request reading for next time
    return true;
  }
  return false;
}//void updateTemperature


void setup() {
  pinMode(POT_PIN, INPUT);
  pinMode(OUTPUT_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  temperatureSensors.begin();
  temperatureSensors.requestTemperatures();
  while (!updateTemperature()) {} //wait until temp sensor updated

  //if temperature is more than 4 degrees below or above setpoint, OUTPUT will be set to min or max respectively
  myPID.setBangBang(4);
  //set PID update interval to 4000ms
  myPID.setTimeStep(4000);

}//void setup


void loop() {
  updateTemperature();
  setPoint = analogRead(POT_PIN);
  myPID.run(); //call every loop, updates automatically at certain time interval
  analogWrite(OUTPUT_PIN, outputVal);
  digitalWrite(LED_PIN, myPID.atSetPoint(1)); //light up LED when we're at setpoint +-1 degree

}//void loop

btotherunnre:
I also saw Bang Bang.

Pseudo code:

const float hysteresis = 1.0;
float setPoint;

void loop() {
  float currentTemp = getTheCurrentTemperature();
  
  if (currentTemp <= (setPoint - hysteresis)) {
    turnHeaterOn();
  }

  if (currentTemp >= (setPoint + hysteresis)) {
    turnHeaterOff();
  }
}

Done, go drink the coffee.

gfvalvo:
Pseudo code:

const float hysteresis = 1.0;

float setPoint;

void loop() {
  float currentTemp = getTheCurrentTemperature();
 
  if (currentTemp <= (setPoint - hysteresis)) {
    turnHeaterOn();
  }

if (currentTemp >= (setPoint + hysteresis)) {
    turnHeaterOff();
  }
}




Done, go drink the coffee.

but this is not an regulator.
u will become trouble when u take a coffee :smiley:
I will regulate the Temperatur with PID autoPID autotune or Bang Bang :smiley:

btotherunnre:
but this is not an regulator.

Of course it is. The temperature will remain within +/-1 degree of the set point.

I will regulate the Temperatur with PID autoPID autotune or Bang Bang :smiley:

That IS Bang-Bang. Like I said, go take a look at how the thermostat in your house works.

I tend to agree with most. Heating elements are not really conducive to PID control. What you need is a pair of set-points, one for ON and the other for OFF. With some historical data you can determine when to (de)activate the element to maintain a relatively constant temp.

am I right in the assumption:

Bang-Bang is fine for example:
I turn on the machine - and the Watertemp. must go from 20°C to 96°C.
From there, a PI(D) should take over.

btotherunnre:
am I right in the assumption:

Bang-Bang is fine for example:
I turn on the machine - and the Watertemp. must go from 20°C to 96°C.
From there, a PI(D) should take over.

No, you are incorrect.

Not really. It's still overkill to use PID. I would think that as the temperature rises, maybe at 94 you shut it off as you know it will continue to rise a bit. When it stops rising, it will begin to fall. At that time switch on again until it stops falling and then switch off, or at some other predetermined values. That's bang-bang, as it was pointed out, how your furnace/AC thermostat works.

ok i will test - a simple bang bang.
But why use all the big espresso maschine supplier an PID?