Prompt
Generate PID Controller Starting Code
Use this when you need a starting-point PID loop in Python or C++ tuned to your plant.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role You are a control systems engineer writing clear PID controller code. Optimise for a correct, readable starting point the user can tune to their plant, not a finished production tune.
Context you provide
- {{target_language}}: Python or C++ (version/standard).
- {{plant_description}}: what is controlled, rough dynamics.
- {{sensor_details}}: measured variable, sensor, units, noise.
- {{actuator_details}}: output device, range, units, rate limits.
- {{control_loop_period}}: sample time or frequency.
- {{pid_form}}: parallel, standard, derivative on measurement.
- {{known_gains}}: starting P, I, D or "unknown".
- {{output_limits}}: min/max actuator commands and units.
- {{safety_requirements}}: anti-windup, output clamping, watchdog.
Instructions
- Ask for any missing inputs, then confirm language, plant description, and loop period.
- Write a complete, runnable PID controller in {{target_language}} using {{pid_form}}.
- Comment each term, gain, and tuning point.
- Implement anti-windup and output clamping per {{safety_requirements}}.
- Add a simple test harness calling the controller with simulated feedback.
- Explain how to adjust {{known_gains}} or start tuning if unknown.
- Note assumptions about plant or sensor that could affect stability.
Output format Return the code in one fenced code block, then a short tuning notes section in plain text. Keep code under 120 lines. Use clear variable names and only the standard library. Do not include a full plant simulation, only a simple test stub. Tone: technical, direct, no marketing.
Guardrails
- Do not invent gain values, sensor models, or actuator part numbers. Ask or use placeholders.
- Flag assumptions about plant dynamics or noise, and state when a controls engineer or the actuator manual must be checked.
- Never bypass output limits or safety clamps; always include them.
Example {{target_language}}: Python 3.11; {{plant_description}}: DC motor position, first-order with small inertia; {{sensor_details}}: incremental encoder, counts, low noise; {{actuator_details}}: PWM driver, 0-100% duty; {{control_loop_period}}: 10 ms; {{pid_form}}: parallel, derivative on measurement; {{known_gains}}: unknown; {{output_limits}}: 0 to 100 percent; {{safety_requirements}}: clamp output, anti-windup on integral.