Today is...
Monday, June 25, 2018
Welcome to, the global online
community of automation professionals.
Featured Video...
Featured Video
EtherCAT with CTC’s master lets your multivendor network play well together...
Our Advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive
PID control using C!!!
I need help in designing the PID controller

Hello everybody,
I am involved in developing a PID controller in C language for a thermal control module. I do not know how to start or where to begin. Can any of you help me out with the design procedure.


By Hikmet Balci on 20 August, 2002 - 3:14 pm


I think the starting point is to write the famous PID equation in z-transform. You can find it in the book "Digital Control of Dynamic Systems" or you can use bilinear transformation to write it down. The second point is to digitize the thermal output. You can achieve it by using an I/O card from National Instrument. So, everthing is in the digital domain, it remains to write the code in C.


> Hello everybody,
> I am involved in developing a PID controller in C language for a thermal control module. I do not know how to start or where to begin. Can any of you help me out with the design procedure.
> regards
> Sajith.P.V

Hi Sajit,
I have a program in fortran that uses P, PI controls, no Derivative using coehn-coon settings.
This program is for a double heat exchanger. though the process temperatures reach set point in a minute for my settings, it depends on other process variables.

e-mail me if you want to have this code.


By Thomas Hergenhahn on 20 August, 2002 - 3:20 pm

1. Calculate difference of setpoint and actual value:

2. Sum up for integral part:
You may want to set sum to zero if controller is disabled:
if (0==enabled) sum=0;
You may want to check the sum for limits to prevent numerical underflow.

3. Calc change of diff for derivative part:
4. Now Combine P,I and D parts:
You may want to check output against limits which are reasonable for the controlled device,
e.g. 0-100 percent.
Make sure the above function is carried out periodically.

Hi Thomas,
Thanks for the reply. But I'm afraid I still am not any clearer. I thought the design of the PID controller would involve obtaining the transfer fuction of the process and the application of design techniques such as the bode plot and so on.

My task involves the control of thermal proceses(5 in number) with independent PID controls. Will the code you sent me help in achieving this task?

Kindly let me know, and thanks once again for your reply.

Hi Sajith,
I have done a project on PID temperature control using C. Here are some simple pid equation

error = setpoint - current reading
Ppart = error * kp;
if ((Ppart >0 && out <max out )||(Ppart < 0 && out >min out ))/**antiwindup condition**/

Ipart= Ipart + ki*error*deltaT;
Dpart= (kd *(error - lasterror ))/deltaT;
out = Ppart+Ipart+Dpart; //total output
lasterror = error

error = setpoint - current reading
lasterror is the value of the previous error
and deltaT = sampling time or time interval between performing the pid function.
You can either perform Zigler Nichols open or closed loop tuning to get the Kp,Ki and Kd values....that would be the tough part of the PID.

Hope this helps.

By Chris Schene on 23 August, 2002 - 1:06 pm

I also use IMC Control and Cohen-Coon rules. I find that sometimes the easiest way to calculate the P, I, and D gain constants is straight from a second order characteristic equation by choosing a damping ratio close to 1, but a little bit over (slightly over damped). If you understand basic feedback control theory, it should be no problem. If you don't I will explain to you how you do it (it's a little involved…but after doing it once, just plug it into a spread sheet).

Anyway, once I get the constants from setting a theoretical damping ratio I set that and then fine-tune the controller by hand…since the theory is always off a little and I need to take into account noise effects in practise.

By the way, what type of temperature process are you controlling? Most temperature control processes I have seen do not need a "D" term. I would avoid the "D" term if not needed, since it makes the loop harder to tune and more sensitive to noise. Nearly all the time I have a "D" term, I need to add additional filtering for noise as well.

Chris Schene
Anadigm, Inc "The FPAA Company"

hello Chris,
Thanks a million for your suggesions. My task is the developement of a software PID controller running on QNX RTOS platform.

Could you please elaborate on the design methodology that you mentioned.

Thanks in advance!!!
Best Regards.

