Go Down

Topic: How to implement PID to control Temperature (Read 9066 times) previous topic - next topic

jpill

Hi.

I want to control temperature of a pot with peltier cells, so, i am now with some simple control, basically:

Code: [Select]

if (actualTemperature < wishedTemperature)
 ... activate relay pin to rise temperature

...
if(actualTemperature > wishedTemperature)
 ... activate relay pin to lower temperature

...


Si, i need to control that temperature with PID, i really have no clear how to implement that?

I see the PID library but i cannot understand the examples in the way i cannot see where i place the temperature read in the functions and when to activate the pins of the relays, i really had read tutorials and try to implement code but i really don't understand.

Thanks.

jremington

#1
Oct 26, 2016, 01:36 am Last Edit: Oct 26, 2016, 01:45 am by jremington
First, get temperature control working using your simple (non-PID) approach. You may find that to be satisfactory.

If your setup works, but not satisfactorily, come back to the forum, post a complete description of your setup (with details of the heater, heater driver, heater power supply and temperature sensor) and the code.

Then you will be prepared to ask specific questions that forum members can answer.
No PM's please.

heartbismane

For me PID control is quite interesting and have heard many times. But the technique is too complicated, so I'm not able to learn and apply for even in a simple app.

Can anyone describe a short overview of PID control? Does the technique really use in many fields?

gpsmikey

A couple of links you might find helpful in your quest:
http://www.csimn.com/CSI_pages/PIDforDummies.html
http://ee.sharif.edu/~industrialcontrol/Introduction_to_PID_Control.pdf
http://fabacademy.org/archives/2014/students/shooshtari.ali/week16/images/PID-Without-a-PhD.pdf

I can't find the one I liked, however, there was an article in Circuit Cellar some time ago on using a PID controller for the home brewer.  It was called "Home-brewed HERMS" and I believe it was in issue 191 - he was using PID for his mash.
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

jackrae

In an ideal world PID output should be an 'analogue' rather than a simple binary condition.  Because the controller does not output analogue you can use PWM as a pseudo-analogue.  However PWM cannot be effectively implemented using a simple driver relay since the relay would be chattering constantly.  You can however use an SSR which will quite happily operate with PWM.  Alternatively, rather than 'normal' PWM you could use "burst mode" on the output which is effectively a very low frequency form of PWM.  In this case ON for so many seconds, then OFF for so many seconds, depending on heat demand.  This method is frequently used in mains type heating systems.

Note also that PID control requires an analogue input sensor, rather than a simple digital switched input since you need to sense both the value of offset and rate of change of offset

gpsmikey

I have a PID program running on a PIC processor currently (new version is ESP8266) that runs PWM with a 2.5 second cycle time to control the temperature of an aluminum plate under my hummingbird feeder to keep it from freezing.  I use two temperature sensors (old version is analog - new version will be 18b20 sensors) - one for ambient air temperature and one for the heat plate temperature.  The 2.5 second cycle time seems to work quite well since the time constant for the heat plate is fairly long (it takes a minute or two at 100% power to bring the plate up to temperature (about 70F)).  I also have 3 green LED's around the edge to show when the power is on to the heater so I can see the duty cycle that way - given the circular shape of the feeder with the blinking green lights around it, people commented on the little flying saucer in my back yard  :)
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3

Jiggy-Ninja

For me PID control is quite interesting and have heard many times. But the technique is too complicated, so I'm not able to learn and apply for even in a simple app.

Can anyone describe a short overview of PID control? Does the technique really use in many fields?
PID is easy to understand. The hard part is tuning the constants.
In an ideal world PID output should be an 'analogue' rather than a simple binary condition.  Because the controller does not output analogue you can use PWM as a pseudo-analogue.  However PWM cannot be effectively implemented using a simple driver relay since the relay would be chattering constantly.  You can however use an SSR which will quite happily operate with PWM.  Alternatively, rather than 'normal' PWM you could use "burst mode" on the output which is effectively a very low frequency form of PWM.  In this case ON for so many seconds, then OFF for so many seconds, depending on heat demand.  This method is frequently used in mains type heating systems.

Note also that PID control requires an analogue input sensor, rather than a simple digital switched input since you need to sense both the value of offset and rate of change of offset
Your "burst mode" is not a low-frequency form of PWM, it is PWM. There is literally no theoretical difference at all whether the frequency is 50 kHz or 50 mHz.

