Comparing ladder logic and object-oriented programming

Cover story: Many young automation professionals are comfortable with object-oriented programming (OOP), but ladder logic remains the standard for industrial automation applications. See advantages and disadvantages of OOP and ladder logic.

07/12/2018


Figure 1: Arduino microcontroller hardware is usually programmed with an object-oriented language, typically a basic form of C language. Courtesy: AutomationDirectMany young people in science, technology, engineering, and math (STEM), in FIRST, and other high school technical programs have used or programmed Raspberry Pi, Arduino, or similar microcontrollers. Most, by default, are learning to program using object-oriented programming (OOP) of some type, and thus gravitate towards it when they move into the real world.

However, industrial automation has relied on ladder logic, also called ladder diagram, for nearly 50 years. Programmers with decades of industrial programming experience like it. But they won't be here forever, and the industry is getting this influx of younger workers who are already familiar with OOP. How can we introduce these new workforce entrants to ladder logic? While OOP has advantages and disadvantages, it's a good starting point for ladder logic. 

Understanding OOP

OOP has a longer history than ladder logic, appearing in the early 1960s or before. Current hardware and software available in the "maker" world is adding to its popularity. Leading microcontrollers, such as Arduino and Raspberry Pi, are examples (Figure 1).

Arduino microcontroller hardware typically uses a basic form of C programming. Raspberry Pi is a Linux-based system with many OOP programming platforms such as Python and Java. These low-cost microcontrollers and Microsoft's .NET platform have made OOP available to those who want to learn it. 

OOP advantages

Compared to ladder logic, OOP's advantages include:

  • Code is portable and easy to reuse
  • Is easy to work with math, loops, etc.
  • Is taught in nearly every computer programming course
  • Code can be run on various hardware platforms.

OOP uses objects; it's necessary to understand the concept of objects and their use to grasp OOP. Once an object or modular class has been written, it is easy to reuse and call it multiple times. For example, when an object is created to control a motor—it takes care of all inputs, outputs, and faults. This single object can be used multiple times as instances when several motors are need to be controlled. It's called when needed and creates an instance when used.

Figure 2: This Productivity2000 PLC from AutomationDirect provides an industrial strength hardware platform for executing ladder logic. Courtesy: AutomationDirectEach instance, for each motor, has its own characteristics such as motor off, motor running, motor RPM, motor overload, etc. Much of the programming work is done when the object is first created. It's a different way of thinking than ladder logic, and it is more powerful because once an object is built, it's easy to use and reuse.

OOP also makes it much easier to do complex math, loop computations, arrays, and nested subroutines. It is also taught in nearly every computer programming course in high school, college, and online. The code created is portable and can be run on various hardware platforms. 

OOP drawbacks

Compared to ladder logic, OOP drawbacks include:

  • Higher overhead
  • Steeper learning curve
  • Not as easy to troubleshoot for maintenance personnel
  • Usually must be compiled so source code can be uploaded to the processor.

OOP can have higher overhead than ladder logic because it tends to need more memory and processing power. The learning curve for an OOP programming language can be longer. Significant time, practice, testing and application, likely in a classroom, may be needed to grasp core concepts.

OOP must often be studied by a programmer to follow the code with a tracer or debugger to follow the logic. With this type of high-level programming, it can be difficult to perform monitoring in real-time. The source code must be compiled before it is downloaded to the controller. Often, the source code is not present in the processor memory. This means the source code must be carefully backed up because the compiled code usually is not editable.

With OOP, libraries need to be connected to other resources used during program compiling. If the connections and resources are not understood, it will be difficult to get the program to run. 

Learning ladder logic

Ladder logic is a very simplistic and self-documenting method of coding, and some even debate whether it is even a programming language. It follows the format of ladder-type electrical diagrams used in relay-based control systems, and most people can learn it quickly. It was the only industrial-strength programming language for decades in the machine automation arena and it's still the main programming language used in the automation industry (Figure 2).

Over time, as different people enter the industry from different areas and backgrounds, different languages have been added to the industrial automation toolbox. These include function block programming, structured text, state programming and sequential function chart. These four languages and ladder logic constitute the IEC 61131-3 standard for programming languages from the International Electrotechnical Commission (IEC).

Figure 3: This AutomationDirect Productivity Suite Software main screen highlights the intuitive nature of ladder logic programming, even for those with limited experience. Courtesy: AutomationDirectThe idea behind IEC 61131 is if each vendor follows the standard, at least to some extent, a person can learn the five languages once and easily move among different platforms from different vendors. However, the reality is different.

Basic ladder logic—such as the use of relay contacts and coils-is the same, but the syntax and the user experience when programming, and the details of how to use the programming platform, must be learned for each vendor.

Despite this lack of standardization, ladder logic advantages over OOP include:

  • Well suited for both machine and process control
  • Easy to understand because it's naturally self-documenting
  • Easy to use for troubleshooting the controlled system
  • Easy to debug
  • Source code that can often be stored in the processor.

