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.
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
Governing equation
Runs locally in your browser — free forever. Scale to the cloud when reality gets heavy.
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.