By Chris Schene on 1 September, 2002 - 11:23 am

Hello sajith,

Actually I think I was getting a little bit ahead of where you are in your design and analysis process. What I described to you are approaches to TUNING the PID controller. Tuning a PID controller is actually quite a straightforward and mechanical process that is largely independent of the platform you are implementing it on. The fact that you are implementing in "C" is almost immaterial to the design of your controller. A PID controller is basically a second order differential equation with a number of practical variations make it useable for a real process or particular vendors' hardware set. Don't make it any harder than that.

The HARD work is establishing the parameters, limitations, and deriving an adequate mathematical transfer function of your plant. Bode plot analyses, pole zero analysis, gain phase margin analysis etc are of little value until you have an adequate process description and plant model (transfer function). Once you have a plant model, there are a number of cook-book type of approaches you can use to tune your controller. A few of those are: Cohen-Coon tuning, Zieglar Nichols, IMC.

It sounds like you need help defining your plant model. The approach I use for obtaining my model is to do step testing and then use basic system identification techniques (SYSID) to derive my plant model. This is much easier than it sounds for most temperature control processes, since many temperature control processes can be approximated as first order exponentials (K/Ts+1) with a phase delay.

Do you need some guidance in developing a plant model?

Chris Schene
Anadigm (the FPAA company)

Hi Chris,
Thanks a lot for your response. I guess I did not make myself clear. And yes Chris, i do need help developing a plant model?

By Chris Schene on 4 September, 2002 - 4:13 pm

> Hi Chris,
> Thanks a lot for your response. I guess I did not make myself clear. And yes Chris, i do need help developing a plant model?


It would probably take a long time for the dialog if we discussed your design just on the message board (turn around time is quite long). If you email me at:


I can probably help you much faster this way. I'll post the pertinent points of our dialog, as it might be useful to others. I am in the process of creating a temperature control "plant" so the dialog will be useful for me as well. Most temperature control plants will have an exponential type response, sometimes with a delay, to a step input.

I assume that you understand basic engineering mathematics (Laplace, ODE's, etc). Is this correct?

Some preliminary question

1) Do you already have the process set up, or are you creating everything from scratch?
2) Are you heating or cooling. With what type of device? What are temperature ranges? How tightly must you control the temperature?
3) How are you measuring the temperature? Thermocouple? Thermister? RTD? Other? Do you need to linearize the temperature measurement?
4) What are you controlling the temperature of?

Send your answers to these question to me and I will probably come back with some more.

Chris Schene

Hi Ranjit,
I had followed ur wonderful discussion on PID using C, that raged on for many days. Thanks for the tip.

I'm in the beggining of the design of a PID controller for automatic control of thermal modules. Will your code help me in achieving my goal? Can you mail me the details regarding your project work.

My i.d is

Best Regards,

Hi Sajith,

Writing a PID code for theoretical uses is a simple matter. You have standard PID equation where you give three factors for P (either
proportional band or gain), I (integral time or repeats per minute) and D (derivative time or inverse of that) and use it with integral and derivative functions.

But how you PID behaves entirely depends on how you have made your algorithm in your software. I have seen a number of controllers from reputed
manufacturers and have found that the P,I and D factors of one controller does not work in another controller for the same application. Sometimes, the second controller is not able to control as well as the first. This could be
basically due to the way the algorithm has been written or the way the algorithm is executed by different CPU's. PID control is generally on error
but many a times it is beneficial to use process variables instead of error in the algorithm.

For thermal control, you may need to add dead time also.

Look into a good handbook and first get the equation that suits your needs. uggestions :Liptak handbooks. Considine (spelling may be wrong) Handbook. Or any of several books on PID available. Also check "": for
books/guides etc.

Then make the algorithm.

Then codify that algorithm.

Then get your hardware right.

Check that you are able to send I/O signals to the field (to the final control elements)

Test it.

buy any one of the several controllers readily available in the market (at very economic rates) if that suits your needs.