PLC programming fundamentals and uses in discrete manufacturing

Programmable logic controllers (PLCs) are the main method of controlling discrete machinery in manufacturing.

By Frank Lamb December 6, 2023
Courtesy: Automation LLC

Programmable logic controller insights

  • Programmable logic controllers (PLCs) serve as robust, modular controllers in industrial settings. They manage real-world inputs/outputs, operate deterministically within set time limits, and enable expandability through specialized modules.
  • PLCs employ a mix of volatile and non-volatile memory, needing battery backup for retaining data. Data storage, program execution, and I/O management are distinct yet interdependent functionalities.
  • Different PLC platforms organize data differently, impacting memory usage and addressing methods. Modern PLCs emphasize tag-based systems for descriptive, easy-to-use memory object management, enhancing programming efficiency.

A programmable logic controller (PLC) or programmable controller, is a digital computer used to control electromechanical processes, usually in an industrial environment. It performs discrete and continuous control functions and differs from a typical computer in several important ways:

  1. It has physical inputs and outputs (I/O); electrical inputs and outputs bring real world information into the system and control real world devices based on that information.

  2. It is deterministic and processes information and reacts to it within defined time limits.

  3. It is often modular. It can have I/O modules, communication modules or other special purpose modules added to it for expansion.

  4. It is programmed using several defined Languages. Some languages allow the program to be changed while the machine or system being controlled is still running.

  5. Software and hardware are platform-specific; components and programming software usually can’t be used between different manufacturers.

  6. It is rugged and designed for use in industrial environments.

Unlike computers, PLCs are made to run 24 hours a day, 7 days a week and can resist harsh physical and electrical environments.

Figure 1: Modular PLCs of various brands.

Figure 1: Modular PLCs of various brands. Courtesy: Automation LLC

Physical layout of a PLC

A block diagram of the physical arrangement of a PLC is shown in Figure 2. Not all the items shown in this diagram are present on every PLC, but this will provide an idea of a typical configuration.

Processor or CPU. The CPU processes all of the logic loaded into the controller and also contains the operating system. It usually has a real-time clock built in which is used for various functions. The system memory is also associated closely with the CPU.

PLC memory. PLC memory consists of the operating system and firmware of the processor (sometimes called system memory), the module firmware (if any), and the program and data that is used by the programmer. There are volatile and non-volatile areas of memory; the volatile part of memory needs a battery, “super-capacitor”, or other rechargeable energy storage module to hold its program and/or data.

Figure 2: Example of a PLC layout and its various components.

Figure 2: Example of a PLC layout and its various components. Courtesy: Automation LLC

Though the program can be saved on Flash or SD RAM cards without a battery, the data exchange rate is too slow to use this for the actual interfacing of the program with its data.

When the PLC is powered on, the program is loaded from non-volatile RAM cards into the user memory of the controller. Not all PLC platforms back up the user memory with a battery or other energy storage device, data memory may be lost when a processor loses power. Some platforms, however, ensure that the data is kept intact even when power is lost by use of battery-backed RAM. This means the values in data registers will be retained and the program will start in its last state.

Other PLC platforms assign some parts of RAM to be “retentive” and other parts non-retentive. Omron separates its retentive bits into “holding” relays and non-retentive “CIO”, and its data into the retentive DM Area and non-retentive “Work Area”. Siemens allows its general “marker” memory to be assigned as retentive or non-retentive and defaults to only 16 bytes of retentive marker memory, but it can be changed. Siemens data blocks, however, are retentive unless defined not to be. Allen-Bradley’s memory is all retentive.

The operating system itself on a processor is held in non-volatile System memory, called “firmware”. To change the firmware on a PLC a “Flash” program or tool needs to be used to download it. This is usually included with the programming software.

I/O, communications and other modules also often have firmware built in. The firmware update tools can also update these modules and the firmware is usually available from the manufacturer’s website. It is necessary to have software that is at least as up-to-date as the firmware being installed.

The RAM part of memory in a PLC can be separated into two general areas: Program memory and data memory

Program memory consists all of the lists of instructions and program code. This is what is sent to the processor. The act of sending the program instructions to the PLC is called “downloading” on most brands of PLC, however this may differ on some platforms.

Data memory includes the Input and Output Image Tables as well as numerical and Boolean dta. You will find that most of the data used in the PLC program is internal memory and not directly related to I/O.

As the program executes, it keeps track of whether bits (BOOLS) are on or off and the values of numbers in Data Memory. Different platforms have different ways of organizing this data.

PLC inputs and outputs (I/O)

Physical I/O can be discrete, single signal bits that are either on or off, or analog, signals that change amplitude in either voltage or current.

Figure 3: Example of a discrete signal.

Figure 3: Example of a discrete signal. Courtesy: Automation LLC

