PPolySim OS

Controller → Code

From tuning to firmware: design a PID, preview exactly how it will behave in closed loop, then export the sketch you flash — the same discrete controller in the preview and the code.

Controller → CodeLive

Controls

Design a P/PI/PID controller, watch the closed-loop response (with an optional load disturbance), then export a ready-to-flash sketch for Arduino, ESP32, C, Python, or MicroPython.

// PolySim OS — PID controller for Arduino (10-bit ADC + 8-bit PWM) (generated for your gains).
// Works in 0..100% units to match the PolySim preview. Plug into the PolySim Hardware
// Bridge — it streams "measurement,output" and sets the setpoint from the control slider.
const float Kp = 3.0000, Ki = 2.0000, Kd = 1.0000;
const float dt = 0.0200, Tf = 0.08;   // loop period + derivative-filter time constant (s)
const int   SENSOR_PIN = A0;    // <-- your sensor
const int   OUTPUT_PIN = 9;     // <-- your PWM output
float setpoint = 60.0000;              // percent (0..100)
float integral = 0, prevMeas = 0, dFilt = 0;

void setup() { Serial.begin(115200); pinMode(OUTPUT_PIN, OUTPUT); }

void loop() {
  if (Serial.available()) setpoint = Serial.parseFloat();
  float meas  = analogRead(SENSOR_PIN) * 100.0 / 1023.0;     // → percent
  float error = setpoint - meas;
  integral += error * dt;
  float d = -(meas - prevMeas) / dt; prevMeas = meas;
  dFilt += (d - dFilt) * (dt / (Tf + dt));                      // filtered derivative
  float u = Kp*error + Ki*integral + Kd*dFilt;
  if ((u > 100 && error > 0) || (u < 0 && error < 0)) integral -= error*dt;  // anti-windup
  u = Kp*error + Ki*integral + Kd*dFilt;
  u = constrain(u, 0, 100);
  analogWrite(OUTPUT_PIN, (int)(u * 255.0 / 100.0));
  Serial.print(meas); Serial.print(","); Serial.println(u);
  delay((int)(dt * 1000));
}

Data Inspector

ControllerPID
Overshoot1%
Settling time2.1 s
Steady-state error0.7
Loop period20 ms

Governing equation

Reading this result: Well-tuned PID: ~1% overshoot, settles in ~2.1s. Export the sketch and flash it.

Runs locally in your browser — free forever. Scale to the cloud when reality gets heavy.

or unlock everything with Pro →
★ Sign in to save this setup
Save your tuned setup, or drop this simulation into your own site, docs, or course page.

How it works

Controller → Code turns a control design into runnable firmware. Tune Kp/Ki/Kd and watch the closed-loop step response of a second-order plant, computed with the exact discrete PID (derivative-on-measurement + integral anti-windup) that the generated code implements — so the preview matches the hardware. Export a ready-to-flash Arduino sketch, portable C, or Python. The Arduino target speaks the PolySim Hardware Bridge protocol (streams measurement,output and reads its setpoint from serial), so you can design here, flash the board, and drive it live from /studio/hardware-bridge. It's the free, browser-native version of what Simulink Coder does — no toolbox, no install.

Ask the AI about this model

The math, the assumptions, real-world uses, or a code translation — explained for this exact simulation.

Related live simulations

Frequently asked questions

Is this PID Arduino code generator controller tool really free?
Yes. Controller → Code runs entirely in your browser using your device's own compute, so local use is free forever. You only pay Compute Tokens if you scale a job to the cloud.
Do I need to install anything?
No. Everything runs client-side in a modern browser — no downloads, no license, no account required to start.
Can I save or share my simulation?
Create a free account to save projects, and use a shareable embed or minted DOI to publish a live, interactive version anywhere.
How accurate are the results?
The solver uses established numerical methods, but results are for research and educational purposes and should be validated against experiment or professional review before you rely on them.