Implementing PID control using a microcontroller

Thread Starter

Mitchy190

Joined Apr 23, 2012
33
Hey all! :D

I am starting a new project and would like to interface a 'temperature sensor' to a 'micro' which will then control a heat source, such as a heat mat.

The user will be able to set the temp via buttons or maybe a pot but the main interface, to the user, will be an LCD.

I would eventually like to be able to set different set temperatures automatically depending on the time of the day, for example the set temp will be lower at night, I would like this all to be done via buttons and using the LCD.

I have some experience in C, java and vhdl but I have never programmed a micro, and I don't know where to start, I also do not know how to implement a PID algorithm in code.

I have studied the mathematical algorithm, but don't have a clue how to implement it, and the ones I have seen in code do not make much sense to me.

> any suggestions on the micro to use?
> " " " temp sensor to use
>How do I implement the PID algorithm?
>And any ideas on the automatic set time?

Any help will be much appreciated!

PS - I am actually using this system to control the temperature of a snake tank in which the temp should be around 29c.
 

MaxHeadRoom

Joined Jul 18, 2013
28,702
The basics of tuning a PID loop in a temp controller is to start to cycle on off heating periods as the temp approaches the set point.
And this is often by experimentation.
You will also need to implement a real time clock, there are quite a few already made to interface to the PIC based around the PCF8583 or you can implement your own.
Max.
 

joeyd999

Joined Jun 6, 2011
5,287
Doesn't your local Home Depot or ? sell an exact unit with all those requirements? Made by Honeywell? ;)
Max.
The Home Depot thermostats are typically "bang-bang" (hysteretic) controllers, not PID. I did build a PID controller that I mentioned in this thread:

http://forum.allaboutcircuits.com/showthread.php?t=64095

Though I didn't give any real technical details about it. If OP wants to ask some very specific questions, I could help.
 

MaxHeadRoom

Joined Jul 18, 2013
28,702
The Home Depot thermostats are typically "bang-bang" (hysteretic) controllers, not PID..
Not quite true.
If it is the mercury/bi-metal, yes, but the one I have claims the ability to control to +/-1°F I don't see that possible with a simple Bi-Metal?
It starts to cycle as the Temp approaches the set point.
Max.
 

MrChips

Joined Oct 2, 2009
30,824
Temperature control via PID on a microcontroller is perfectly doable.
Start off by completing a system design.
What inputs and outputs are required, pushbuttons, LCD displays, RTC, PWM, etc?
 

crutschow

Joined Mar 14, 2008
34,470
An alternate to PID is to use a Fuzzy Logic loop. It uses a series of If-Then-Else statements for the control and is often easier to understand and tweak for stable operation than is a PID loop, which is basically a digital simulation of a control loop consisting of analog integrators and differentiators.
 

THE_RB

Joined Feb 11, 2008
5,438
...
PS - I am actually using this system to control the temperature of a snake tank in which the temp should be around 29c.
PID is of use when the system needs to respond to large/fast changes in load, or needs to produce fast changes in temperature.

Your application will heat a volume to a nice stable temperature, then maintain it at that fixed temperature forever. PID is totally not needed, you just need a setpoint controller (like a simple comparator) and put the sensor reasonably close to the heating element to reduce cycling delays.
 

crutschow

Joined Mar 14, 2008
34,470
PID is of use when the system needs to respond to large/fast changes in load, or needs to produce fast changes in temperature.

Your application will heat a volume to a nice stable temperature, then maintain it at that fixed temperature forever. PID is totally not needed, you just need a setpoint controller (like a simple comparator) and put the sensor reasonably close to the heating element to reduce cycling delays.
A more elaborate feedback controller could be needed if there is a large thermal lag in the system, which could cause significant overshoot in the system temperature.
 

alexfreed

Joined Oct 8, 2012
72
First of all with no microcontroller experience Arduino may be a good platform: inexpensive, programmable in "C" and easy to interface. If the heater is 120V powered, a triac will be a good control element plus an optocoupler for safe connection to the uC. If the heater is DC powered low voltage type, a power MOSFET will be good.

