Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • PID Control

PID Control

Proportional, Integral, and Derivative

A PID control loop involves some contributions from each of the three kinds of control: proportional, integral, and derivative. The amount of contribution that each of the controls makes can be adjusted by changing their proportionality constants, Kp, Ki, and Kd. By making these constants larger or smaller, you can make the contribution of one of the controls more dominant or subtle in the system. One system might need only light integral control, some proportional and strong derivative, while another system might need strong integral and proportional controls, but not much derivative, while still another system might need roughly equal measures of each.

Here are several important things to keep in mind when working with PID control loops:

  • Error is the difference between the level you want and the level that is measured, and control loops work to correct error.
  • Proportional control resists error by applying an opposing influence that is proportional to the error.
  • Integral control detects and corrects trends in error over time.
  • Derivative control detects and resists abrupt changes in the system.

The next example program will perform this PID control loop. To make comparisons easier, Kp, Ki, and Kd are all set to 10.

Example Program – PidAlgorithm.bs2

Keep in mind that proportional always does some work when there’s some error. However, integral and derivative are ready to do extra work to correct the error, integral to correct trends, and derivative to correct abrupt changes.

  • Enter, save, and run PidAlgorithm.bs2.
  • Here is a series of sensor inputs that the integral control will detect as a trend and resist much more strongly than either proportional or derivative: 1 2 3 4 5 5 5 5 5 5 5 5 5
  • Here is a series of sensor measurements that derivative will work hard to correct while proportional and integral don’t do nearly as much: 3 -3 4 -4 5 -5 6 -6 5 -5 4 -4
' PidAlgorithm.bs2
' Demonstrates how a combination of proportional, integral, and
' derivative control influence error correction in a feedback loop.
 
' {$STAMP BS2}
' {$PBASIC 2.5}
 
SetPoint       CON     0                     ' Set point
Kp             CON     10                    ' Proportionality constant
Ki             CON     10                    ' Integral constant
Kd             CON     10                    ' Derivative constant
 
Current        CON     0                     ' Array index - current error
Accumulator    CON     1                     ' Array index - accumulated error
Previous       CON     2                     ' Array index - previous error
Delta          CON     3                     ' Array index - change in error
 
sensorInput    VAR     Word                  ' Sensor input variable
error          VAR     Word(4)               ' Four different types of errors
p              VAR     Word                  ' Proportional term
i              VAR     Word                  ' Integral term
d              VAR     Word                  ' Derivative term
drive          VAR     Word                  ' Output
 
DO
 
  DEBUG "Enter sensor input value: "
  DEBUGIN SDEC sensorInput
 
  ' Calculate error.
  error(Current) = SetPoint - sensorInput
 
  ' Calculate proportional term.
  p = Kp * error(current)
 
  ' Calculate integral term.
  error(Accumulator) = error(Accumulator) + error(Current)
  i = Ki * error(Accumulator)
 
  ' Calculate derivative term.
  error(Delta) = error(Current) - error(Previous)
  d = Kd * error(delta)
 
  ' Calculate output.
  drive = p + i + d
 
  ' Display values.
  DEBUG CR, CR, "ERROR", CR,
        SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR,
        "PROPORTIONAL", CR,
        SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR,
        "INTEGRAL", CR,
        SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR,
        "DERIVATIVE", CR, 
        SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR,
        "OUTPUT", CR, 
        SDEC ? p, SDEC ? i, SDEC ? d, SDEC ? drive, CR, CR
 
  ' Save current error to previous error before next iteration.
  error(Previous) = error(Current)
 
LOOP

 

Your Turn – Tuning the PID Control Loop

Assume that your sensor inputs will range from -10 to 10. Adjust your constants ( Kp, Ki, and Kd ) so that the maximum contribution any of the controls can make to the output ranges fr om 650 to 850. To get the integral control to adhere to this requirement, you will also have to use the MIN and MAX operators.


Printer-friendly version
Derivative Control
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress