I'm an electrical engineer that was kind of thrust into the digital world and learning as I go. I'm programming a TI processor to do a PID (proportional-integral-derivative) loop, illustrated by this diagram:

PID loop image from Wikipedia

I'll also describe it:

Negative feedback op-amp, with non-inverting terminal grounded. Input through negative terminal. The feedback loop is an RE series circuit in parallel with a resistor and all that in parallel with a cap.

Anyone have any idea how to convert this circuit to C code? I'm a bit out of my element on this and could use the help.

  • You can link to a picture and someone will helpfully convert that link into an actual picture for you. Commented Feb 6, 2013 at 16:14
  • 2
    The link you yourself posted provides a basic pseudocode for going about it. If you don't mind C#, here is an example of a pid loop in C#.
    – Neil
    Commented Feb 6, 2013 at 16:14
  • 1
    Neil is right. I've implemented almost exactly that loop in C on TI. One hint: use a constant time loop, and factor the fixed dt into the constants, instead of doing extra divides and multiplies in the loop.
    – AShelly
    Commented Feb 6, 2013 at 16:25
  • 1
    @Neil that was a link that I added in revision 2 because I didn't know what a PID loop was and I suspected many others didn't either.
    – user40980
    Commented Feb 6, 2013 at 16:32
  • @MichaelT, ah my apologies then.
    – Neil
    Commented Feb 6, 2013 at 16:40

1 Answer 1


The Circuit

Ok i just created an account in here when i saw this question. Am not able to edit your question so that i could correct the typo you did. I believe you meant RC series circuit in parallel instead of RE ( if it is, I don't have a single clue what it means)

Seems like the analog circuit you want to simulate using C looks something like this

                  |------| |--------------|
                  |           Rp          |
                  |          Rd    Cd     |
           Rf     |----/\/\/\---| |-------|
Vin o----/\/\/\---|                       |
                  |    |\                 |
                  |    | \                |
                  |----|- \               | 
                       |   \              |
                       |    \-------------|---------o  Vout
                       |    /
                       |   /
                       |+ /
                   ----| /
                  |    |/
               ___|___ GND

  Vin is the input signal.
  Vout is the Output.
  Rp controls the propotional term ( P in PID) 
  Ci controls the Integral term ( I id PID)
  Rd and Cd controls the differential term ( D in PID)
  Rf is the gain control, which is common to all of the above controllers.

(I couldn't resist my urge to draw this since i wanted to tell you how electrical/electronics engineers used to communicate in forums and emails without images ... and why we just love courier, fixed width font)

I must confess that the circuit you are using is simple to setup but is very complex mathematically, when it comes to tune the Propotional, Integral and Derivative constants of the system to a desired value individually its not possible.

I strongly suggest you use the circuit from this source for studying.

Even though a little tedious to set up, mathematically its much simpler to analyse as you can directly relate it to the standard mathematical form instead of ideal one.

Lastly the Vout goes to control a motor or whatever needs to be controlled. And Vin is the Process variable voltage.

Before getting your feet wet in C (sea?)

I assume you are reading the signals from some kind of analogue to digital converter. If not then you would have to simulate the signal as an input.

If using Standard form we have,

Assuming the the loop running time is small enough (a slow process), we can use the following function for calculating output,

output = Kp * err + (Ki * int * dt) + (Kd * der /dt);


Kp = Proptional Constant.
Ki = Integral Constant.
Kd = Derivative Constant.
err = Expected Output - Actual Output ie. error;
int  = int from previous loop + err; ( i.e. integral error )
der  = err - err from previous loop; ( i.e. differential error)
dt = execution time of loop.

where initially 'der' and 'int' would be zero. If you use a delay function in code to tune the loop frequency to say 1 KHz then your dt would be 0.001 seconds.

Drawning in C

I found this excellent code for PID in C, though it doesn't cover every aspect of it, its a good one nonetheless.

//get value of setpoint from user
  // reset Timer
  // write code to escape loop on receiving a keyboard interrupt.
  // read the value of Vin from ADC ( Analogue to digital converter).
  // Calculate the output using the formula discussed previously.
  // Apply the calculated outpout to DAC ( digital to analogue converter).
  // wait till the Timer reach 'dt' seconds.

If we take a slow process, then we can use lower frequency such that dt >>> code execution time for single loop ( far far greater than ). In such cases we can do away with timer and use a delay function instead.

  • 6
    The Ascii Diagram blew my mind. +1
    – TtT23
    Commented Feb 7, 2013 at 5:35
  • 1
    link "this source" is not working
    – Ccr
    Commented Jul 28, 2016 at 11:32
  • Oh am sorry to hear that, a good resource got lost :(. Well the concept has been explained in the while loop example code that i had shared. I am not experienced on how to handle this situation, maybe some editors can fix it with proper message. (the dead link)
    – D34dman
    Commented Jul 28, 2016 at 15:49
  • 2
    The missing "this source" might be available here: educypedia.karadimov.info/library/piddocs.pdf Commented Jul 29, 2016 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.