As for the program, I would first try an experiment. Use PWM (no need for high frequency for a heater) to drive the heater at various duty cycle and determine at what duty cycle the temperature is stable at the desired level. At "normal" room temperature. So the program will drive the heater at 100% while the temperature is say 10° less than desired, off if more than desired and scaled from 100% to the found constant temp duty factor between Tx - 10 and Tx. Maybe 100% all the way to within 2°. See how it works.
 

John P

Joined Oct 14, 2008
2,026
Actually a domestic thermostat isn't totally a "bang bang" system. It has a heating element called an "anticipator" which warms the actual switching element when the power is on. The idea of this is that the device which delivers the heat has a certain thermal mass, and once it's hot, it stays warm (effectively "on") for a while. Thus the actual heating power needs to be turned off a little early, or you'd see the temperature rise above the set point. It's a simple device, but it lets most heating systems run without much fluctuation in temperature. How it makes things look in terms of conventional servo math (which I don't understand anyway) I don't know.
 

MrChips

Joined Oct 2, 2009
30,824
While a bang-bang control system is perfectly suitable for domestic temperature control, application of PID control techniques to such an application is an excellent introduction to learning about PID control.

As suggested in my previous post, start by building all the peripheral devices necessary, buttons, display, temperature sensors, PWM output, etc. The PID control comes last.

As Alex suggested, the PID design begins by setting the PWM output from 0 to 100% and studying the behaviour of the the system, i.e. the final temperature attained with the given load conditions.

I have built such a system for TEC control and have found that PI control was sufficient. The derivative component was not required.
 

alexfreed

Joined Oct 8, 2012
72
Greatly simplified the PID system looks like this. The main term in the control equation is P, that is "proportional". The power applied to the heater is proportional to the difference between the desired and measured temperature. In many cases this is all that is needed. If something changes quickly in the environment, say a window is opened, a proportional system may take a long time to return to the right value. But the I integral term will "see" that the gap is still there over time and increase the power accordingly. The right temperature will be reached faster, but in many cases will overshoot and not settle for a while. The D derivative term to the rescue: it will "see" that we are reaching the right value quickly and reduce the power before we overshoot. So start with a simple proportional system and see how it behaves. Chances are that will be enough.
 

Thread Starter

Mitchy190

Joined Apr 23, 2012
33
Thank you all your replys!

I am now going to figure out and get all of the hardware I need, I am thinking of using the PIC18f4550 or one from that range, they seem like a good all rounder, plus you can program it in 'C'.

However I cannot just pick a random LCD I like the looks of can I? I've seen one I like but to active a logic high the voltage needs to be something like 7volts which im pretty sure the PIC isn't capable of outputting? I may have this concept wrong?

But anyway I will show the progression of the project!

And yes i am aware that there is probably a simpler way to implement my goal, but hey! I've studied control theory but never implemented such a device and I'm interested; thought this would a good place to start :) i will also test my system out and see how it reacts to disturbances etc, so a full PID would be cool to implement.

Good explanation alexfreed :)
 

GetDeviceInfo

Joined Jun 7, 2009
2,196
often, for such implementation, a variable duty cycle will suffice. Turn up your dial for a longer on duty, and you get some resultant heat, with the user regulating via the dial.

If you check atmel's site you'll find PID code for the AVR.
 

GopherT

Joined Nov 23, 2012
8,009
From Wikipedia, PID controller...
A lot to read but if you scroll to the bottom, you see the following very simple Psudocode. 10 lines and you are done. Now you just need to set coefficients to show the importance of proportional, integral and differential.

previous_error = 0
integral = 0
start:
error = setpoint - measured_value
integral = integral + error*dt
derivative = (error - previous_error)/dt
output = Kp*error + Ki*integral + Kd*derivative
previous_error = error
wait(dt)
goto start
If you measure your temp and run your PID at the same interval each time, then you can ignore the change in time (dt) factor and eliminate the divide and multiple (just assume each update is one time unit so it can be ignored).

If you update too frequently, the derivative and integral terms become moot.

Also, unless you plan to use some type of temperature sensor and PWM control with varying duty cycle, the whole idea of PID is moot as well.
 
Last edited:
Top