When a PLC is used primarily to replace relays, timers, and counters, it's hard to beat the simplicity and usefulness of ladder diagram programming.
Their ability to accept programming in ladder diagram format is one of the reasons for the success of programmable logic controllers (PLCs) in the industry. The many similarities between the ladder diagrams used to program PLCs and the relay ladder logic formerly used to control industrial systems eased the transition from hardwired relay systems to PLC-based systems for many people in the electrical industry. And, the ability to monitor PLC logic in ladder diagram format also made troubleshooting easier for those already familiar with relay-based control systems. Although there are many higher-level languages now available for PLC programming, the majority of systems are still programmed in ladder diagram format because of these advantages.
Anatomy of a ladder diagram
The logic in a ladder diagram typically flows from left to right. The diagram can be divided into sections called rungs, which are roughly analogous to the rungs on a ladder. Each rung typically consists of a combination of input instructions. These instructions lead to a single output instruction; however, rungs containing function block instructions may be more complicated.
Each input or output instruction is assigned an address (shown below the instructions in Fig. 1, on page 22) indicating the location in the PLC memory where the state of that instruction is stored. Function block instructions may include one or more addresses to store parameters related to the function that they perform.
The numerical format of the address depends on the scheme used by the particular manufacturer, and it may be stated in a binary-based numbering system. (See sidebar on page 26.) A name may also be associated with each address (shown above the instruction in Fig. 1) to make the ladder diagram easier to interpret.
Types of instructions
Ladder diagram programming allows PLCs to perform several different types of tasks, including Boolean logic, timing, counting, arithmetic, and special functions. We'll discuss the basic instructions and function block instructions, which are common to nearly all PLCs. In addition to these instructions, most PLCs support many extended instructions to perform more complicated tasks.
Boolean logic. Boolean logic is actually what PLCs and relay systems perform. The legal operations in Boolean logic are AND, OR, and NOT. The AND operation simply means all of the inputs must be ON for the output to be ON; it's analogous to relay contacts connected in series.
The OR operation means that the output is ON if at least one of the inputs is ON; it's analogous to relay contacts connected in parallel.
The NOT operation means that the output is OFF if the input is ON and vice-versa; it's analogous to a normally closed relay contact. To describe the situation in Rung 1 of Fig. 1 using Boolean logic, for example, we can say that Output 201 is ON if Input 101 OR 102 is ON AND Input 103 is NOT ON.
Relay instructions. PLCs perform Boolean logic using basic relay instructions. Common input instructions are examine-on (normally open contact) and examine-off (normally closed contact). Input instructions can be used to examine either the state of external PLC inputs or internal bits in the PLC memory. The examine-on instruction is ON if the input or bit being examined is ON; the examine-off instruction is ON if the input or bit being examined is OFF. The examine instructions can be connected in series and parallel in any combination to perform virtually any Boolean logic function, and the result can be connected to a relay coil (output) instruction. Relay coil instructions can be used to control either external PLC outputs or internal relay coils (bits) in the PLC. You can see an example of the use of relay instructions in Rung 1 of Fig. 1.
Timing instructions. The basic timing instructions are on-delay and off-delay. With on-delay timers, the output associated with the timing instruction is turned ON some specified time after the input is ON, but it turns OFF immediately when the input is turned OFF.
With off-delay timers, the output is turned ON immediately when the input is turned ON. However, it remains ON for a specified period of time after the input has been turned OFF, but before going OFF.
Timing instructions are usually connected like relay coil instructions in the ladder diagram, so any combination of input conditions can be programmed to trigger them. When a timing instruction is created in the PLC program, space is reserved for the timer running value and the preset value at which the timer will trip, and the programmer must set the preset value to the desired number when inserting the instruction. The status of the timer output can be checked with the examine instructions for use in other rungs in the program.
Various combinations of on-delay and off-delay timers can be used to perform more complicated functions like, for example, interval or cycle timing. A simple on-delay timing instruction is shown in Rung 2 of Fig. 1.
Counting instructions. The basic counting instructions provide for count-up, count-down, and counter reset functions. The count-up instruction simply increases the value of a counter by 1 each time an upward (OFF to ON) transition is detected in the input.
A count-down instruction does the opposite, decreasing the count value by 1 on each upward transition of the input. When the value of the count reaches or exceeds a preset number, the output of a counter instruction is turned ON.
The reset instruction resets the value of the count to 0 or to a predetermined reset value. A positive reset value may be used, for example, for a down counter that is expected to count down to 0. As with timing instructions, counting instructions are usually connected like relay coil instructions in the ladder diagram, and space is reserved for the running count value and the preset trip value when the instruction is created in the PLC program. Counter instructions usually have the additional feature in that they can be referenced to the same address, so that the up, down, and reset actions can be performed on the same counter if desired.
The counter output status can be checked with the examine instructions for use in other rungs in the program. Rung 3 in Fig. 1 shows a typical count-up instruction.
Arithmetic instructions. Almost all PLCs have simple ladder diagram instructions to add, subtract, multiply, and divide two numbers. A ladder rung for an instruction used to perform an arithmetic operation typically has three parts. First are the input conditions that must be true in order for the computation to take place; this can be any combination of examine instructions. Second are the locations of the two numbers to be operated upon; these locations are often entered into the ladder diagram as get instructions, which resemble examine instructions and which tell the program where to find the numbers in memory. The third and final part of an arithmetic ladder rung is the output location; it's usually entered as an address assigned to the actual arithmetic instruction (+, -, x, or /), which resembles a relay coil instruction.
Each time the input conditions are met, the two numbers are retrieved from the input locations, the indicated arithmetic operation is performed, and the result is put into the output location. Several arithmetic instructions can be linked to perform more complex operations by assigning outputs from previous rungs as inputs in subsequent rungs. A simple add instruction is shown in Rung 4 of the Fig. 1 ladder diagram program.
Another group of arithmetic instructions are the compare instructions, which determine if one number is less than, equal to, or greater than another number. They are programmed in the same way as the arithmetic operations; each time the input conditions are met, the indicated comparison ([less than], =, or [greater than]) is made. The output bit assigned to the instruction is turned ON if the comparison is true. A compare (less than) instruction is shown in Rung 5 of Fig. 1.
Function block instructions
The basic ladder diagram instructions just described are fine for performing many simple tasks, but sometimes things get more complicated. What if there's a need to program a shift register, stack, or process controller, for example? Or perhaps there's a desire to configure a serial communication module or a network connection. Although these complex tasks probably could be programmed with combinations of the standard instructions, PLC manufacturers have introduced function blocks to make programming some of the more common tasks easier.
Like an arithmetic rung, a rung that contains a function block has three parts. First are the input conditions, which again consist of combinations of examine instructions. There may be multiple input conditions, however, because function block instructions often have several inputs. Then comes the function block itself, which may contain locations for many parameters that must be set to control its action. Finally there are the outputs, which resemble relay coils, and which allow the function block to interact with other rungs in the program.
Any given function block may have one or several outputs. Numerical outputs can be used by other rungs as inputs to arithmetic instructions, and relay coil (bit) outputs can be examined with the examine instructions.
Many PLC manufacturers have now converted timing, counting, and arithmetic instructions to the more user-friendly function block format, and the programming documentation available for a specific PLC will explain the programming of the various available function block instructions in detail. Rung 6 in the Fig. 1 ladder diagram program shows an example of a function block that is used to send data over a network.
Summary
As PLCs are applied to increasingly complicated tasks, and as people in the electrical industry become more comfortable with computer programming, the use of high-level languages for PLC programming is sure to increase. The additions of extended and function block instructions to the basic instructions representing relays, timers, and counters, however, have helped ladder diagram programming to remain a flexible and viable tool for many PLC applications.
SUGGESTED READING
EC&M Articles:
"Knowing the Basics of PLCs - Part 1," October 1995, p. 20.
Books:
L.A. Bryan and E.A. Bryan, Programmable Controllers: Theory and Implementation, Industrial Text Co., 1988.
RELATED ARTICLE: WHY THE USE OF BINARY NUMBER SYSTEMS?
Unfortunately, computers and microprocessors don't use the same numbers that we learned in elementary school. People learn to do mathematics using decimal (base-10) numbers, but microprocessor-based systems like PLCs are inherently binary because they are based on solid-state switches that can be either ON or OFF. Numerically, binary (base-2) values are represented simply by either the numbers "0" (OFF) or "1" (ON). Several binary digits (bits) must be used to represent numbers of practical size, so binary numbers are usually represented by 8, 16, or 32 bits.
Because long strings of bits are cumbersome for people to deal with, binary numbers are often converted to other numbering systems when used for practical purposes like PLC addressing. Hexadecimal (base-16) and octal (base-8) are the two most popular such systems in the PLC domain. Each hexadecimal digit can represent 4 bits, while each octal digit represents 3 bits. There's also a hybrid scheme called binary coded decimal (BCD), where each digit of a standard decimal number is represented by a group of 4 binary digits. The table above shows a comparison of these various numbering schemes.
Ryan G. Rosandich is Assistant Professor, Engineering Management, University of Kansas Regents Center.