Introduction to PID Control
Proportional-Integral-Derivative (PID) control is a popular control algorithm used in many engineering and industrial applications. It helps maintain a setpoint by automatically adjusting a system's control inputs, making it effective in various feedback control systems. The PID controller relies on three components:
- Proportional (P) – Adjusts the output based on the current error.
- Integral (I) – Considers the accumulated error over time, helping correct any offset.
- Derivative (D) – Predicts future errors by observing the error rate of change.
PID controllers are used in applications where precise control is needed, such as temperature control, speed control in motors, and stabilization in robotic systems. With the power and flexibility of Arduino, you can easily build a Arduino PID controller for small to medium-scale applications.
How Arduino Can Be Used for PID Control
Arduino can handle the calculations required for PID control and make real-time adjustments to output signals based on sensor inputs. The PID Library by Brett Beauregard is an excellent tool that simplifies the PID implementation on an Arduino.
Components Needed
For this example, let’s create a simple temperature control system where Arduino maintains a set temperature by adjusting the heating element’s power. Here’s what you’ll need:
- Arduino Board (e.g., Arduino Uno or Nano)
- Temperature Sensor (e.g., LM35 or DS18B20)
- Heater (could be a simple resistor or an actual heating element)
- 5V Relay Module (to control the heater’s power)
- Power Source (suitable for the heater)
Circuit Diagram
The following is the circuit diagram for the Arduino based PID Controller controlling a bulb (the heater), by detecting surrounding temperature.
Installing the PID Library
To use the PID library, follow these steps:
- Open the Arduino IDE, go to Sketch > Include Library > Manage Libraries.
- Search for PID by Brett Beauregard and install it or download and install from the following github url:https://github.com/br3ttb/Arduino-PID-Library.
Example Code for Arduino PID Controller with Temperature Control
The following code demonstrates a basic PID control loop using an Arduino to maintain a target temperature. Here, an LM35 temperature sensor provides feedback, while a transistor controls the heating element.
Code
#include <PID_v1.h>
// Define the pins
const int tempPin = A0; // Temperature sensor input pin (e.g., LM35 on A0)
const int heaterPin = 9; // Heater control pin (PWM)
// PID parameters
double setpoint = 25.0; // Desired temperature in Celsius
double input, output; // Variables for sensor reading (input) and control output
// Define PID terms
double Kp = 2, Ki = 5, Kd = 1; // Tune these values as needed
PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
pinMode(tempPin, INPUT);
pinMode(heaterPin, OUTPUT);
// Initialize the PID controller
myPID.SetMode(AUTOMATIC);
}
void loop() {
// Read temperature from the sensor
int sensorValue = analogRead(tempPin);
double millivolts = (sensorValue / 1024.0) * 5000;
input = millivolts / 10.0; // Convert LM35 voltage to temperature (Celsius)
// Run the PID computation
myPID.Compute();
// Apply the output to control the heater
analogWrite(heaterPin, output);
// Print data for monitoring
Serial.print("Setpoint: ");
Serial.print(setpoint);
Serial.print("°C, ");
Serial.print("Temperature: ");
Serial.print(input);
Serial.print("°C, ");
Serial.print("Output: ");
Serial.println(output);
delay(1000);
}
Code Explanation
- Define Sensor Input and Control Output:
input
represents the current temperature, whileoutput
is a control signal to the heater. - Initialize PID Parameters:
Kp
,Ki
, andKd
are the PID gains. These values are crucial in tuning the PID response to reach the setpoint efficiently. - Configure PID: The
PID
objectmyPID
is created with input, output, and setpoint references. The controller is set to AUTOMATIC mode to start controlling. - Read Sensor and Compute Output: The LM35’s analog value is converted to temperature in Celsius. The PID’s
Compute()
function adjusts theoutput
based on the current temperature error. - Control the Heater: The
output
value (0-255) is applied to the heater through PWM. Higher values increase heating power, thus increasing the temperature.
PID Tuning
The key to effective PID control lies in tuning the Kp, Ki, and Kd values. Here’s a basic guide:
- Proportional (Kp): Start with a low value and gradually increase it until the system starts reaching the setpoint but may oscillate.
- Integral (Ki): Helps correct offset; increase this slowly to address steady-state errors. Be cautious, as too high a value may lead to instability.
- Derivative (Kd): Provides damping; increase this if the system oscillates too much. It should be the smallest of the three values in most cases.
Adjust these parameters until the temperature stabilizes around the setpoint without excessive oscillation.
Video Demonstration
Watch how the Arduino works as a PID Controller which controls the blub heating element by reading LM35 temperature sensor data and tries to automatically settle to a preset setpoint value which is 25 degree centigrade in this example.
Applications of PID Control with Arduino
Beyond temperature control, PID control can be applied to various other Arduino projects:
- Motor Speed Control: Use a Arduino PID controller to maintain a specific RPM.
- Balancing Robots: Balance a robot by adjusting motor speeds based on a gyroscope’s angle feedback.
- Line-Following Robots: Maintain a robot on track by adjusting steering based on distance sensors. See Autonomous Robot Car with Arduino.
- Servo Position Control: Control a servo motor to maintain precise angles in robotic arms.
Conclusion
Using an Arduino for PID control is a powerful way to enhance DIY projects with professional control techniques. With some practice in tuning the parameters, you can create a smooth, responsive control system for various applications. Try experimenting with different sensors and actuators to see how PID control can bring more precision to your Arduino projects!