void motorDrive(double cmd) { if (cmd >= 0) { digitalWrite(dirPin, HIGH); // Forward analogWrite(pwmPin, cmd); } else { digitalWrite(dirPin, LOW); // Reverse analogWrite(pwmPin, -cmd); } }
Open Tinkercad right now. Create a new circuit. Drag an Arduino and a DC motor. Write a simple P controller. Watch it oscillate. Then add D to calm it. Then add I to zero the error. You will never forget how a PID feels once you have tuned it—even in a browser.
// Tinkercad PID Position Control for DC Motor double setpoint = 0; // Desired angle (0-1023 from pot) double input = 0; // Actual angle from feedback pot double output = 0; // PWM signal (-255 to 255) sent to motor double lastError = 0; double integral = 0; // PID Gains - Start with P only double Kp = 5.0; double Ki = 0.5; double Kd = 0.8; tinkercad pid control
// Motor pins const int pwmPin = 9; const int dirPin = 8;
This article will guide you through the theory of PID, why you need it, and how to build, tune, and debug a PID controller inside Tinkercad Circuits. By the end, you will have a simulation of a temperature regulator or a motor positioner that you can export directly to physical hardware. PID stands for Proportional-Integral-Derivative . It is a control loop feedback mechanism widely used in industrial control systems. The goal is simple: take a measured process variable (e.g., temperature, speed, position) and force it to match a desired setpoint (e.g., 100°C, 2000 RPM, center position) by adjusting a control variable (e.g., heater power, motor voltage, steering angle). void motorDrive(double cmd) { if (cmd >= 0)
// Time delta for derivative and integral unsigned long now = millis(); double deltaTime = (now - lastTime) / 1000.0; if (deltaTime > 0.05) { // Run PID every 50ms output = computePID(setpoint, input, deltaTime); motorDrive(output); lastTime = now;
// PID output double outputRaw = Pout + Iout + Dout; lastError = error; Write a simple P controller
// Integral term with anti-windup (clamp) integral += error * dt; double Iout = Ki * integral;