Comparing Programming Languages: Structured Text vs. Ladder Logic
Structured text and ladder logic are fairly common yet visually different languages PLCs can utilize. How do you choose the best language for your application?
The modern programmable logic controller (PLC) can utilize many programming languages within the IEC 61131-3 standard. Two of the most common are ladder logic and structured text. Let’s examine each of these languages, their similarities, and differences.
Structured text (ST) is a recognized programming language within the IEC standard and is becoming more frequently used within automation. The ST language is a stripped-down version of C or C++ computer programming language. The syntax is very similar, so if you are familiar with one of the C languages, the ST language would feel very comfortable.
Figure 1. Programming code. Image used courtesy of James Harrison
Statements and Tasks
The ST language uses a series of statements to control variables and outputs. If you want to compare a boolean expression, you might use an IF statement. Some statements allow the programmer to loop through an array of data and perform some functions, while other statements perform a task until a boolean expression is true or false.
IF pbStart THEN
MotorON := TRUE;
ELSIF pbStop THEN
MotorON := FALSE;
Figure 2. An example of ST language.
Using these statements allows the programmer to create compartmentalized or function-based code. The programmer can break up the code into tasks or classes containing functions or state machines. The ST language gives the programmer a large degree of freedom on how the code will flow and react to input and outputs.
Using Structured Text in Automation
Virtually any automation control scenario can be done with ST. State machines can be used for sequencing, FOR loops can be used for processing data, and WHILE loops can be used for cyclic operation.
PLC manufacturers are developing hardware whose native language is ST, primarily because of the software used to program the PLC. Programmers can map inputs and outputs to variables accessed within the code, and sometimes complicated code can be easily represented with ST programming. In addition, robots typically use ST, or a variation, for their programming language.
Ladder logic was the first language used with PLCs. It was intended to represent an electrical digram because the PLC was designed to replace large electrical panels with hundreds of relays. The ladder language is a graphical language that is easy to read and is used throughout the world as a standard for machine automation.
Figure 3. An example of ladder logic.
Inputs and boolean variables are represented with a contact electrical symbol, while outputs are represented with a coil. More complicated functions, like a MOVE instruction—which moves data from one variable to another—are represented by a block with a title; MOV, for example, for the move instruction.
Using Ladder Logic in Automation
PLCs were designed to use ladder logic; for a time, it was the only language you could use on a PLC. Some PLC manufacturers have included sequencer function blocks, and most recently, FOR loop function blocks.
Figure 4. PLCs with multiple inputs and outputs.
Ladder logic works well for simple automation. Just about anybody can easily understand it, it’s highly flexible, and it’s easy to troubleshoot with the graphical representation of syntax.
Ladder logic has evolved to be a compatible language with the ability to send messages to computers or other Ethernet devices and handle large complex data structures. Ladder logic can and is used in just about any automation project you can throw at it.
Structured Text vs. Ladder Logic: Similarities and Differences
The most obvious difference is that ladder logic is graphical, while ST uses words and special characters for its syntax. This difference seems to be a hurdle that some people cannot get over, depending on your experience and learning type.
Another difference between the languages is that the ladder’s contacts, and coils will change color when energized. But, in ST, you will need to use the programming software’s watch window or debugging tools to know the state of variables. Seeing the code solve and knowing which variable is true or false with a simple glance can make troubleshooting easier.
Both languages typically solve left to right, top to bottom, in a cyclic pattern. Both systems will also typically use tag-based or variable-based programming, meaning you can name your tags or variables in any text you want.
So … Structured Text or Ladder Logic?
There is no right or wrong answer to this question.
Some people prefer ST simply because it is more compact and easier to read. Some people prefer ladder logic because it’s what they learned, know, and love.
If we look at this question from a pure aesthetics point of view, complex code can be represented in ST in a compact form. ST language uses less space than ladder logic for the same tasks. Most PLC manufacturers now support subroutines and some form of functions that can be used for repeated code. This means ladder logic can use the same space-saving features available in ST, only in a ladder language.
Both Rockwell and Siemens allow ST and ladder logic to be used in the same program. Siemens even lets you have ST and ladder in the same function call or function block. My personal preference is to use ST when I need to manipulate strings or arrays of data, and when setting machine parameters. The rest of the time, I focus on using ladder logic in the most efficient way I can.
Which programming language do you prefer, and why?