Mount this to mimic the motor's position. Connect the outer legs to 5V and GND. Connect the middle wiper pin to Analog Pin A1 . Motor Driver (L293D): Connect Enable 1 to Arduino Pin 9 (PWM). Connect Input 1 to Arduino Pin 8 . Connect Input 2 to Arduino Pin 7 .
The constrain(integral, -100, 100) line is crucial. Without it, if the motor cannot spin fast enough to cool the system down, the integral value grows to infinity, leaving the motor stuck at full speed long after the temperature has dropped. tinkercad pid control
in small increments. This forces the steady-state error to zero, locking your system precisely onto the target. Summary of Results ✅ Successful Emulation Mount this to mimic the motor's position
// Pin Definitions const int sensorPin = A0; const int motorPin = 3; // PID Tuning Parameters (Constants) double Kp = 4.0; // Proportional Gain double Ki = 0.5; // Integral Gain double Kd = 1.0; // Derivative Gain // PID Variables double setpoint = 40.0; // Target temperature in Celsius double input, output; double error, lastError; double integral, derivative; // Timing Variables unsigned long lastTime; double dt; // Change in time void setup() Serial.begin(9600); pinMode(motorPin, OUTPUT); lastTime = millis(); void loop() // 1. Calculate time elapsed (dt) unsigned long now = millis(); dt = (double)(now - lastTime) / 1000.0; // Convert to seconds if (dt >= 0.1) // Run the PID loop every 100ms // 2. Read sensor and convert to Celsius int rawAnalog = analogRead(sensorPin); double voltage = rawAnalog * (5.0 / 1023.0); input = (voltage - 0.5) * 100.0; // 3. Calculate Error error = setpoint - input; // 4. Calculate P, I, D Terms double pTerm = Kp * error; integral += error * dt; // Constrain integral to prevent windup integral = constrain(integral, -100, 100); double iTerm = Ki * integral; derivative = (error - lastError) / dt; double dTerm = Kd * derivative; // 5. Compute Total Output output = pTerm + iTerm + dTerm; // 6. Restrict output to Arduino PWM limits (0 - 255) output = constrain(output, 0, 255); // 7. Write output to motor analogWrite(motorPin, output); // 8. Debugging / Visualizing via Serial Plotter Serial.print("Setpoint:"); Serial.print(setpoint); Serial.print(","); Serial.print("CurrentTemp:"); Serial.print(input); Serial.print(","); Serial.print("MotorOutput:"); Serial.println(output); // 9. Save state for next iteration lastError = error; lastTime = now; Use code with caution. Tuning the PID Loop in Tinkercad Motor Driver (L293D): Connect Enable 1 to Arduino
To have a closed-loop system, the Arduino needs to "see" the current state:
1 x Potentiometer connected to the motor shaft (to act as the Process Variable feedback) Jumper wires Wiring Steps