6703f78cdbd68efa814b7c5d Shutterstock 2294141741

Object-oriented programming vs. ladder logic in PLCs

Oct. 7, 2024
Could you be using OOP without even knowing it?

A manager recently said all his people used ladder logic and didn’t know object-oriented programming (OOP). He went on to explain that the company wants to be able to reuse code.

In essence, even ladder programmers are using OOP because they are utilizing premade functions that are in the default libraries and putting that in their ladders. For example, timers are premade functions already in many libraries, and those are used throughout ladder logic.

Get your subscription to Control Design’s daily newsletter.

OOP is simply software that is divided into objects. An object can be thought of as a block, a unit or a cannister—the list goes on. But most describe this piece of software as an object so that, when you have a discussion, others may follow.

Within that object, the descriptions, associated data, procedures, interfaces or methods are included and combined to make an element. An element is typically a function. For instance, a motor starter is something that can be found in almost any system. To create an object that is repeatable in a PLC program, or library, the first step is to understand what is being interfaced with physically. Then describe the inputs and outputs in software, determine data types and then create the functionality in the function block that is wanted to be repeatable. Physically, a motor starter is described electrically as shown in Figure 1.

Taking the physical functionality and describing it in code may equate to the function block shown in Figure 2.

The data table is in Figure 3.

Now, the structure is set up to allow the object to be reused. Some ladder logic in the function block could look like Figure 4.

The function block code could also be written in structured text (Figure 5). Both blocks would act the same way.

Once the block is made, then it is usable for being called in a routine. The example in Figure 6 shows the function being called twice. The code is not made around it because the idea is just to show the objects usability. The code in the block is also incomplete for this example because alarming and auxiliary contact were not dealt with. The idea is to inspire the visualization of object-oriented code and how it can be reused.

Let’s review the steps to creating a reusable object. First, understand the physical functionality. Next, list out the inputs and outputs that interface with the PLC. At this time, decide if these inputs and outputs are integers or Boolean and assign a type. Then write pseudo code for the logic required to run the function. Put that pseudo code into executable code in the function-block logic. Remember that it needs to cover the generic function so that it can be repeatable. After that, program, compile and test. Once the bugs are worked out, then it is sharable with the rest of the controls group. The advantage of these reusable code blocks is that code starts looking the same, and it saves time.

Sponsored Recommendations

2024 State of Technology Report: PLCs and PACs

Programmable logic controllers (PLCs) and programmable automation controllers (PACs) are the brains of the machine in many regards. They have evolved over the years.This new State...

2024 State of Technology Report: Packaging Equipment

Special considerations and requirements make packaging equipment an interesting vertical market unto itself. This new State of Technology Report from the editors of ...

High Sensitivity Accelerometers to Monitor Traffic and Railroad Vibration for Semiconductor Manufacturing

This paper examines highly sensitive piezoelectric sensors for precise vibration measurement which is critical in semiconductor production to prevent quality and yield issues....

Simulation for Automation Guide

How digital twin solutions are expanding the capabilities of plant engineers.