Ladder Logic Best Practices: Optimizing Your PLC Programs

Learn key strategies for improving the readability of PLC programs.
Ladder Logic Optimization - thumbnail
Listen to this article

Hello and welcome back to another article from RealPars, the world's largest online learning platform for cutting-edge industrial technologies.

In today’s article, we are going to talk about optimizing your Ladder Diagram programs for readability.

Before we dive into how to optimize Ladder Diagram programs, I want to point out that there are different types of optimization in PLC programming.

Sometimes, when PLC programmers talk about optimization, they are talking about performance optimization.

With performance optimization, you want to improve the performance of your PLC code to reduce scan times and improve memory utilization.

Other times, we are talking about readability optimization. With readability optimization, you are optimizing your PLC programs for the people who have to work with them.

Readability optimization focuses on making your code more understandable so that it is easier to debug, maintain, and extend.

In this article, we will see how to optimize a program for readability and highlight what a big difference optimization can make to the readability of your programs.

It is very important to make sure that your PLC programs are optimized for readability because in many cases, the PLC program is the only documentation available for a machine after it has been running for many years.

Unreadable PLC code can lead to lost production time for factories and late-night service phone calls for PLC programmers.

Now that we know what you will learn in this article, let’s dive into the content by looking at the application that we will study in this article.

Application example

In this article, we will optimize the code for a simple PLC program that controls a mixing tank.

Note that this article focuses on optimizing an existing application. If you want to learn how to write your own PLC applications from scratch, then I encourage you to check out our course CODESYS 1: Introduction to PLC Programming.

In this application, an operator can press a start button to start a mixing process.

When the process is started, two valves open to fill a mixing tank with input liquids to be mixed.

Once the tank is filled, the valves close and an agitator turns on to mix the liquids.

After a predefined mixing time, the agitator stops rotating and a discharge valve is opened to drain the tank.

Mixing tank

Optimization

The current program is more or less impossible to read. If you don’t know what this program did, you will never figure out what it is controlling or how the logic works.

Not optimized mixing tank program

Let’s see how we can transform the readability of this program with a few simple tweaks.

Naming

The first step that we can take toward optimizing this program is to define logical names for all of the variables that are used in the program. This will help to provide some context about what is happening in the program.

Program optimization by naming variables

In general, you should use a naming convention to name all of your variables to ensure that variable names are consistent throughout your projects.

In this example, I use the prefix in to indicate a variable is a hardware input, ou to indicate that a variable is a hardware output, and var to indicate that a variable is a local variable.

Variables naming convention

Already, it’s more clear what is happening in this program. If you read through it, you can see that the tank is filled until it reaches an upper setpoint level, then the contents are mixed, and then the tank is drained until it reaches a lower setpoint level.

Variable comments

Next, we can add comments to the variables to provide more information about them such as the range of values that a variable can have, the unit of measure of the variable’s value, and information about the hardware that is associated with the variable.

Here, I have added comments to help another engineer understand what a value of True means for each Boolean variable as well as the unit of measure and the range of possible values for inTankFillLvl.

Variable comments

If you trying to debug an unfamiliar program, variable comments can be a lifesaver for helping you understand what each variable represents quickly and without needing to dig through drawings.

Depending on your preference, you can choose to display comments in the Declaration Area only or both the Implementation and the Declaration Area as shown here.

Variable comments in the Implementation Area

Rung comments

As well as variable comments, we can use rung comments to make our programs more understandable.

Where variable comments document variables in a project, rung comments can be used to document logic. You can use rung comments to explain what is happening in a particular piece of code and give more context about why it's programmed that way.

Rung comments

In this example, I have added comments to each rung to explain what is happening in that rung. I have also provided more information about why the logic is programmed in a certain way.

For example, the lower limit for the draining step is set to 1.0 instead of 0.0 as you may expect and the rationale for this configuration is explained in the comment for that rung.

Rung comments example

Engineering units and constants

As well as adding comments to our code, we can make our code easier to understand by scaling raw data to use engineering values and avoiding the use of hardcoded constants.

At the moment, the tank level is expressed in a value between 4 and 20 milliamps but what does that actually mean? How much liquid is in the tank when the sensor reads a value like 10 milliamps? How much liquid has to be in the tank for it to be considered full?

To make it easier to understand what numbers in our programs represent, we can scale values from raw data values like 4 to 20 milliamps to engineering units like 0 to 100 liters as shown here.

Scaling values from raw data

This is better but we can still make this program more clear by removing hardcoded numbers.

Instead of using hardcoded numbers like 4.0 and 20.0, we can use constants in their place. Constants let us give a logical name to values while ensuring that they cannot be changed at runtime.

Engineering units and constants

Now that we have optimized this program for readability, let’s recap what we have done and what a difference it has made.

Conclusion

In this article, you learned how to optimize your PLC programs for readability.

We started the article with a program that was almost impossible to read and understand without taking a long time to analyze the logic and possibly referring to the schematics for the machine.

We improved that program by;

  • Adding logical names that follow a standard naming convention for all of the variables that are used in the program,
  • Adding variable comments that provide more context about variables like the unit of measure, allowed range, and where the value for a variable is coming from,
  • Adding rung comments that explain what is happening in the program and why it is programmed in a certain way,
  • Making the program easier to understand by scaling raw data to engineering units and using constants to avoid the use of hardcoded values in the program.

The end result is a program that is easy to read and understand. As a PLC programmer, it is essential that you write code that is clean and clear so that the people who work on it after you are able to understand, debug, and maintain the code.

Remember that readable code tends to be easier to troubleshoot and extend. This means that you will get fewer service calls from unhappy customers when you write clean, readable code.

If you want to learn more about how to write readable code, then I suggest you check out our course CODESYS 1: Introduction to PLC Programming.

This course teaches you how to write your first PLC program using CODESYS, a completely free, full-featured PLC development system, using best practices.

This beginner PLC course has been developed in partnership with WAGO. Upon completing the course, you will be issued an official certificate of completion from WAGO to certify that you are trained in the basics of CODESYS.

We can’t wait to see you inside.

Join the Top 1% of Automation Engineers

Try for Free

Learn from Industry Experts

Start your learning journey today!
With a 7-day trial, then 25/month
Start Free Trial Now