Implementing PID control using a microcontroller

Discussion in 'The Projects Forum' started by Mitchy190, Oct 21, 2013.

  1. Mitchy190

    Thread Starter Member

    Apr 23, 2012
    33
    0
    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.
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    Doesn't your local Home Depot or ? sell an exact unit with all those requirements? Made by Honeywell? ;)
    Max.
     
  3. Mitchy190

    Thread Starter Member

    Apr 23, 2012
    33
    0
    Haha probably! But I want a project to do and the satisfaction of making one myself!
     
  4. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    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.
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,730
    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.
     
  6. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    f you are new at micros I'd suggest starting by looking at what others have already done and using their examples as a starting point in learning where to go with this project.

    Look over a few existing implementations, then choose a microcontroller based on whichever one looks like the best one for your skills and interests.

    Here's an example:

    http://forums.parallax.com/showthread.php/77656-PID-Control-Intro-with-the-BASIC-Stamp
     
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,534
    2,369
    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.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    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?
     
  9. crutschow

    Expert

    Mar 14, 2008
    13,004
    3,232
    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.
     
  10. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
  11. crutschow

    Expert

    Mar 14, 2008
    13,004
    3,232
    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.
     
  12. alexfreed

    Member

    Oct 8, 2012
    72
    10
    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.
     
    Mitchy190 likes this.
  13. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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.
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    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.
     
    Mitchy190 likes this.
  15. alexfreed

    Member

    Oct 8, 2012
    72
    10
    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.
     
    lasercowboy likes this.
  16. Mitchy190

    Thread Starter Member

    Apr 23, 2012
    33
    0
    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 :)
     
  17. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    What LCD requires 7 volts? I don't have tons of experience with them, but all that I have are 5 volt compatible.

    A quick look at Jameco shows one 6.5V and one 7V; all the others are 5V. I would stay with that.

    http://www.jameco.com/Jameco/catalogs/c133/P31.pdf
     
    Last edited: Oct 22, 2013
  18. Mitchy190

    Thread Starter Member

    Apr 23, 2012
    33
    0
  19. GetDeviceInfo

    Senior Member

    Jun 7, 2009
    1,571
    230
    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.
     
  20. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,031
    3,800
    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.

    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: Oct 23, 2013
Loading...