You are on page 1of 9

8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.

com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50 

 Search 

Skip to Main Content

PID Controller Basics & Tutorial: PID


Implementation in Arduino
1 Nov 2019

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 1/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

Published By JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50


Jeremy S Cook


For those who work in industrial controls, proportional, integral, derivative
(PID) theory
Searchis likely a familiar concept. For most people, the term is entirely
foreign, but you might be more familiar with it than you think. When you press
the gas pedal in a car, you not only consider how far you are from your ideal
speed—the proportional part of PID—you also take into account how your
particular vehicle accelerates as well as conditions like hills.

These more subtle effects are what the I and D terms consider mathematically. In this example, they
would prevent a car's speed from bouncing from an upper to a lower limit, and we can apply the same
concept to a variety of control situations. While limit-based control can get you in the ballpark, your
system will tend to act somewhat erratically.

PID Equation
We can express PID control mathematically with the following equation. P, I, and D are represented by
the three terms that add together here. K p , K i , and K d are constants that tune how the system reacts
to each factor:

We can also replace K i and K d with 1/T i and T d , respectively. This change gives the equation a better
relationship to its physical meaning and allows the units to work out properly to a unitless number:

We can also transpose the equation to extract the K p value and apply it to the entire equation, in
what's known as the standard form. One advantage of this form is that we can adjust the overall K p
constant for the whole equation at one time:

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 2/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50


Search

All of this may look a bit intimidating, perhaps even to someone who graduated with an engineering
degree. The good news is that you don't have to dig out your Modeling and Analysis of Dynamic
Systems textbook to understand what's going on here. And while it never hurts, you don't even have to
be able to do calculus.

Breaking the first equation down, we produce u(t)—the unitless controller output on the left-hand
side of the equation—by adding three mathematical elements on the right-hand side of the equal sign:
P, I, and D. Each element has a constant K value in front (K p , K i , and K d ), which signifies each element's
weight as they form u(t), or the control output at a specific time. We can individually tune each K value
for better system performance, which we'll explain further below:

Proportional (P—K p )

The first and most crucial term in this equation is the e(t). As such, the K p value that comes before it is
generally larger than the other K values in the equation. Here, e(t) is simply the instantaneous error at
a point in time—the actual value of a controlled device minus the desired value. Multiply this by K p
and you have its contribution to the overall controller output.

Integral (I—K I )

The second term in this equation has to do with the combined error over time. It's the sum of all errors
experienced on the device:

Each time a controller calculates u(t), it adds the instantaneous error to a running tally.

This figure is then multiplied by K i and added into u(t). 

Consider a situation where a force holds a motor in place and doesn't allow it to return to the setpoint
under normal conditions. As it's out of spec longer and longer, the I term will continue to increase,
eventually overcoming this force or reaching the limits of the motor's capability.

Derivative (D—K d )

The third term in this equation has to do with how fast the error changes:

Theoretically, if you only had a D term in this equation and your process steadily maintained the wrong
value, this term would remain zero and wouldn't contribute to the proper output. On the other hand, if
one of the other two terms tries to make the device's output snap back into place too quickly, the
Agent Offline
derivative can help dampen the effect.
https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 3/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

While the calculus-based expression of this concept can be useful, the reality of PID control is much
JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50
less magical than it first appears. Practically speaking, we calculate output using only addition,
subtraction, and multiplication, factoring in the error and time between readings. In fact, we used 
pneumatic systems to implement early forms of PID control, using mechanical means to input each 
"term."
Search
1. Controllers take an instantaneous error reading.

2. Subtract the previous instantaneous reading from it.

3. Multiply that resulting valueby K d to calculate its contribution to u(t).

Arduino PID Controller Tutorial


In many situations, it's expedient to plug in a dedicated PID controller to your process, but you can
make your own with an Arduino or other similar dev board. You can even write your own PID routine.
Express each term in your code in a manner similar to:

- P:instanteneousError = setpoint – input;

- I:cumulativeError = += error * elapsedTime;

- D:rateOfError = (error – errorLastCalculation)/elapsedTime;

To get the necessary output, multiply each of these terms by its respective K value and add them
together. On the other hand, if you'd like to avoid reinventing the wheel, you can jump in with Brett
Beuregard's PID library. This library takes care of the details for you and lets you focus on tuning P, I,
and D constant values as needed.

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 4/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50


Search

To demonstrate PID theory in action, I pulled out an Arduino Nano Every from my toolbox, along with:

- Motor driver board

- Infrared sensor

- Motor salvaged from a Hubsan H107C drone

I printed a breadboard mount for the motor along with a "fan" that blocks light 50% of the time to
avoid missing pulses as it spins. The motor driver is the input to the (otherwise uncontrolled) motor,
and feedback is based on the time between pulses.

When I finally worked out the code and obtained the 3D-printed parts, I was able to control the motor
in a more sophisticated way than operating the power button with constant PWM (pulse-width
modulation) output.

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 5/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50


Search

Code snippet from the PID routine linked above

Notable bits of code include using an interrupt routine to register each pulse as the blocking device
spins and calculating the time between pulses using millis(). I set up the PID in the first part of the
code outside of the functions with:  Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 6/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50
PID myPID(&difference, &driverOut, &setPoint,Kp,Ki,Kd, DIRECT);.

Kp, Ki, and Kd are defined before this line in the code, and &difference controls &driverOut, with

&setPoint modified by the serial monitor. We can manipulate this math to transform this time
Search
difference into RPM, but the PID settings work out as DIRECT outputs because this particular driver
turns on when the input is pulled low. As the analog output goes higher (motors off), the delay
between pulses also increases.

An earlier version of PID routine. Note CH1 reads input pulses, while CH2 is output calculated using PID
routine—low pulses are inputs to motor.

This simple PID controller example was driven by parts on-hand (including the new Arduino Nano
Every) and a motor with which I was loosely familiar. But I was able to demonstrate how to apply PID
control in a wide variety of situations, even without all the facts. Whether it's motor control,
temperature control of an oven, or balancing a robot, a little PID setup and tuning gives you endless
experimentation options.

   

L AT E S T N E W S  Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 7/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50


Search

Solutions for all functions of a surveillance camera system


Protecting your property is crucial for true peace of mind. Advancements in…

5 days ago

Aerospace and defense


Learn about Abracon components for aerospace and defense applications.

7 days ago

Molex's Solutions for Aerospace & Defense Applications


Explore the trusted solutions Molex offers for aerospace and defense desi…

7 days ago

Connect with Electronic Components

   

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 8/9
8/28/23, 4:40 PM PID Controller Basics & Tutorial: PID Arduino Project | Arrow.com

JOIN ARROWPERKS AND SAVE $50 OFF $300+ ORDER WITH CODE PERKS50

Contact Us 

Search

Businesses
Electronic Components

Enterprise Computing Solutions

Intelligent Solutions

About
Careers

Company

Investor Relations

Newsroom

Connect with Arrow Global

   

Privacy Policy

Terms and Conditions

Cookie Policy

© 2023 Arrow Electronics, Inc. All rights reserved

 Agent Offline

https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino 9/9

You might also like