Why Should you use Structured Text ST?
By Mouhammad Hamsho
18.08.2022
I am gonna cut the road short for you and try to convince you why you should switch from Ladder Logic to Structured text when programming your PLC.
Ladder logic has been the leading PLC programming language for ages. Its popularity stems from the fact that it is a graphical language, and claimed to be easy to maintain and modified by technicians who later might decide to extend or modify an existing machine system. However, what most people don’t realize that the programming field has evolved by miles since Ladder logic was invented.
What does Ladder logic lack?
- Lack of code structure elements, in plain language, Data structures like Arrays
- Lack of repetitive work-savers elements, in plain language, Loops.
Lack of code structure elements:
Let us take a very typical daily routine program example:
Figure 1: Decomposing all motor functions and components
Assume we have three motors, each has an overload signal, overcurrent signal, start button, stop button, and a relay coil.
Using any advanced PLC platform, be it Codesys, TIA Portal, SoMachine or any other one, You would have to define variables for every signal you have and then map it to the proper IO module.
Your variable list would end up with something like:
//structured Text bool variable declaration Example
motorOverload1 : bool;
motorOverload2 : bool;
motorOverload3 : bool;
motorOverload4 : bool;
And the variables list just grows too fast when having, for example, a 100 motor in your design. That can get pretty ugly to manage yet most programmers still do it the traditional old-fashioned way.
Structured Text Declaration Example
Lack of repetitive code-saver elements:
As the picture above shows, one needs to write every single line of that program even though all the lines the ladder code are just the same except for the signal name numbering. Again, for a 100 motors, this could get really messy and unnecessary pretty quickly.
Structured Text (ST) to the rescue:
Like any super basic programming language, loops and arrays are an essential part of saving repetitive work and structuring the code. In the picture below, we have saved tons of variables definitions by just defining an array with a size equal to the number of motors needed for every signal.
The next step would be to map the functionality. Any of the 3 motors would operate as follows:
- Check if stop button, Overload or over current are activated
- If yes, then the motor should not start, hence the PLC should not activate the relay to activate the motor
- If none of the mentioned signal is activated, then the program checks it the start button is activated
- If yes, we would turn on the relay to run the motor
- If no, then nothing would happen
Now that is the logic for a single motor. What we did here is that a FOR loop was included to apply that same functionality for all the 3 motors we have.
If the requirement of the project changed, and say we now have 100 motors, all that needed to be done is to change that “3” number with “100” in the code. Even that number can be parameterized so that we change it in only one place and not all over the code