Before we write a single line of code, let’s demystify the acronym.
Imagine you are driving a car. You want to maintain a speed of 60 mph (the Setpoint). Your foot is on the gas pedal (the Output). The speedometer tells you your current speed (the Process Variable).
The error is simple: Error = Setpoint - Current Speed.
P (Proportional): If you are going 50 mph (error = 10), you press the gas hard. If you are going 59 mph (error = 1), you press gently. This is "proportional" gain. Flaw: It never reaches 60 exactly; it always leaves a small gap (steady-state error). tinkercad pid control
I (Integral): This looks at the past. If you have been going 58 mph for the last 10 seconds, the Integral accumulates that error and pushes the gas pedal a little more to close the gap completely. Flaw: Too much Integral causes "windup" and overshoot.
D (Derivative): This predicts the future. If you suddenly lift your foot, the car slows down. Derivative resists rapid changes. If you are approaching 60 mph very fast, Derivative eases off the gas to prevent overshoot. Flaw: It is sensitive to noise.
The Formula:
Output = (Kp * Error) + (Ki * Integral_Sum) + (Kd * Derivative) Mastering PID Control in Tinkercad: From Simulation to
1. The H-Bridge (L293D) & Motor:
2. The Feedback Sensor (Position):
3. The Target Setpoint (Slider):
class PID public: float Kp, Ki, Kd; float setpoint, input, output; float outMin, outMax;PID(float p, float i, float d, float minOut, float maxOut) Kp = p; Ki = i; Kd = d; outMin = minOut; outMax = maxOut; integral = 0.0; prevError = 0.0; prevTime = 0.0; firstRun = true; float compute(float currentInput) input = currentInput; unsigned long now = micros(); float dt = (now - prevTime) / 1e6; if (firstRun
private: float integral, prevError; unsigned long prevTime; bool firstRun; ;