Thursday, April 19, 2018

IAM Search

PLC Logic Programming Part 1

Although it seems each model of PLC has its own idiosyncratic standard for programming, there does exist an international standard for controller programming that most PLC manufacturers at least attempt to conform to. This is the IEC 61131-3 standard, which will be the standard presented in this chapter.

One should take solace in the fact that despite differences in the details of PLC programming from one manufacturer to another and from one model to another, the basic principles are largely the same. There exist much greater disparities between different general-purpose programming languages (e.g. C/C++, BASIC, FORTRAN, Pascal, Java, Ada, etc.) than between the programming languages supported by different PLCs, and this fact does not prevent computer programmers from being “multilingual.” I have personally written and/or analyzed programs for over a half-dozen different manufacturers of PLCs (Allen-Bradley, Siemens, Square D, Koyo, Fanuc, Moore Products APACS and QUADLOG, and Modicon), with multiple PLC models within most of those brands, and I can tell you the differences in programming conventions are insignificant. After learning how to program one model of PLC, it is quite easy to adapt to programming other makes and models of PLC. If you are learning to program a particular PLC that does not exactly conform to the IEC 61131-3 standard, you will still be able to apply every single principle discussed in this chapter – the fundamental concepts are truly that universal.

The IEC 61131-3 standard specifies five distinct forms of programming language for industrial controllers:

  • Ladder Diagram (LD)

  • Structured Text (ST)

  • Instruction List (IL)

  • Function Block Diagram (FBD)

  • Sequential Function Chart (SFC)

Not all programmable logic controllers support all five language types, but nearly all of them support Ladder Diagram (LD), which will be the primary focus of this book.

Programming languages for many industrial devices are limited by design. One reason for this is simplicity: any programming language simple enough in structure for someone with no formal computer programming knowledge to understand is going to be limited in its capabilities. Another reason for programming limitations is safety: the more flexible and unbounded a programming language is, the more potential there will be for complicated “run-time” errors that may be very difficult to troubleshoot. The ISA safety standard number 84 classifies industrial programming languages as either Fixed Programming Languages (FPL), Limited Variability Languages (LVL), or Full Variability Languages (FVL). Ladder Diagram and Function Block Diagram programming are both considered to be “limited variability” languages, whereas Instruction List (and traditional computer programming languages such as C/C++, FORTRAN, BASIC, etc.) are considered “full variability” languages with all the attendant potential for complex errors.

Memory maps and I/O addressing

Every discrete input and output channel on a PLC is linked to a corresponding bit in the PLC’s digital memory. Similarly, every analog input and output point on a PLC is linked to a corresponding word (a block of bits) in the PLC’s memory. The association between I/O points and memory locations differs between different manufacturers and models of PLC, sometimes radically. Learning how the memory of any particular computer is organized – for I/O as well as for other purposes – is one of the first things any programmer should become familiar with when learning how to work with any computer system, PLCs included.

Every PLC manufacturer publishes reference manuals providing this information, usually in the form of a chart known as a memory map. A “memory map” shows the organization of digital memory into different sections, reserved for different uses. The following table shows a partial memory map for an Allen-Bradley SLC 500 PLC1:

File number
Type Logical address range
0 Output image O:0 to O:30
1 Input image I:0 to I:30
2 Status S:0 to S:n
3 Binary B3:0 to B3:255
4 Timers T4:0 to T4:255
5 Counters C5:0 to C5:255
6 Control R6:0 to R6:255
7 Integer N7:0 to N7:255
8 Floating-point F8:0 to F8:255
9 Network x9:0 to x9:255
10 User defined x10:0 to x255:255
Note that Allen-Bradley’s use of the word “file” differs from modern parlance. In the SLC 500 controller, a “file” is a partition of random-access memory used to store a particular type of data. By contrast, a “file” according to modern convention is a contiguous collection of data bits with collective meaning (e.g. a word processing file or a spreadsheet file). Within each of the Allen-Bradley PLC’s “files” are multiple “elements,” each element consisting of a set of bits (8, 16, 24, or 32) representing data. Elements are addressed by number following the colon after the file designator, and individual bits within each element addressed by a number following a slash mark. For example, the first bit (bit 0) of the second element in file 3 (Binary) would be addressed as B3:2/0.

A hallmark of the SLC 500’s addressing scheme common to many legacy PLC systems is that the address labels for input and output bits explicitly reference the physical locations of the I/O channels. For instance, if an 8-channel discrete input card were plugged into slot 4 of an Allen- Bradley SLC 500 PLC, and you wished to specify the second bit (bit 1 out of a 0 to 7 range), you would address it with the following label: I:4/1. Addressing the seventh bit (bit number 6) on a discrete output card plugged into slot 3 would require the label O:3/6. In either case, the numerical structure of that label tells you exactly where the real-world input signal connects to the PLC. If an input or output card possesses more than 16 bits – as in the case of 24- or 36-bit discrete I/O cards – the addressing scheme further subdivides each element into words and bits (each “word” being 16 bits in length). Thus, the address for bit number 20 of a 32-bit input module plugged into slot 7 would be I:7.2/4 (since bit 20 is equivalent to bit 4 of word 2 – word 1 addressing bits 0 through 15 and word 2 addressing bits 16 through 31).

To illustrate the relationship between physical I/O and bits in the PLC’s memory, consider this example of an Allen-Bradley SLC 500 PLC, showing one of its discrete input channels energized (the switch being used as a “Start” switch for a mixer motor)2:

An example to illustrate the relationship between physical I/O and bits in the PLC's memory

