Languages for Programmable Logic Controllers
By Ed Butts, PE, CPI
The July installment of The Water Works introduced the concept of programmable logic controllers (PLCs) as they apply to industrial control systems. In a follow-up this month, we will discuss the various languages used for PLCs and how these devices can be incorporated into water system control.
PLC Programming Languages
Most PLCs today use one of the following five programming languages:
- Ladder logic diagram
- Functional block diagram
- Sequential function chart
- Instruction list
- Structured text.
Supervisory Control and Data Acquisition (SCADA) and Hand-Machine-Interface (HMI) systems enable users to view data from the control room and provide an interface for users to provide control input. PLCs are an essential hardware component element in these systems.
PLCs act as the physical interfaces between devices at the treatment or pumping plant and a SCADA or HMI system. PLCs communicate, monitor, and control automated processes like machine (pump and motor) functions or peripheral devices.
PLCs are available using a number of programming languages, and each offers specific advantages and disadvantages. The five primary programming languages used for PLCs are divided into two subsections, graphical and text-based:
- Graphical Ladder logic, Functional block diagram, Sequential function chart
- Text-based: Instruction list, Structured text
PLCs from different manufacturers can be programmed in various ways, and with a few exceptions, a program written in one format can be viewed in another.
Before selecting the actual program language, technicians and engineers should examine the available types and pick the one most suited to the personnel’s background and understanding of the needed control logic and fit it to the application.
Ladder Logic Diagram
LLD is a simplistic and self-documenting method of coding and has been debated as to if it is even an actual programming language.
It follows the conventional format of standard ladder-types of electrical diagrams used in relay-based control systems, so most people knowledgeable with general electrical circuits can learn it quickly.
It was the only industrial-strength programming language used for decades in the machine automation arena, and it’s still the main programming language used in the automation industry.
Ladder logic is well suited for both machine and process control, particularly for automating systems with lots of discrete inputs/outputs (I/O). It has also been modified over the years to handle analog I/O, making it a good fit for many water process control applications that tend to have a higher proportion of analog I/O as compared to machine control applications.
The ease-of-use simplifies troubleshooting and debugging as well. When monitoring the logic, it is straightforward and easy to see what is happening. No software degree or advanced programming skills are necessary. With ladder logic, it’s easy for maintenance and engineering personnel to follow the flow and understand what is going on by tracking the I/O illuminated lamps.
The ladder diagram (Figure 1a) consists of two vertical lines representing the power rails. Circuits connected as horizontal lines between two vertical rails are called ladder rungs. Each rung must start with an input or inputs and must end with at least one output.
The term input is used for a control action such as closing the contacts of a switch used as an input to the PLC. The term output is used for a device connected to the output of a PLC such as a motor or solenoid.
As the program is scanned, the outputs are not updated instantly, but the results stored in memory and all outputs are updated simultaneously at the end of the program scan. Relays are normally drawn in a schematic form, using a circle to represent the input coil. The output contacts are shown with two parallel lines.
Normally open (NO) contacts are shown as two lines and will be open (nonconducting) when the input is not energized. Normally closed (NC) contacts are shown with two parallel lines with a diagonal line through them. Many relays also have multiple outputs, and this allows an output relay to also be a simultaneous input.
Many relays also possess multiple outputs which allows an output relay to also be a simultaneous input. Ladder diagrams were originally modeled from conventional relay logic, which used physical external devices such as switches and mechanical relays to control processes. Therefore, ladder logic is generally the easiest PLC language to learn and work with for newcomers and experienced technicians/electricians already familiar with reading line schematics.
Ladder diagrams utilize internal control logic to replace all external connections, except the physical devices that need an electrical signal to activate them. A ladder diagram is built in the form of horizontal rungs with two vertical rails that represent the electrical connection on typical relay logic schematics (refer to the example in Figure 1b). Instructions are generally needed to tell the relay, timer, counter, etc. what it is expected to do, for how long, and in what time frame.
Functional Block Diagram
FBD (Figure 2) is a type of graphical language for depicting signal and data flows through input blocks, these being reusable software elements. A functional block diagram is the second most popular PLC programming language and can conceivably replace thousands of lines from a text program.
FBDs were introduced to overcome the weaknesses associated with text programming and ladder diagrams. An FBD network primarily comprises interconnected functions and function blocks to express system behavior. A key part of their introduction was to address the need to reuse common tasks such as proportional-integral-derivative (PID) control, counters, and timers at different parts of an application or with different projects.
The primary concept behind an FBD is data flow. A functional block diagram describes a function between input and output through a functional block which is depicted as a rectangular block with inputs entering from the left and outputs exiting on the right.
FBD is the fundamental language for all PLC programmers as it’s relatively simple in nature and programs function together within a PLC program graphically. Because FBDs are graphical, it is easy for system designers without extensive programming training to understand and program control logic.
As its name implies, FBD allows a PLC technician to place functions written with lines of code into boxes or blocks. The boxes can then be connected to create the larger PLC program. Almost all PLC programs are written, at least partially, with FBD because it offers the technician the ability to connect various functions together.
A function block is a program instruction unit, which when executed, yields one or more output values. Thus, a block is represented in a manner with the function name written in the block. The language uses common terms as “gates” such as AND, NAND, OR, NOR, XOR, and NOT to replicate actual control functions and schematic symbols.
For example, AND refers to the use of two open contacts in series while OR refers to the same two contacts but now in parallel.
However, there are many more options available than just normally open contacts and many function blocks have been incorporated into ladder logic, including relays, timers, counters, and analog and digital inputs and outputs.
Functional blocks can have standard functions, such as those of the logic gates or counter or timers or have functions defined by the user (i.e., a block to obtain an average value of inputs).
The function blocks are integral to this programming language, as they delineate the relationship between input and output functions. Function block programming is a language outlined in the IEC 61131-3 standard as a visual programming language that ties various instruction blocks together and allows them to execute a process based on conditional logic.
Sequential Function Chart
SFC (Figure 3a) is another graphical programming model typically used in PLCs to define the operational flow for a given process. It is similar in concept to a flowchart of the process as it defines the steps or states through which the system progresses.
An SFC organizes the functional specification for the system and programs and controls the system as a series of steps and transitions. SFCs allow the user to construct a flowchart consisting of three types of components—steps, actions, transitions—and are used to demonstrate a pictorial representation of a system’s operation as a display of the sequence of events involved in its operation.
SFC charts use the following elements: The operation is described by a number of separate sequentially connected states or steps that are represented by rectangular boxes, each of which represent a particular state of the system being controlled. The initial step in a program is represented differently from the other steps. Each connecting line between states has a horizontal bar representing the transition condition that has to be realized before the system can move from one state to the next.
Two steps can never be directly connected; they must always be separated by a transition. Two transitions can never directly follow from one to another; they must always be separated by a step.
When the transfer conditions to the next state are realized, the next state or step in the program occurs. The process thus continues from one state to the next until the complete machine cycle is completed.
Outputs/actions at any state are represented by horizontally linked boxes and occur when that state has been realized. An SFC is a series of scripts that are defined in a single location, and then called in sequential order. Additional elements in the chart can determine where the flow of the chart will lead. Charts can loop around indefinitely or execute a set number of times before ending.
An SFC offers the following specific advantages: a graphical division of processes into its major logic pieces (steps); faster repeated execution of individual pieces of the logic; simpler screen display; less time to design and debug the program; faster and easier troubleshooting; direct access to the point in the logic where a machine faulted; and easy updates and enhancements.
The structure of a typical SFC to control logic is illustrated in Figure 3b.
IL (Figure 4a) supervises programs as a series of specific instructions with each instruction entered on a new line. IL is a low-level text language that is simple to implement and used by several PLC manufacturers mainly for small and medium-sized PLCs and is particularly suitable for small and basic programs.
However, some manufacturers do not support instructionlist programming and use only the higher-level language of structured text.
Each instruction consists of an operator followed by one or more operands, the subjects of the operator. Thus, an instruction list might include an “LD A” instruction to indicate that the operand A is to be loaded, with LD being the operator used to indicate the loading.
For operators, mnemonic (i.e., a memory aid) codes are used, each code corresponding to an element operator or ladder. Figure 4b illustrates an instruction list program with a comparable ladder logic. The codes used vary somewhat from manufacturer to manufacturer, although a standard was suggested and commonly accepted under IEC 1131-3.
Although they may have incorporated the IEC standard, many vendors use additional vendor-specific calls and function blocks to suit their hardware, such as reading or writing to inputs and outputs. Therefore, if considering using instruction list as the PLC programming language, it is critical that the programmer understand the many variabilities and unique instructions associated with the specific brand of PLC.
Instruction list programs can also be prone to run-time errors and can cause infinite loops or illegal arithmetic operations. Most importantly, in today’s time-sensitive control environments, personnel other than programmers, including maintenance technicians and electricians, should be able to troubleshoot issues or faults with the equipment, including controls and programs.
Although instruction-list language is programmer-friendly and without the need for special training in the language, it’s difficult to analyze and troubleshoot the code. In other words, support personnel need to be trained specifically in instruction list language, which is simply not practical in most cases—especially when there are other languages that can address the same applications and are more user-friendly to non-programmers.
ST (Figure 5) programming language is text-based, and although not included in the two most popular programming languages, is often regarded as one of the easiest languages to understand for beginners and for those developing programs that will be read by others as it’s essentially a blank text file.
Structured text lends itself heavily to complex algorithms, complex mathematical functions, array manipulation, and repetitive tasks. In structured text, the user has access to numerous mathematical functions, but there are six operators
that have been implemented directly into the programming language:
- Addition (ADD)
- Subtraction (SUB)
- Multiplication (MUL)
- Division (DIV)
- Exponent (**)
- Modulo (MOD) operators.
The language uses commonly recognized “statements” for corresponding functions, such as TRUE, FALSE, IF, FOR, WHILE, WHILST, ELSE, ELSEIF, THEN, and CASE. In most editors, the lines are numbered for convenience of referencing code in documentation.
While graphics-based programs, such as function block diagrams or ladder diagrams, may seem easier to decipher, using a text-based language will generally consume less space and allow users to more easily follow the logic of the program and controlled system.
Another benefit of ST is that it can be combined with different programming languages. For example, function blocks containing functions written in an ST format can be generated, and because ST is a standardized programming language, programming of different PLC brands can usually be performed with it.
Like ladder logic, programs written in ST are executed one line at a time. The basic concept of ST revolves around a “program” statement for the beginning and “end program” statement for the end which sandwiches the PLC program.
Although structured text is extremely powerful, it does have some potential issues if improperly applied. For example, using WHILE and FOR statements can cause some dangerous conditions if the use or location of the specific statement is not understood. This is because during the time the WHILE statement is repeating, no other code is executing before or after the loop. The primary ways to avoid these potential issues is to fully understand what the statement implies. Use WHILE as a separate task in a low-priority instruction, or entirely avoid using WHILE in the programming.
This concludes this installment of The Water Works. We’ll be back in the January 2023 issue and continue this topic with a discussion on advanced control methods, including SCADA and Cloud systems.
Until then, keep them pumping!
Ed Butts, PE, CPI, is the chief engineer at 4B Engineering & Consulting, Salem, Oregon. He has more than 40 years of experience in the water well business, specializing in engineering and business management. He can be reached at email@example.com.