title: How do I use a PID controller?
body: <p>I'm currently working on a temperature controller.</p>
  
  <p>I have a Temperature_PID() function that returns the manipulated variable (which is the sum of the P, I, and D terms) but what do I do with this output?</p>
  
  <p>The temperature is controlled by PWM, so 0% duty cycle = heater off and 100% duty cycle = heater on.  </p>
  
  <p>So far I tried </p>
  
  <pre><code>Duty_Cycle += Temperature_PID();
  if(Duty_Cycle &gt; 100) Duty_Cycle = 100;
  else if(Duty_Cycle &lt; 0) Duty_Cycle = 0;
  </code></pre>
  
  <p>This didn't work for me because the I term is basically makes this system very unstable.  Imagine integrating an area, adding another small data point, and integrating the area again, and summing them.  Over and over.  That means each data point makes this control scheme exponentially worse.</p>
  
  <p>The other thing I would like to try is </p>
  
  <pre><code>Duty_Cycle = Expected_Duty_Cycle + Temperature_PID();
  </code></pre>
  
  <p>where Expected_Duty_Cycle is what the temperature should be set to once the controller reaches a stable point and Temperature_PID() is 0.  However, this also doesn't work because the Expected_Duty_Cycle would always be changing depending on the conditions of the heater, e.g. different weather.</p>
  
  <p>So my question is what exactly do I do with the output of PID?  I don't understand how to assign a duty cycle based on the PID output.  Ideally this will stay at 100% duty cycle until the temperature almost reaches the set point and start dropping off to a lower duty cycle.  But using my first method (with my I gain set to zero) it only starts lowering the duty cycle after it already overshoots.</p>
  
  <p>This is my first post.  Hope I find my answer.  Thank you stackoverflow.</p>
  
  <p>EDIT:
  Here's my PID function.</p>
  
  <pre><code>double TempCtrl_PID(PID_Data *pid)
  {
      Thermo_Data tc;
      double error, pTerm, iTerm, dTerm;
  
      Thermo_Read(CHIP_TC1, &amp;tc);
  
      pid-&gt;last_pv = pid-&gt;pv;
      pid-&gt;pv = Thermo_Temperature(&amp;tc);
      error = pid-&gt;sp - pid-&gt;pv;
      if(error/pid-&gt;sp &lt; 0.1)
          pid-&gt;err_sum += error;
  
      pTerm = pid-&gt;kp * error;
      iTerm = pid-&gt;ki * pid-&gt;err_sum;
      dTerm = pid-&gt;kd * (pid-&gt;last_pv - pid-&gt;pv);
  
      return pTerm + iTerm + dTerm;
  }
  </code></pre>
  
  <p>EDIT 2:
  Never used this before so let me know if the link is broken.
  <a href="https://picasaweb.google.com/113881440334423462633/January302013">https://picasaweb.google.com/113881440334423462633/January302013</a></p>
  
  <p>Sorry, Excel is crashing on me when I try to rename axes or the title.  Note: there isn't a fan in the system yet so I can't cool the heater as fast as I can get it to heat up, so it spends very little time below the set point compared to above.
  The first picture is a simple on-off controller.
  The second picture is my PD controller.  As you can see, it takes a lot longer for the temperature to decrease because it doesn't subtract before the temperature overshoots, it waits until the temperature overshoots before subtracting from the duty cycle, and does so too slowly.  How exactly do I tell my controller to lower the duty cycle before it hits the max temperature?</p>