Legacy PLC systems typically reference each one of the I/O channels by labels such as “I:1/3” (or equivalent) indicating the actual location of the input channel terminal on the PLC unit. The IEC 61131-3 programming standard refers to this channel-based addressing of I/O data points as direct addressing. A synonym for direct addressing is absolute addressing.

Addressing I/O bits directly by their card, slot, and/or terminal labels may seem simple and elegant, but it becomes very cumbersome for large PLC systems and complex programs. Every time  a technician or programmer views the program, they must “translate” each of these I/O labels to some real-world device (e.g. “Input I:1/3 is actually the Start pushbutton for the middle tank mixer motor”) in order to understand the function of that bit. A later effort to enhance the clarity of PLC programming was the concept of addressing variables in a PLC’s memory by arbitrary names rather than fixed codes. The IEC 61131-3 programming standard refers to this as symbolic addressing in contrast to “direct” (channel-based) addressing, allowing programmers arbitrarily name I/O channels in ways that are meaningful to the system as a whole. To use our simple motor “Start” switch example, it is now possible for the programmer to designate input I:1/3 (an example of a direct address) as “Motor start switch” (an example of a symbolic address) within the program, thus greatly enhancing the readability of the PLC program. Initial implementations of this concept maintained direct addresses for I/O data points, with symbolic names appearing in addition to those absolute addresses as mere supplements for program readability. One PLC manufacturer (Koyo) still does this, calling the symbolic labels nicknames.

The modern trend in PLC addressing is to avoid the use of direct addresses such as I:1/3 altogether, so they do not appear anywhere in the programming code. The Allen-Bradley “Logix” series of programmable logic controllers is the most prominent example of this new convention at the time of this writing. Each I/O point, regardless of type or physical location, is assigned a tag name which is meaningful in a real-world sense, and these tag names (or symbols as they are alternatively called) are referenced to absolute I/O channel locations by a database file. An important requirement of tag names is that they contain no space characters between words (e.g. instead of “Motor start switch”, a tag name should use hyphens or underscore marks as spacing characters: “Motor start switch”), since spaces are generally assumed by computer programming languages to be delimiters (separators between different variables).

I will hold to this modern convention of symbolic addressing throughout my presentations on PLC programming. Each data point within my PLC programs will bear its own tag name rather than a direct (channel-based) address code.

Ladder Diagram (LD)

In the United States, the most common language used to program PLCs is Ladder Diagram (LD), also known as Relay Ladder Logic (RLL). This is a graphical language showing the logical relationships between inputs and outputs as though they were contacts and coils in a hard-wired electromechanical relay circuit. This language was invented for the express purpose of making PLC programming feel “natural” to electricians familiar with relay-based logic and control circuits. While ladder diagram programming has many shortcomings, it remains extremely popular and so will be the primary focus of this chapter.

Every ladder diagram program is arranged to resemble an electrical diagram, making this a graphical (rather than text-based) programming language. Ladder diagrams are to be thought of as virtual circuits, where virtual “power” flows through virtual “contacts” (when closed) to energize virtual “relay coils” to perform logical functions. None of the contacts or coils seen in a ladder diagram PLC program are real; rather, they are representations of bits in the PLC’s memory, the logical inter-relationships between those bits expressed in the form of a diagram resembling a circuit.

The following computer screenshot shows a typical ladder diagram program3 being edited on a personal computer:
Typical ladder diagram program being edited on a personal computer


Contacts appear just as they would in an electrical relay logic diagram – as short vertical line segments separated by a horizontal space. Normally-open contacts are empty within the space between the line segments, while normally-closed contacts have a diagonal line crossing through that space. Coils are somewhat different, appearing as either circles or pairs of parentheses. Other instructions appear as rectangular boxes.

Each horizontal line is referred to as a rung, just as each horizontal step on a stepladder is called a “rung.” A common feature among ladder diagram program editors, as seen on this screenshot, is the ability to color-highlight those virtual “components” in the virtual “circuit” ready to “conduct” virtual “power.” In this particular editor, the color used to indicate “conduction” is light blue. Another form of status indication seen in this PLC program is the values of certain variables in the PLC’s memory, shown in red text.

For example, you can see coil T2 energized at the upper-right corner of the screen (filled with light blue coloring), while coil T3 is not. Correspondingly, each normally-open T2 contact appears colored, indicating its “closed” status, while each normally-closed T2 contact is uncolored. By contrast, each normally-open T3 contact is uncolored (since coil T3 is unpowered) while each normally-closed T3 contact is shown colored to indicate its conductive status. Likewise, the current count values of timers T2 and T3 are shown as 193 and 0, respectively. The output value of the math instruction box happens to be 2400, also shown in red text.

Color-highlighting of ladder diagram components only works, of course, when the computer running the program editing software is connected to the PLC and the PLC is in the “run” mode (and the “show status” feature of the editing software is enabled). Otherwise, the ladder diagram is nothing more than black symbols on a white background. Not only is status highlighting very useful in de-bugging PLC programs, but it also serves an invaluable diagnostic purpose when a technician analyzes a PLC program to check the status of real-world input and output devices connected to the PLC. This is especially true when the program’s status is viewed remotely over a computer network, allowing maintenance staff to investigate system problems without even being near the PLC!


Continue reading to PLC Logic Programming Part 2

Go back reading to PLC Input/Output Capabilities

Go Back to Lessons in Instrumentation Table of Contents

Comments (1)Add Comment
Software Engineer
written by msargent, May 07, 2013
When was this article written?


Write comment

security code
Write the displayed characters


Related Articles


  • ...more


Important: All images are copyrighted to their respective owners. All content cited is derived from their respective sources.

Contact us for information and your inquiries. IAMechatronics is open to link exchanges.

IAMechatronics Login