Demystifying PID Control With A Look At The New Arduino PID Library

We’ve been hiding away in air-conditioned comfort to wait out the hot weather afflicting most of the US right now. Luckily we’re keeping busy with the great links coming into our tips box.

[Brett] sent us a note about his work on the new Arduino PID library. He is the author of the original library and recently decided it was time for a ground-up rewrite. But along the way he took the time to explain PID control and the choices he made during development.

We see a lot of PID controllers around here, like this router based espresso machine add-on. Proportional-Integral-Derivative Controllers are a way to make sure the control you intended to get from your devices is actually achieved in practice. They monitor a process and accumulate results over time in order to account for future events. From what we’ve just described you can see why the subject needs to be demystified.

Get yourself elbow-deep into [Brett’s] article. He does a great job of discussing each issue, and uses a multitude of easily understandable graphs to show the hurdles each portion of code is meant to overcome.

18 thoughts on “Demystifying PID Control With A Look At The New Arduino PID Library

  1. I think that Brett has done a great job. Congratulations, it’s a great way to make PID easier.

    Anyway, I’ve always thought that the simplest (simplest final product) way to make a PID is the traditional one (the only shitty thing is that some more (not much) knowledge is required):
    ·Calculate the transfer function knowing what’s your desired maximum error, damping, overshoot, etc… in a continuous system (most easy using Laplace Transform (I repeat, don’t scare, it’s quite easy, a (well) trained monkey could do it))
    ·Convert your transfer function to a discrete one with the method you prefer (MATLAB, paper and pencil…etc)
    ·Convert it to a discrete temporal system, with its delays and all.

    That’s what, in a scenario of a assembler programming for example, will be the simplest solution. Just sums, products, and delays :)

    Anyway I repeat, I like Brett’s job. Thanks for it!

  2. @Stunt21 – I’m currently writing my own PID for a project and that’s exactly how I’m doing it (going the MATLAB route too). I’m going to go out on a limb here and say you, too, were EE trained? Haha.

    This is a great PID lesson though, very informative and I may have to “steal” some ideas for my own project!


    I was just preparing to code my first PID, and this easily the best tutorial I’ve seen. The task looks a lot less intimidating now. I especially love how you show the complete code as it is incrementally changed to accommodate each improvement. Will definitely be reading again and studying in detail.

    This is a real asset to all of us. I would love to see you go further and explore:

    1) Feed forward. It seems fairly simple in concept, but an example in your style of writing would make it crystal clear.
    2) Reset tiebacks. I haven’t heard this one before and couldn’t find anything on it. What is it?
    3) Velocity instead of position. Again, an example would be great.
    4) Different PID forms. Maybe touch briefly on this. Are there any that have notable benefits/drawbacks in any given situation? Having that info would be a good start for individuals to further explore them on their own.

    Integer math is really a topic of its own. Although I’m sure you would do it justice, it’s not specific to PID; and probably adequately explained elsewhere.

  4. @Colecoman1982: That is right for my first point, the continuous (‘s’ or Laplace Transform) transfer function.

    But once you switch it to the discrete (‘z’ transform) transfer function (by using the Residue Theorem on pencil&paper, or any of the functions as ‘c2d’ on MATLAB/Octave on PC), such transfer function “works” (sorry proffessors, but the transform domains involve much math stuff) only on certain discrete values of time, such as iterations of a microcontroller.
    You end up with a ‘Action[k] = 2*Error[k-1] + Error[k]’ thing for your transfer function (‘k’ would be the current iteration), which obviously a microcontroller can solve efficiently :)

    The advantage with this method is that you can use a P, PD, PI, II, PID, or just any other controller you want, because YOU define your own transfer function, whether for (Hey, @Chris!) position, velocity, accelleration…and any with a % of error, or zero error (adding an integral factor (‘s’ or ‘z-1’) to the transfer function).

    I’ve made like 2 systems I can remember using PID, and after having done the calculations, the PID itself didn’t take more than 5 lines in the code… ;)

    I’m leaving for a 20 days backpack trip in some hours. If I can help you with PID, find my email on the link and let me know. Have fun with volts!

  5. Looks good, but although some of the math is too much, it would be interesting to talk about PID tuning and how the different parameters affect control (potentially leading to instability).

  6. @Stunt21: Actually I was trying to make a joke. I was drawing attention to the fact that you called your version of a PID “traditional”. PIDs pre-date digital logic. In fact, according to Wikipedia, they date back to the 1890’s and were mechanical in nature back then and for a long time after. After that, they were, also for a long time, designed using all analog electrical components. In neither case was it necessary to deal with the system in a discreet manner.

  7. Wow, that’s great. I looked into a PID controller for my robot and eventually settled on a kalman filter because I heard arduino couldn’t handle PID. But, now there’s a PID library?! That’s awesome because I could never get my kalman filter working.

  8. Hi everyone, thanks for the positive response.

    @Chris it’s going to be a while before I get to those things. It took me months to refine the library and writeup, and I haven’t even started thinking about how to best explain those topics.

    @George as part of my employment agreement I’m not supposed to give pid tuning advice without my company getting paid. This is also why there is no auto-tune for the pid library. If you’re specifically interested in tunings for your process, try the diy pid control google group. there’s a growing community that should be able to give you some tuning help

    @ed there’s a link to the Arduino library in the first paragraph. that should give you a working c++ library that you can use. Also, if you’re looking for Arduino-specific, there are examples included on that page, and bundled with the library.

  9. QUOTE: “They monitor a process and accumulate results over time in order to account for future events.”

    I think I know what you are trying to say but this is wrong -One huge drawback of a PID control system is that it is a purely reactionary – it doesn’t account for future events, only what has happened. Perhaps you are talking about the derivative term which helps to prevent overshoot but it still doesn’t account for future events.

  10. Interesting link but for those new with the concept I will suggest to start with the excellent paper called “PID without a PhD” by Tim Wescot (first answers by google, prefer the Pdf file here: ) also linked on the wikipedia page for PID.
    Very practical approach, which help me a lot to understand the tuning part for example (for which, the author of your linked page cannot explain much)
    (And sorry for my poor english which is only my third language :-)

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.