PID Voltage Control by Temperature

Hello guys, i've questions about pid.

Im making a project which uses compressor to cool some kinda laser diode.Compressor has a special driver which has a input 5V analog input. Im driving compressor with arduino and made a spimple dac to convert digital signal to analog signal(via port manuplation).My cause to use pid is the compressor will be used different ambient temperatures so it should adjusts its speed by temperature changes.Now im working at home so i don't have a system here but i've ds18b20 temperature sensor and my dac circuit that all i need.I wrote the pid code with dac and ds18b20 codes.So my input is temperature and my output is voltage(0-~5V).I set the Setpoint 23 degree celsius.

My understanding of pid is the input value reaches the setpoint it oscillates a little but doesn't change more.
In my code input(temperature sensor value) reaches the Setpoint but my Output(voltage) increases continuosly until maximum.

Ofcourse the cause should be from my pid tuning parameters i set my parameters with this method;

-Set all gains to zero.
-Increase the P gain until the response to a disturbance is steady oscillation.
-Increase the D gain until the the oscillations go away (i.e. it's critically damped).
-Repeat steps 2 and 3 until increasing the D gain does not stop the oscillations.
-Set P and D to the last stable values.
-Increase the I gain until it brings you to the setpoint with the number of oscillations desired (normally zero but a quicker response can be had if you don't mind a couple oscillations of overshoot)

I also suspicious about map command which mapped my sensor value but i don't know a newbie drowning pid sh*t........

here is my code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <PID_v1.h>
double Setpoint, Input, Output;
//double aggKp = 4, aggKi = 0.2, aggKd = 1;
double consKp = 0.5, consKi = 0.05, consKd = 0.25;
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

void setup()
{

  sensors.begin();  // Start up the library
  Serial.begin(9600);
 for (int i=42;i<50;i++)// PORT manuplation it implies "PORTL"
    pinMode(i,OUTPUT);
//Input = sensors.getTempCByIndex(0); 
Setpoint = 23.3;
  myPID.SetOutputLimits(50, 236); //MIN,MAX value the compressor working between these values
  //(0.8V-4.5V)
  //turn the PID on
  myPID.SetMode(AUTOMATIC);  // MANUAL
 
}
void loop(void)

{

 sensors.requestTemperatures();
  Serial.print("Temperature: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print((char)176);//shows degrees character
  Serial.print("C  |  ");
  
  //print the temperature in Fahrenheit
  Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  Serial.print((char)176);//shows degrees character
  Serial.println("F");
  
  delay(500);

  Setpoint = 23.3;
Input = map(sensors.getTempCByIndex(0), 0, 1023, 0, 255); 
//double difference = abs (Setpoint-Input);
//if(difference<1){
 myPID.SetTunings (consKp, consKi, consKd);//}
  //else{
    //myPID.SetTunings (aggKp, aggKi, aggKd);}

myPID.Compute();
PORTL = Output;//(VOLTAGE OUTPUT)


}

the attached image is my dac circuit btw.

duwolf:
My understanding of pid is the input value reaches the setpoint it oscillates a little but doesn't change more.
In my code input(temperature sensor value) reaches the Setpoint but my Output(voltage) increases continuosly until maximum.

i thought PID is more appropriate to control some that has inertial. In my two experiences, the first was to synchronize a clock by controlling the frequency of the clock. Adjusting the frequency high/low cause the phase alignment to change and is likely to overshoot.

PID attempts to adjust what it can knowing the error and the rate of change with varying amounts of oscillation around the target.

in the other case, a motor, controlling the speed of the motor indirectly affects position. Again, controlling the speed of the motor affects both the position and rate of change.

in your case, can the compressor simply be set high/low to control temperature? Does the thing being controlled have any inertia?

If you're cooling, I would expect you to be using REVERSE, not DIRECT.

I also suspicious about map command which mapped my sensor value but i don't know a newbie drowning pid sh*t........

You should not map the temperature input. The ds18b20 returns a temperature not some analog value between 0 and 1023. Your setpoint is in degree C, and your input should be in the same units in order for the error term to have any meaning.

gcjr:
in your case, can the compressor simply be set high/low to control temperature? Does the thing being controlled have any inertia?

It cant be controlled high/low couse it constantly cycled R134 to condenser>expansion valve>evaporator>comperessor gas cools the pure water and water cools the diode. Compressor motor should have inerta i think. but it is not important right now cause im just working with temperature sensor and multimeter. and im trying to move step by step.

wildbill:
If you're cooling, I would expect you to be using REVERSE, not DIRECT.

yeah im aware of that when i change it to REVERSE multimeter reads minimum value all the time so i change it to DIRECT to see changes easiliy. thanks for your comment :slight_smile:

cattledog:
You should not map the temperature input. The ds18b20 returns a temperature not some analog value between 0 and 1023. Your setpoint is in degree C, and your input should be in the same units in order for the error term to have any meaning.

im sure that my input is in degree Celsius, when i erase the map part and writing like this;

Input = sensors.getTempCByIndex(0);

and there is no output..

Btw guys i've an idea to eliminate the PID

i taught, i calibrate the motor parameters to temperature paramaters.

PORTL works with 0-255 voltage value
example when i wrote 150 to serial it gives 2.85V it equals to %65 of my compressor

should i equalize the voltage values to temperature values like(of course it will be in C language);

for example when temperature is

27 C >PORTL=150
26.5 C >PORTL=152
.
.
.
.

i think its kinda manual PID but resolution is weaker. but i eliminate the PID inconsistency cause sometimes it oscillates and can damage my compressor. I can take opinions about this too.

Running the compressor at a % value based on ambient temperature (or do you mean the measured temperature of the laser diode?) can work if the laser diode produces a constant heat output, and its temperature is a function of the heat lost to ambient. To keep the laser diode at a constant temperature, you are relying on the loss to ambient and the loss to the compressor cooling.

What you haven't described is the laser diode, its operating regime, and its temperature characteristics.

Can you please explain more about the characteristics of the system you are trying to control.

Full PID control may not be the best option, particularly if there is a constant output required to maintain a setpoint.

cattledog:
Running the compressor at a % value based on ambient temperature (or do you mean the measured temperature of the laser diode?) can work if the laser diode produces a constant heat output, and its temperature is a function of the heat lost to ambient. To keep the laser diode at a constant temperature, you are relying on the loss to ambient and the loss to the compressor cooling.

What you haven't described is the laser diode, its operating regime, and its temperature characteristics.

Can you please explain more about the characteristics of the system you are trying to control.

Full PID control may not be the best option, particularly if there is a constant output required to maintain a setpoint.

yeah ambient temperature is an external factor for my project it can't be known.laser diode generates heat around 79W maximum but it will change im not sure about that, diode worked between 20-35 degree Celsius efficently.Im trying to achieve to keep laser diode 27 degree Celsius.Laser diode looks like square plate under that plate there is cool water flow to decrease the temperature of diode. My temperature measurement should be between diode and water flowing plate but there is no space between those two. After lots of simulations i calculated the heat transfer between these plates (heat generation vs liquid flow) and i conclude i should measure the system temperature from under the water flowing plate.Its around 13 degree Celsius optimum.

at 79W and 2.85V((%65)which is arduinos output and 21 degree Celsius room temperature it cooled 13 degree Celsius.

So the setpoint should be 13 degree Celsius for PID right?

and if ambient temperature changes it will effect the measurement ofcourse
if i use that

13degree C > PORTL = 150 thing for all probable temperatures in theory it can be work i guess.

In that attachment there is picture of my system.
https://ibb.co/Pr3wK4q

diode worked between 20-35 degree Celsius efficiently. Im trying to achieve to keep laser diode 27 degree Celsius.

How did you manage to measure the temperature of the diode to determine these values? Ideally since this is the temperature you are trying to control, it would be best to use this measurement point.

After lots of simulations i calculated the heat transfer between these plates (heat generation vs liquid flow) and i conclude i should measure the system temperature from under the water flowing plate. Its around 13 degree Celsius optimum.
at 79W and 2.85V((%65)which is arduinos output and 21 degree Celsius room temperature it cooled 13 degree Celsius.

So the setpoint should be 13 degree Celsius for PID right?

Yes 13C is your setpoint for desired temperature in this situation if you can't directly measure the diode. However, 13 C is dependent upon the watts output of the laser and the ambient temperature of the room.

laser diode generates heat around 79W maximum but it will change im not sure about that,

Understanding the heat output of the laser diode is important. It is is not running at 79W maximum but say at 50W because of some duty cycle what happens to the temperature with the compressor at 2.85V and the measurement at 13C. Will the diode temperature drop below 20C? What about startup? If the diode has been off, and then turns on, do you want to start cooling immediately with 13C?

cattledog:
How did you manage to measure the temperature of the diode to determine these values? Ideally since this is the temperature you are trying to control, it would be best to use this measurement point.

these values comes from my optical engineer friend. she knows the values and worked with diodes.

cattledog:
Understanding the heat output of the laser diode is important. It is is not running at 79W maximum but say at 50W because of some duty cycle what happens to the temperature with the compressor at 2.85V and the measurement at 13C. Will the diode temperature drop below 20C? What about startup? If the diode has been off, and then turns on, do you want to start cooling immediately with 13C?

when the heat generation and compressor starts after 10 min it reaches balance point which is 13 C. Btw there is no diode right now there are heat resistances to simulate cause diodes are expensive to work on right now. Actually i don't know diode temperature drop below 20C cause i can't measure but i did some measurement i heat up the resistance about 79 W and measure the temperature with thermocouples the top and bottom of the plate temperature reaches the nearly 150 C after this experiment i started the compressor with same condition and collect the temperature datas with thermocouples when there is no change in temperatures the values for the top of the plate was 71C and bottom is 13C calculating the heat transfer in heat generation object is nearly impossible to me so i believe i achieve to reduce the temperature due to this values also my supervisor approves too. When start up compressor should work immediately for cooling i don't believe the diode temperature drop below 20C at start cause there is heat generation and cooling just starts so its not a problem.If diode has been off compressor should be off but water pump work constantly to carry the heat so diode isn't easilly heat up due to ambient temp or getting cooled in that time cause every cycle of water it takes the heat from diode and carries but water also heat up while flowing. When diode turns on compressor turns on. Honestly I am just an automotive engineer, don't know much about these stuff but i need a solution should I work on PID or just manually write the code like, this temperature work that %

should I work on PID or just manually write the code like, this temperature work that %

Given what you have explained, I don't think you will need PID to manage the temperature.

13degree C > PORTL = 150 thing for all probable temperatures in theory it can be work i guess.

I don't think there will much problem either running the compressor at a constant 2.85v. If you see significant variations in temperature under actual operating conditions, then moving the voltage up or down from that point dependent upon the temperature reading would be easy enough to do.

Keep it simple until you know that you have to do more.

Thanks for your help and time mate.