My previous job had big thermal chambers that used PID controllers to modulate the temperature inside them according to a specific thermal profile, example:

1) ramp to 85C in 10 minutes
2) Hold for 1 hour
3) Ramp to -40C in 30 minutes
4) hold for 1 hour
5) Ramp to 25C in 20 minutes
  Repeat 100 times

The multi-kilowatt heaters and cooling compressors were controlled by relays with a PWM period of about 10 or 15 seconds. The time constant for most things in Newton's Law of Cooling is YUUUGE!!, probably many many minutes long for a good pot of water. A PWM cycle lasting several seconds should be more than good enough for controlling temperature on most things with a large thermal mass.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

jackrae

PID is easy to understand. The hard part is tuning the constants.Your "burst mode" is not a low-frequency form of PWM, it is PWM. There is literally no theoretical difference at all whether the frequency is 50 kHz or 50 mHz.

My previous job had big thermal chambers that used PID controllers to modulate the temperature inside them according to a specific thermal profile, example:

1) ramp to 85C in 10 minutes
2) Hold for 1 hour
3) Ramp to -40C in 30 minutes
4) hold for 1 hour
5) Ramp to 25C in 20 minutes
  Repeat 100 times

The multi-kilowatt heaters and cooling compressors were controlled by relays with a PWM period of about 10 or 15 seconds. The time constant for most things in Newton's Law of Cooling is YUUUGE!!, probably many many minutes long for a good pot of water. A PWM cycle lasting several seconds should be more than good enough for controlling temperature on most things with a large thermal mass.
Sorry to disappoint, but burst mode definitely isn't PWM.  You are not Modulating the Width of a Pulse, you are controlling On and Off times of a power feed.  In PWM you generally have a fixed frequency drive and you then control the On period within each and every cycle, whilst in burst mode you switch On for several (or many) cycles and then switch Off for several (or many) cycles  -  quite different from PWM.  That's why I described it as a "form" of PWM, I was using a metaphor to explain the difference to someone who is learning about temperature control via PID.

Jiggy-Ninja

Sorry to disappoint, but burst mode definitely isn't PWM.  You are not Modulating the Width of a Pulse, you are controlling On and Off times of a power feed.  In PWM you generally have a fixed frequency drive and you then control the On period within each and every cycle, whilst in burst mode you switch On for several (or many) cycles and then switch Off for several (or many) cycles  -  quite different from PWM.  That's why I described it as a "form" of PWM, I was using a metaphor to explain the difference to someone who is learning about temperature control via PID.
Except there isn't a difference. You don't need a metaphor to compare them because they're exactly the same thing.

You say with burst mode it's on/off for "several cycles" each. What cycle time you are using? The system designer has total freedom to decide how long or short the PWM period is. My previous job was validating electric power steering modules for a Tier 1 supplier. The thermal chambers we used had a PWM period of 10 s for the temperature and humidity controls, and the motors in the steering modules themselves used a 40 us period (25 kHz) into the motor's H-bridges. Even with a 250,000 fold difference in frequency, one is not any more or less PWM then the other.

The EPS motor needed very snappy responses, so it required a fast PWM frequency. Thermal systems are much much slower, so they can get away with much slower PWM frequencies as well.

PWM doesn't stop being PWM just because the frequency goes below some arbitrary value. I need to bring this out for emphasis:
Quote
you are controlling On and Off times of a power feed
That is what PWM is.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

jpill

#9
Nov 03, 2016, 01:14 am Last Edit: Nov 03, 2016, 01:23 am by jpill
Hi.

I appreciate all the coments about this topic; the theory shows PID a few hard to implements, but in real life with PID library is not that hard, the thing is how to obtain the constants (kp, ki, kd), to this i will check with many values.

In my case the SetPoint (temperature) comes from user, so is always different, is this an inconvenient or what changes in PID implementation.

Also input is a sensor value.
PID myPID(&input, &output, &temperature, kp, ki, kd, DIRECT);

Thanks.

allanhurst

If you're keen it's time you learnt some control theory....

Try wikipedia for a start...

regards

Allan

jremington

Quote
the thing is how to obtain the constants (kp, ki, kd),
Google "pid tuning" for standard approaches and tutorials.
No PM's please.

Go Up