Technical Article

# Comparing Programming Languages: Structured Text vs. Ladder Logic

August 14, 2022 by Shawn Dietrich

## Ladder logic and structured text language are fairly common yet visually different. How do you choose the best PLC language for your automation 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 Programming

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.

#### ST Language Inputs and Outputs

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.

PROGRAM_CYCLIC

      IF pbStart THEN

            MotorON := TRUE;

      ELSIF pbStop THEN

            MotorON := FALSE;

      END_IF

END_PROGRAM

##### Figure 2. An example of structured text programming language which can be broken up into tasks or classes containing functions or state machines.

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.

#### Automating with PLC Structured Text

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 Programming

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. A graphical language ladder logic example. In this PLC language, electrical contact symbols represent inputs and boolean variables, and coils represent outputs. A MOVE instruction (MOV) is represented by a block.

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. Different PLC types 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.

### Comparing the Two Common PLC Programming Languages

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 … Is Structured Text or Ladder Logic Better?

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?

### Learn More About:

7 Comments
• P
Phil_Mc January 27, 2022

I prefer Ladder Logic for simple digital controls; as you mention it’s easier to debug when the software animates the on/off states.
For data manipulation: if I can achieve this simply in ladder logic using just one or two function blocks, then I’d choose that option, but for more complex data manipulation I’d prefer ST.

Like.
• R
rytko January 28, 2022

I used to prefer ladder because that’s what was available when I first started programming over 25 years ago. When ST became available, I only programmed complex data manipulations and calculations in ST - I once had to write my own steam table calculations about 15 years ago which required summing a bunch of products created by looping through arrays of constant coefficients that multiplied measurements - ST was perfect for that! Now that I program for the same types of equipment (switchgear) but have to use a wide variety of PLC platforms per customer direction, I prefer ST for its portability. Sometimes the syntax is slightly different between platforms, but find and replace operations in a text file makes syntax changes pretty easy. Porting between platforms is almost impossible in the diagram-based languages of LD, FB, or SFT because it requires reprogramming each diagram.

LD is great for those who learned logic with relays and switches/contacts like electricians/technicians, but as the next generation comes up behind me and college-educated EEs seem to be doing more of the programming, they seem to prefer ST because many of them haven’t been exposed to relay ladder logic, but they’ve had to program a lot in text languages. Some of the younger electricians/techs I work with who have grown up with more programming in their lives don’t seem to mind programming in ST either.

Recently, I’ve ended up designing a couple of fairly complex controller algorithms using MATLAB and then used Simulink Coder to port my algorithms to ST for use in a PLC. That’s a pretty cool thing.

Like.
• C
C_Danger January 28, 2022

I use ST in about 75% of my programs.
The SELECT CASE statement is very powerful and makes programming a machine sequence very straight forward.
It does take a different mindset to program in ST vs Ladder.
One of the biggest differences being that when programming in ST a bit acts like a latched bit in Ladder.  Once set it remains set until is is turn off.
this can cause some issues if you do not keep it in mind but in general I find it much faster to develop in ST, easier to debug and alter sequences esp when using the Select Case statements and almost portable between different platforms.
If I have developed a handy function on a Omron and want to use it on a AB then mostly only a little tweaking in notepad is all that is required.
I do my Mapping to I/O in Ladder because it is easier to see , as mentioned the “logic flow” is presented visually.

One note , the two code snippets are almost functionally the same but is bot start and stop buttons are both on in the ladder code the motor will be off but in the ST code shown the motor would be On
easlier changed by using this

IF pbStop THEN
MotorON := 0;
ELSIF pbStStart THEN
MotorON := 1;
END_IF;

OR

if pbStart then MotorOn:=1;end_if;
if pbStop then MotorOn:=0;end_if

Like.