.st0{fill:#FFFFFF;}

Arduino

Building a PID Controller with Arduino Uno 

 August 11, 2023

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

A PID controller is a sophisticated and elegant control system that is used by engineers, hobbyists, and industry professionals. The beauty of the PID controller lies in its simplicity, coupled with an incredible degree of versatility. To truly grasp the significance of PID controllers, one must first journey into the world of control systems.

The Essence of Control Systems

Imagine you are driving a car and need to maintain a specific speed. Your foot on the accelerator and your eyes on the speedometer create a feedback loop that allows you to adjust the throttle to keep the speed constant. This is a fundamental example of a control system.

Now, think about more complex systems like a spacecraft or a manufacturing process, where the parameters needing control are numerous and interconnected. The challenge becomes more intricate, requiring sophisticated tools and methods. This is where PID controllers come into play.

What is a PID Controller?

PID stands for Proportional, Integral, and Derivative, representing the three core components of this controller.

  • Proportional (P): The Proportional term produces a control output that is directly proportional to the error, which is the difference between the desired value (setpoint) and the current value (process variable). The controller’s output is large if the error is large, and vice versa.
  • Integral (I): The Integral term considers the history of the error. If the error has been present for an extended time, it will accumulate, and the controller will respond by changing its output in relation to a summed error over time.
  • Derivative (D): The Derivative term predicts future error. It provides a control output to counteract the rate of error change. The faster the error changes, the more significant the controlling action.

Why is PID So Pervasive?

PID controllers offer a perfect balance of simplicity and efficiency. By merely tuning three parameters (Kp, Ki, Kd), PID controllers can be adapted to control a wide variety of systems.

Typical Uses of PID Controllers

PID controllers are widely used in various fields, including:

  • Temperature Control: In industrial processes, maintain a consistent temperature.
  • Motor Control: In robotics and machinery, to control the speed and position of motors.
  • Flight Systems: In aviation, to maintain the stability and control of aircraft.
  • Economic Systems: To regulate macroeconomic variables such as interest rates.
  • Healthcare Equipment: In devices like infusion pumps to control the flow rate.

Implementing a PID Controller with Arduino Uno

To implement a PID controller on Arduino Uno, you will need the following:

  • Arduino Uno
  • LED
  • Momentary button
  • Resistors (330 Ω for the LED and 10 KΩ for the button)
  • Breadboard and wires

Wire the LED so that it turns on and pin 9 is HIGH, and the button so that pin 2 reads HIGH when pressed.

Arduino Code for PID Controller

Here’s an example PID sketch for the Arduino Uno. In this sketch, I’m using the PID library by Brett Beauregard.

#include <PID_v1.h>

#define BUTTON_PIN 2
#define LED_PIN 9

double setpoint, input, output;
double Kp = 2, Ki = 5, Kd = 1;

PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  setpoint = 100;
  myPID.SetMode(AUTOMATIC);
}

void loop() {
  input = analogRead(A0);
  myPID.Compute();
  analogWrite(LED_PIN, output);

  if (digitalRead(BUTTON_PIN) == HIGH) {
    setpoint = 200;
  }
}

The sketch first defines the required parameters and initializes the PID object. The setup function configures the button and LED pins and activates the PID controller. The loop function reads the input, computes the PID output, and adjusts the LED brightness accordingly.

Here’s a detailed line-by-line explanation of the given Arduino sketch implementing a PID controller:

  • Line 1: #include <PID_v1.h> includes the PID library, which contains the functions required to create and manage a PID controller.
  • Line 3: #define BUTTON_PIN 2 assigns pin 2 to a constant called BUTTON_PIN, representing the button’s connection.
  • Line 4: #define LED_PIN 9 assigns pin 9 to a constant called LED_PIN, representing the LED’s connection.
  • Lines 6-7: Declare double variables setpoint, input, output, and PID coefficients Kp, Ki, and Kd. These will be used to store the setpoint value, the current input, the calculated output, and the PID tuning parameters.
  • Line 9: PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT); initializes the PID controller, passing pointers to input, output, and setpoint, along with the PID coefficients, and the direction (DIRECT).
  • Lines 11-15: void setup() is the setup function that runs once when the Arduino is powered on or reset.
    • pinMode(BUTTON_PIN, INPUT); configures the button pin as an input.
    • pinMode(LED_PIN, OUTPUT); configures the LED pin as an output.
    • setpoint = 100; initializes the setpoint value.
    • myPID.SetMode(AUTOMATIC); activates the PID controller.
  • Lines 17-24: void loop() is the main function that runs repeatedly.
    • input = analogRead(A0); reads the analog value from pin A0 and assigns it to input.
    • myPID.Compute(); computes the PID output based on the current input, setpoint, and PID parameters.
    • analogWrite(LED_PIN, output); writes the computed PID output to the LED, controlling its brightness.
    • if (digitalRead(BUTTON_PIN) == HIGH) { setpoint = 200; } checks if the button is pressed and changes the setpoint to 200.

