CODESYS PLC Programming: Tips for Maximizing Performance

Enhance the PLC code efficiency within CODESYS.
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 this article, you will learn about CODESYS PLC programming and how to optimize your PLC code for performance.

Before we talk about how to optimize your PLC code, let’s quickly define what optimization is in the context of PLC programming.

Introduction to optimization

In this article, you will see how we can refactor, or update, a CODESYS PLC application to optimize it for performance.

If you want to learn more about programming PLCs using CODESYS, check out the course CODESYS 1 – Introduction to PLC Programming.

In PLC programming, performance optimization usually aims to lower PLC cycle times and reduce the memory usage of the application.

Low cycle times are important because we want to control processes at near real-time. High cycle times may lead to the loss of control of a process which can create dangerous situations.

Low memory usage is important because PLCs have limited memory resources. Unlike computers which have a lot of memory available, a typical PLC, like the WAGO CC100, has only 32 megabytes available to store an application.

Now that we know what optimization involves, let’s look at the application that we will optimize in this article.

Application introduction

In this article, we will optimize a CODESYS application that monitors a temperature and controls a fan.

When the temperature reaches an upper limit, the fan turns on, and when the temperature reaches a lower limit, the fan turns off.

This sounds like a simple application but it will highlight how many opportunities there are to optimize even the easiest of applications.

CODESYS application

Let’s take a look at the current code and see how we can optimize it.


Optimization starts with an analysis of the current code.

The aim here is to identify parts of the code that we can refactor to simplify calculations, consolidate logic, and improve memory usage by reducing the number of variables used.

Streamlining complex calculations

By simplifying calculations and removing duplicate logic from an application, we can significantly reduce the cycle time of the application.

Eliminating Redundant Logic

To start, we can see that there is a very complex calculation spread across three rungs.

Since the calculation, which converts the temperature in Celsius to Fahrenheit is spread out, it uses two intermediate variables rTempInC1x9 and rTempInC1x9Div5 to store intermediate values for the calculation.

This is a waste of memory and can be optimized by placing the mathematical operators in series.

This is easier to do in Function Block Diagram, so I will create a new POU in FBD to replace the existing one.

In this new POU, I update the logic as shown here, as you can see we have eliminated several rungs from the application and a number of intermediate variables.

Looking at the new program, we can see another chance to optimize it. Why should we scale the analog input to a value in degrees Celsius and then convert that value to degrees Fahrenheit?

Instead, we can scale the analog input directly to a value in degrees Fahrenheit and eliminate 3 instructions from our program to reduce the cycle time.

Optimizing Memory Usage in Function Block Diagrams

Removing duplicate calculations

Switching back to the original program, we can see that the same calculation is done twice. The temperature is calculated on rungs 1 – 4 and again on rung 6.

Duplicate calculations

By removing this duplicate calculation, we can eliminate another instruction and intermediate variable from our program.

Finally, when determining if the fan should run, our original program uses two intermediate variables to control the fan.

Intermediate variables

Once again, if we place our instructions in series, we can eliminate two intermediate variables from our program as shown here.

The end result is a program that is optimized for performance and is, in my opinion, easier to understand, debug, and maintain.

Optimized program

In this example, we optimized the program for performance by re-writing the logic in a more suitable language, optimizing calculations, and eliminating redundant variables.

In other applications, you may also optimize performance using more advanced methods like by re-organizing the POUs in the application, reducing communication overhead, or reducing data processing.

Enhancing Performance Through Code Optimization

Now that we are done with our optimization, we can test and benchmark the results.

Test and benchmark

After optimizing an application, you should check how much of an impact your changes have made to understand the value of the optimization and if the performance targets were reached.

The first thing to check is the reduction in memory use. You can see the memory use of an application when you build the application. to do this, click on Build > Generate Code and check the memory statistics in the Messages pane as shown here.

Test and benchmark

You can also check the reduction in cycle time by downloading the project to a PLC and opening the Monitor tab of the Task Configuration object.

This tab gives some statistics about the cycle time of the PLC including the last cycle time, average cycle time, max cycle time, and min cycle time.

Task Configuration Monitor tab


In this article, you learned how to analyze an application to identify opportunities for performance optimization, implement them, and test the effectiveness of the optimization.

Optimization is very useful in high-speed or memory-critical applications where scan time and memory usage are really important, however, extreme optimization can lead to code that is more complex and harder to follow.

When you are optimizing your PLC applications, you should always think about the trade-off between performance and complexity. In general, new programmers should focus on writing code that is easy to understand and maintain over code that is completely optimized for performance.

If you would like to learn more about effective PLC programming with CODESYS, check out our course CODESYS 1 – Introduction to PLC Programming

CODESYS 1: Introduction to PLC Programming

Join the Top 1% of Automation Engineers

Get started now

Learn from industry experts

Our step-by-step courses are designed by the top 1% engineers around
Get started now