Figure 3 shows a digital or discrete signal. Typical signal levels for discrete inputs and outputs are 24 Vdc and 120 Vac, but other levels may be present depending on the type of device or input card. In addition to the designation one (1) and zero (0) or on and off, discrete signals may be described as true or false.

See examples of digital input and output devices below (Figure 4):

Figure 4: Examples of discrete input/output (I/O) devices.

Figure 4: Examples of discrete input/output (I/O) devices. Courtesy: Automation LLC

Analog signals vary in either voltage or current. Ranges are typically 0 to10 Vdc or -10 to +10Vdc (voltage type) or 0 to 20mA or 4 to 20mA (current type). The electrical signal is then converted into a number for use in the PLC program.

Figure 5: Example of an analog signal.

Figure 5: Example of an analog signal. Courtesy: Automation LLC

Here are some examples of analog input and output devices (Figure 6):

Figure 6: Examples of analog I/O devices.

Figure 6: Examples of analog I/O devices. Courtesy: Automation LLC

PLC communication methods

As mentioned earlier, there are a wide variety of communications methods used to communicate with PLCs and their devices. Serial communications, ethernet and various fieldbus protocols are just some of these.

Table 1: Different types of PLC communication platforms, serial, fieldbus and Ethernet.

Table 1: Different types of PLC communication platforms, serial, fieldbus and Ethernet. Courtesy: Automation LLC

Note that some of these protocols are the same across different brands, even then, they may not communicate properly without modification of parameters. Serial and Ethernet are the two most common methods of connecting with a PLC, and Fieldbus protocols are usually used to communicate with remote or distributed I/O devices. Ethernet is used to program PLCs and for distributed devices, but it is important that I/O communication is deterministic, as described in the previous PLC definition.

PLC data

Depending on the platform, PLCs treat data in different ways. Older PLCs were usually either Byte-based or Word/Integer based. This had a major effect on how memory was stored and used. Some PLCs have registers assigned to specific data types, that is, Bit, Integer or Real, whereas other brands may separate data by whether it is retentive or place all data together.

It is important when learning a new PLC’s programming platform to first understand how its memory is organized. In the older GE PLCs for instance, data memory and I/O share the same space. It could be quite embarrassing if you were to cause actuators to move when intending to simply save an integer to a register.

Figure 7: Types of PLC data structures.

Figure 7: Types of PLC data structures. Courtesy: Automation LLC

Figure 7 shows the layout of several PLCs’ memory areas. The first list, Allen-Bradley’s SLC and MicroLogix family, shows that data is segregated into numbered files, O0, I1, S2…F8. Each data file is expandable up to 255 words but after that, new file numbers must be added, such as N9, B10 and so on.

The next table shows Siemens’ Step 7. I/O is assigned during hardware configuration rather than by slot number as with Allen-Bradley. The general memory area “M” is of a fixed size, whereas Memory Blocks or Data Blocks (DBs) contain a mix of data types and can be up to 64KB in size.

I/O addressing varies from brand to brand. Inputs may be addressed as I or X, outputs as O, Q or Y, and analog I/O designations may use a completely different format than digital ones. Some brands designate I/O based on the slot number where the card is assigned while configuring hardware; this can’t be changed. Other platforms, like Siemens, have a default location where I/O is assigned during configuration, but this can be overridden by the programmer. Addressing may also be octal, decimal or even hexadecimal.

Figure 8: PLC I/O addressing examples.

Figure 8: PLC I/O addressing examples. Courtesy: Automation LLC

Viewing data types: PLC software allows the user to view a number in many of the formats listed here, removing the need for a calculator. It is not always clear in what format the data is being viewed, but there are often designators. They also may have a signed integer (decimal or Base 10) will have no designator; however a Hex number will have a W#16 prefix, indicating that it is base 16. A REAL will have a decimal point or be expressed with an exponent, while a binary representation may have a prefix or appear as a string of ones and zeros.

Dot fields and separators: If a single bit of an integer is designated, it may be shown with a separator such as a slash or a dot; for example, N7:5/3  or Q3.2. Dot fields are also often used to designate an element of a complex data type, such as a timer. For example, Timer1.ACC designates the accumulated value (integer or double integer) of Timer 1. It is important to understand how memory is addressed for your particular PLC before beginning a program.

Tags: Many modern PLC platforms don’t use numerical data registers at all. Instead, they allow users to create memory objects as required in the form of text strings. Allen-Bradley’s ControlLogix and Siemens’ TIA Portal platforms are examples of this. Most major PLC manufacturers make a PLC with tag-based data. Tags are also called Symbols on some platforms, but a symbol is not necessarily a tag; it may simply be a mnemonic address or shortcut to a register address. Tagnames are downloaded into the PLC and used instead of an address.