Ladder logic is well suited for machine and process control, particularly for automating systems with lots of discrete input/output (I/O). Ladder logic has been modified over the years to also handle analog I/O, making it a good fit for many process control applications, which tend to have a higher proportion of analog I/O as compared to machine control applications.

A wide range of skilled, technical, and engineering personnel can learn ladder logic quickly because it's easier to use than OOP. The logic is very systematic and orderly, making it simple to follow and understand due to its self-documenting nature. Each line of code must be true before it turns something on. If there are five motors, there are typically at least five lines of code, providing a high degree of simplification.

Easier to learn

Ladder logic is intuitive for electrical engineers and maintenance personnel. While ladder logic requires a different mindset than OOP, it can be learned quickly, and it takes less time to understand code written by others. It is very clear when the logic is true. Contacts on or off, energized coils, comparing variables, and common math functions are easy to follow-even by someone with limited programming experience (Figure 3).

Its ease-of-use simplifies troubleshooting and debugging as well. It is straightforward and easy to see what is happening when monitoring the logic. 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. Ladder logic can be thought of as a truth table. If the logic on the left is true, the logic on the right is solved or turned on.

Ladder logic source code and descriptors are often stored in the controller. This can eliminate the frustration a programmer experiences when trying to understand a compiled program without access to the source code, a common issue with OOP.

However, when compared to OOP, ladder logic can:

  • Be unfamiliar to computer programmers, information technology (IT), etc.
  • Be difficult for math, text, and data handling
  • Be dependent on scan time
  • Require specialized hardware to execute, such as a programmable logic controller (PLC).

Ladder logic is a symbolic language unfamiliar to computer programmers and IT personnel because it's not something they typically learn in school. Handling math, text strings, and data in ladder logic can be difficult because ladder logic was not originally conceived to deal with these functions.

Ladder logic is also scan time dependent. Larger programs take longer to scan and solve the logic. With ladder logic, the inputs are read, the logic is scanned, the data table and outputs are updated, the overhead communication is performed, and then the cycle repeats. Functions such as interrupts and other programming techniques can be implemented to ensure certain rungs are executed quicker. Although software-based PLCs with ladder logic can run on a PC, often the hardware, such as a PLC, is matched to the programming software, with both purchased from the same vendor. This ensures compatibility, but hinders switching vendors.

Jerry Reaves is the PLC silo product manager at AutomationDirect and has been with the company  for more than 14 years and is a graduate of DeVry University - Electrical and Electronics Engineering. Courtesy: AutomationDirectIn addition to comparing pros and cons of ladder logic and OOP, users should evaluate the existing use of these languages within the environment where they will be deployed. If the plant or facility has standardized on ladder logic, the use of OOP will be discouraged, even if it's a better fit for the application. OOP use is growing, however, and it's expected to co-exist with ladder logic for decades to come. An aspiring automation professional would do well to learn both languages.

Jerry Reaves is the PLC silo product manager at AutomationDirect. Edited by Mark T. Hoske, content manager, Control Engineering, CFE Media, mhoske@cfemedia.com.

MORE ANSWERS

KEYWORDS: PLC programming, ladder logic, OOP

  • Ladder logic and object-oriented programming
  • PLC programming strategies
  • Learning ladder logic and OOP will help.

Consider this

Do your PLC programmers have adequate training?



Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
Engineers' Choice Awards
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by Control Engineering subscribers.
System Integrator Giants
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
System Integrator of the Year
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
How to Maximize Factory Automation Efficiency with Low Cost Machine Vision
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Wireless Reliability in Harsh Environments
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
Human Factors and the Impact on Plant Safety
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
Augmented reality and virtual reality education, autotuning PID control, cybersecurity advice, educating engineers
July 2018
Ladder logic best practices and object-oriented programming, safety instrumented systems, enclosure design issues and challenges, process control advice
June 2018
Discrete and process sensor fundamentals, autotuning controls, system integrator roundtable
Edge Computing
This article collection contains several articles on how today's technologies heap benefits onto an edge-computing architecture such as faster computing, better networking, more memory, smarter analytics, cloud-based intelligence, and lower costs.
Data Center Design
Data centers, data closets, edge and cloud computing, co-location facilities, and similar topics are among the fastest-changing in the industry.
PLCs
Programmable logic controllers (PLCs) represent the logic (decision) part of the control loop of sense, decide, and actuate. Featured articles in this digital report compare PLCs and programmable automation controllers (PACs), industrial PCs, and robotic controllers.
SIDB

Find and connect with the most suitable service provider for your unique application. Start searching the Global System Integrator Database Now!

SCADA standardization, capital expenditures, data-driven drilling and execution
June 2018
Machine learning, produced water benefits, progressive cavity pumps
April 2018
ROVs, rigs, and the real time; wellsite valve manifolds; AI on a chip; analytics use for pipelines
John O. Ayuk, PE, CFSE, PMP, CAP
Automation Engineer; Wood Group
Doug Baker
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
Data Centers: Impacts of Climate and Cooling Technology
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
Safety First: Arc Flash 101
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
Critical Power: Hospital Electrical Systems
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.
click me