In summary, this sketch uses the PID library to implement a PID controller that reads an input value from an analog pin, computes the PID output, and controls the brightness of an LED. The setpoint can be changed by pressing a button, demonstrating how a PID controller can adapt to changes in the setpoint.

When External Interrupts Might Be Better

In some cases, external interrupts may be preferable to internal timer interrupts. External interrupts can provide an immediate response, whereas internal timers might have a slight delay. This consideration might be essential in applications requiring precise timing.

Expanding Your Project with Other Hardware

Beyond the basic setup, one can experiment with various sensors or actuators, such as temperature sensors or motors. Integrating these components allows you to create more complex systems utilizing PID control.

Commercial PID Hardware and Software: An Overview

Commercial PID controllers have become a vital tool in various industries, offering precision and efficiency in controlling systems. Here, we will explore the fundamental characteristics of commercial PID hardware and software and provide examples of popular brands and models.

Hardware Characteristics

PIDs that are common on the market have certain hardware characteristics. Here is a short list:

  1. Input/Output Configuration: Commercial PID hardware offers various input and output options for sensors and actuators. Common input types include thermocouples, RTDs, and voltage inputs.
  2. Display and Interface: Many controllers offer a user-friendly interface with LCD or LED displays, allowing easy monitoring and adjustments of parameters.
  3. Modularity and Scalability: Some models allow for expansion, providing flexibility in applications that require additional input/output channels or communication interfaces.
  4. Robust Construction: Commercial units are typically built to withstand harsh industrial environments and conform to various safety and quality standards.

Software Characteristics

And here is a list of software characteristics that are common in commercial PID systems:

  1. Ease of Configuration: Modern PID control software allows for intuitive configuration and tuning of controllers, with graphical interfaces to visualize the process.
  2. Integration with Other Systems: Commercial PID software often provides seamless integration with PLCs, SCADA systems, and other industrial automation tools.
  3. Real-Time Monitoring and Analytics: Advanced software solutions offer real-time data tracking, trend analysis, and diagnostic features, helping optimize performance.
  4. Security and Compliance: Industrial-grade software often includes robust security features and compliance with relevant industrial standards.

Examples of Hardware Brands and Models

PIDs from Honeywell, Siemens and Omron are the most recognisable in the market. Here are some models to look at if you are interested in taking a closer look:

  • Honeywell: Models like the Honeywell UDC3200 offer a variety of features, including universal inputs and outputs and Ethernet connectivity.
  • Siemens: Siemens provides robust controllers like the SIMATIC series, known for their reliability and integration with various industrial automation systems.
  • Omron: The E5CC series from Omron features precise control and user-friendly interfaces and is commonly used in temperature control applications.

In conclusion…

PID controllers are essential to modern technology, offering precise control over various systems. By understanding and implementing a PID controller with Arduino Uno, you can gain practical insight into this fascinating and widely applicable tool.


Tags


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Understanding the power requirements is crucial for any enthusiast using the Arduino Uno in their projects. How you manage voltage, amperage, and power sources impacts the success or failure of your endeavours. In this guide,

Read More
The Ultimate Guide to Powering Your Arduino Uno Board

If you are curious about how electronic devices work, understanding the fundamentals of the operation of their basic components is essential. Every electronic device—from the smartphones in our pockets to the satellites orbiting our planet—contains

Read More
A Guide to Essential Electronic Components in Circuits