Tags are usually created in a data table as required. Instead of numeric addresses such as “B3:6/4” or “DB2.DBW14”, symbolic names such as “InfeedConv_Start_PB” or “Drive1402.ActualSpeed” are created as memory locations. As tags are created, details such as the data type (BOOL, Timer, REAL) and the display style (Hex, Decimal) need to be selected.

Tags have the advantage of being more descriptive than numerical register numbers. In addition, descriptions and symbols from register addresses were only present in the computer and were not downloaded into the PLC. With tags, since the address is the actual register location, a tag-based program can usually be uploaded straight from the PLC.

Figure 9: Example of aliasing. Courtesy: Automation LLC

Figure 9: Example of aliasing. Courtesy: Automation LLC

Also, the same tags from the PLC program can be used in the human-machine interface (HMI) or supervisory control and data acquisition (SCADA) program directly. This saves time rather than having to map PLC addresses to HMI tags. Of course, I/O addresses are still created from the hardware configuration of the PLC, but manufacturers have created various ways to connect I/O addresses with tags. One of the most useful of there is with the ControlLogix platform, where any tag or address can be “aliased” to any other and both show up in the ladder logic (Figure 9).

Data groups: In addition to the “atomic” or elemental data types such as BOOL, Byte, Integer, or REAL as described back in the Data section of this book, individual elements can be grouped.

Array: An Array is a group of similar data types. For instance, an array can be defined that contains ten integers, or 50 REALs, or 32 BOOLs. Data types can’t be mixed in an array. Complex data types such as timers, counters or user-defined types (UDTs) also be placed into an array. Typically, an array will be shown with square brackets, such as Delay_Tmr[6]. This designates element number 7 of the array if the array begins at 0.

Some platforms allow multidimensional arrays to be defined, such as Integer [2,4,5]. This means Integer number five of the fourth group of the second group.

Elements that are composed of more than one data type are known as Structures. A structure may be defined by the programming software, as with instructions, or by the programmer.

User-defined types (UDTs): A UDT is a group of different types of data, or a structure. Later in this course, data types that consist of more than one type of data will be discussed; for instance, Timers and Counters consist of two integers or double integers and several bits, all combined into a structured data type called “timer” or “counter”.

A UDT can only be used with Symbols or Tags; this is because a UDT is not data. Instead, it is a definition of data. After defining a UDT, a tag or symbol must be created using the new data type.

A common reason to build a UDT is to describe an object more complex than a simple element of data. As an example, a Variable Frequency Drive has many pieces of data that may be associated with it. For instance, a motor needs to start and stop. It has various numerical parameters to describe its movement such as commanded speed, actual speed, acceleration, and deceleration. We may also want to know its status: whether it has faulted and what kind of fault has occurred.

Figure 10: User-defined type (UDT) on the left and the tag made from the UDT (right).

Figure 10: User-defined type (UDT) on the left and the tag made from the UDT (right). Courtesy: Automation LLC

On the left is a UDT named “drive” defined in the software, and on the right is a tag made from the UDT (Figure 10). The definition does not get downloaded to the processor; it can only be modified on the programming device.

The sub-elements of the tag are an example of the dot fields described earlier. By making a UDT, many drives can be added to a program without a lot of extra typing. UDTs are in important element in Rapid Code Development.

On non-tag-based systems, UDTs can cause problems if the commented program is not available. Remember: descriptions and non-tag symbols are not kept in the processor. This is why it is difficult to reconstruct a Siemens S7 program if you don’t have the original code; the downloaded data blocks do not contain the names of the elements.

PLC hardware configuration

The first step in beginning a new PLC program is configuring the hardware. This is because different processors have different amounts of memory, and because the addresses for I/O are determined by the configuration. As cards are added, new addresses or tags are generated and available for selection in the program. You can’t write a program until you know the I/O addresses and memory configuration.

Figure 11: PLC hardware configuration example.

Figure 11: PLC hardware configuration example. Courtesy: Automation LLC

Some platforms assign I/O addresses by location in the rack (Slot Number) while others allow the programmer to assign addresses. Usually there will be a default address that can be modified in the properties of the card. As explained later in this course, some brand’s memory allocation can overlap the I/O area, so careful configuration and planning is important. Selecting hardware will often also include entering a hardware or firmware number for each card. If a rack is used, selecting the rack size will be necessary before inserting cards.

Depending on the platform, the next step in setting up a new program is to configure the data areas. Newer tag-based systems allow tags to be added one at a time either locally or globally, but many of the older platforms require data tables to be sized offline.

– This has been edited from the “Maintenance and Troubleshooting in Industrial Automation” book by Frank Lamb, the founder and owner of Automation Consulting LLC and a member of the Control Engineering editorial advisory board. Edited by Chris Vavra, web content manager, Control Engineering, CFE Media and Technology, cvavra@cfemedia.com.


Author Bio: Frank Lamb is founder and owner of Automation Consulting LLC and member of the Control Engineering editorial advisory board.