How to use PID controller
Show older comments
I have a control loop such that:
- The loop starts by calculating the error between the desired and measured temperature.
- The relay block turns the heating system on or off based on this error, with a hysteresis of ±5 degrees to prevent frequent switching.
- The output from the relay (on/off signal) is passed through a transfer function to simulate the dynamics of the system (introducing a delay or lag in the heating response).
- The scaled output from the transfer function (now a power signal) is converted into a physical signal.
- Finally, the physical signal controls the heat source, which in turn affects the system's temperature.
I want to use PID controller block instead of this, how can I achieve that?

4 Comments
Sam Chak
on 4 Sep 2024
It is possible. However, if you do not wish to tune the PID controller through trial-and-error or aimless autotuning, then the mathematical model (preferably in transfer functions) enclosed in the red frame must be known. Since the transfer function of the actuator is provided, the remaining task is to derive the transfer function of the heater in the blue frame.
Ideally, the transfer function derived from first principles should match the transfer function estimated by the Simulink Model Linearizer. Unfortunately, I am unfamiliar with Simscape. Most Simscape documentation provides the physics, such as that of the Pipe (TL), but does not directly offer the transfer function, which is preferred in future release.
If you encounter difficulties, you may consult @Yifeng Tang, who is an expert and regularly addresses Simscape application-related issues.

Shivam Gothi
on 4 Sep 2024
Can you please share the simulink model of the above closed loop system? It will be usefull to tune the PID controller and also test its performance.
Muhammad Ali
on 16 Sep 2024
Yifeng Tang
on 17 Sep 2024
I've done something similar to this. PID tuner does the job. It will first attempt to linearize the fluids systems, which is usually not that linear :p So it matters at which time you do the linearization (I think you can choose that in the tuner). Usually t=0 is NOT a good point to do the linearization. I've found that using a time where the system has stablized tends to give you a linearized system that's more useful.
Accepted Answer
More Answers (1)
Shivam Gothi
on 16 Sep 2024
0 votes
I followed the same steps as suggested in above answer and got good results. I am attaching the modified simulink file.
You need to first connect the PID controller block in the network and then click on "tune" button. The tuner app will decide the gains best suitable to achieve the specified performance. Below given is the screenshot of "PID tuner app" I used in your attached model.

the screenshot from scope inside the "temperature control block" found inside "heating system" is shown below:

Please let me know whether this is correct and as per your expectation.
3 Comments
Muhammad Ali
on 17 Sep 2024
Shivam Gothi
on 17 Sep 2024
I have saved and attached the Simulink model files in the R2023a version. Please let me know if you are able to open them successfully. Additionally, if any of the responses meet your expectations and prove useful, I would appreciate your feedback or if you could mark the answer as accepted.
Muhammad Ali
on 17 Sep 2024
Categories
Find more on Real-Time PID Autotuning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






