Welcome to the AutoTunePID library! This guide will help you get started quickly with basic PID control. Whether you're new to PID controllers or experienced, this guide will walk you through your first implementation.
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries
- Search for "AutoTunePID"
- Click Install
- Download the latest release ZIP from GitHub Releases
- Open Arduino IDE
- Go to Sketch > Include Library > Add .ZIP Library
- Select the downloaded ZIP file
Here's the simplest way to use AutoTunePID:
#include <AutoTunePID.h>
using namespace atp;
// Create PID controller (min output, max output, tuning method)
AutoTunePID pid(0.0f, 255.0f, TuningMethod::ZieglerNichols);
void setup() {
// Set your target value (setpoint)
pid.setSetpoint(100.0f); // Target temperature, speed, etc.
// Optional: Set manual PID gains (skip if using auto-tuning)
pid.setManualGains(2.0f, 0.5f, 0.1f); // Kp, Ki, Kd
}
void loop() {
// Read your process variable (temperature, speed, position, etc.)
// Scale to your units (e.g., 0-100°C)
float currentValue = static_cast<float>(analogRead(A0)) * (100.0f / 1023.0f);
// Update PID controller
pid.update(currentValue);
// Get control output and apply to your actuator
float output = pid.getOutput();
analogWrite(9, static_cast<int>(output)); // PWM output to motor, heater, etc.
delay(100); // Control loop timing
}- Upload the sketch to your Arduino
- Monitor the serial output (add
Serial.println(output);to see values) - Adjust the setpoint and observe how the output responds
- Your basic PID controller is working!
PID stands for Proportional-Integral-Derivative. It's a control algorithm that:
- Proportional (P): Responds to current error
- Integral (I): Corrects accumulated error over time
- Derivative (D): Predicts future error based on rate of change
| Term | Description | Your Code |
|---|---|---|
| Setpoint (SP) | Target value you want | pid.setSetpoint(100.0f) |
| Process Variable (PV) | Current measured value | float currentValue = static_cast<float>(analogRead(A0)) |
| Error | Difference: SP - PV | Calculated automatically |
| Output | Control signal to actuator | pid.getOutput() |
| PID Gains | Control sensitivity | pid.setManualGains(Kp, Ki, Kd) |
Choose based on your application:
| Method | Best For | Description |
|---|---|---|
| Ziegler-Nichols | General purpose | Good starting point for most systems |
| Cohen-Coon | Processes with dead time | Better for slow-responding systems |
| IMC | Fast response needed | Balances speed and stability |
| Manual Tuning | Fine control | When you know your system well |
using namespace atp;
// Heater control (Normal mode)
AutoTunePID heaterPID(0.0f, 255.0f, TuningMethod::ZieglerNichols);
heaterPID.setSetpoint(25.0f); // Target temperature in °C
// In loop:
float currentTemp = readTemperatureSensor();
heaterPID.update(currentTemp);
analogWrite(heaterPin, static_cast<int>(heaterPID.getOutput()));using namespace atp;
// DC motor control
AutoTunePID motorPID(0.0f, 255.0f, TuningMethod::CohenCoon);
motorPID.setSetpoint(1500.0f); // Target RPM
// In loop:
float currentRPM = measureMotorRPM();
motorPID.update(currentRPM);
analogWrite(motorPin, static_cast<int>(motorPID.getOutput()));using namespace atp;
// Servo position control
AutoTunePID servoPID(0.0f, 180.0f, TuningMethod::Manual);
servoPID.setManualGains(1.2f, 0.1f, 0.05f);
servoPID.setSetpoint(90.0f); // Target angle
// In loop:
float currentAngle = readServoPosition();
servoPID.update(currentAngle);
servo.write(static_cast<int>(servoPID.getOutput()));- Try the Examples: Look at
examples/BasicPID/BasicPID.ino - Learn Manual Tuning: Read
docs/manual.md - Understand Auto-Tuning: See
examples/ZieglerNichols/
- Operational Modes: Check
docs/usage.mdfor all control modes - Signal Filtering: Learn about anti-windup and filtering
- System Architecture: Read
docs/system_architecture.md
- Output not responding: Check your actuator wiring and power
- Oscillating wildly: Reduce PID gains or use different tuning method
- No change in output: Verify your setpoint and input ranges
- Examples: Check
examples/folder for complete working sketches - Documentation: See all docs in
docs/folder - Issues: Report bugs on GitHub Issues
Happy controlling!