Ladder Logic Debugging: Solving Problems in PLC Programs

Learn to fix common issues in PLC programs effectively.
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, I will show you how to debug common problems that you will encounter while programming, testing, and commissioning PLCs.

After programming PLCs, the ability to test and debug code is the most important skill that a PLC programmer can have.

Before being used in a production environment, all of your code has to be thoroughly tested to verify that it does what it is supposed to do in normal and abnormal situations like when a device fails.

Any bugs found during this testing process must be solved since software bugs can lead to lost productive time, damaged products, and unsafe situations in a factory or plant.

Note that this article teaches you how to debug PLC code that is already written. If you want to learn more about how to write PLC programs using the Ladder Diagram programming language, then check out our course CODESYS 1.

Let’s start by looking at the most common type of error that you will encounter: syntax errors.

Syntax errors

A syntax error is a mistake in the syntax, or wording, of your code. Most syntax errors are caused by programmers typing the name of a variable incorrectly.

CODESYS is constantly checking your code for syntax errors while you work and any syntax errors are highlighted with a red squiggly line underneath the source of the error.

When you hover your mouse over a part of your code with an error, a tooltip provides more information about the error that CODESYS has detected.

Syntax Errors

CODESYS also checks for and reports syntax errors when your project is built into executable code. You can build your project manually by clicking on Build > Generate Code. The project is also built automatically before downloading to a PLC.

Build a project in CODESYS

Any syntax errors that are found during a build are displayed in the Messages window at the bottom of the screen. In this case, we can see that the compiler is complaining that the variable vaTankLvl is not defined.

If I’m not sure where in my project this error is coming from, then I can double-click on the message and CODESYS will take me straight to the source of the error.

Messages window in CODESYS

Syntax errors are generally easy to find, understand, and resolve. In this case, I just have to correct the typo in my code to clear the error.

Correcting Syntax errors

Logical errors

Syntax errors are easy to find and fix because the CODESYS compiler highlights them to us.

Logical errors are mistakes made by a programmer when writing his application logic. In this case, the syntax of the code is fine but the logic is not.

Logical errors aren’t highlighted by the CODESYS compiler because the compiler doesn’t know how your logic should behave and therefore can’t tell you if it’s wrong.

In this example, I have accidentally used the same variable, ouOpenFillValve1, twice in my logic.

Logical errors example

The compiler does not report an error because the syntax is fine but the logic won’t work the way I want it to.

Logical errors are found during unit and system testing of a project.

Even experienced programmers accidentally introduce logical errors into their programs and logical errors are one of the reasons why I encourage my students to test their code early and often.

By testing code often, we can find and fix logical errors as they are introduced before the code becomes too complex and difficult to debug.

In this case, a unit test would show that only one valve is opening and I can fix the error by updating the code to use the correct variables.

Correcting logical errors

Timing and sequence errors

Timing and sequence errors are a subset of logical errors that can cause a lot of difficulties for PLC programmers.

Very often, PLC programmers program the logic to control sequential processes. In a sequential process, one step follows another in a pre-defined logical way.

An example of a sequential process is a mixing process. In a mixing process, a tank is filled, mixed, and drained sequentially.

Mixing process

A sequential process is controlled by a piece of software called a sequencer. A sequence error occurs when there is a problem with the sequencer and it does not activate the next sequential step or activates the wrong step.

This can cause a sequential process to behave in an unexpected way or get stuck mid-process.

Very often, sequence errors are caused by timing errors.

A timing error is a specific type of error that occurs because some variables don’t have the correct value at the correct time or the logic is not configured to check the correct variables at the correct time.

In this example, the tank should only have one step active at any time but because of the way the logic is written, someone could press the start button and start a new cycle before the current cycle is finished.

This is an example of a sequence error and a timing error. It is a sequence error because the logic allows multiple sequence steps to be active at the same time. It is a timing error because it allows the process to be started at the wrong time.

To fix this issue, we could update our logic to only allow someone to start the cycle when no other step is active.

Fixing timing and sequence errors

Debugging techniques

Sequence and timing errors can be difficult to solve because it is often hard to find and fix the root cause of the error by reading your code. To solve timing errors most PLC programmers will use a combination of techniques including;

– Trap bits which are test variables that are set to different values in your PLC program to see if a section of code is being executed,

– Traces which are graphs that show the values of a set of variables over time, and

– Breakpoints to stop the execution of a program at a certain time to see what values variables have and then execute the remainder of the program line by line to see where the error is occurring

These are advanced debugging techniques that are outside the scope of this article.


Now you know what the most common types of errors you will encounter in your PLC programs are and how you can resolve them.

Solving errors can be very difficult and frustrating so the best thing you can do as a PLC programmer is write good code that doesn’t have any errors.

Although it’s not possible to write perfect code, you can increase the quality of your code and reduce the number of errors in your programs by;

– Organizing your code well,

– Following a descriptive naming convention,

– Using rung and variable comments to document your logic,

– Regularly having peers review your code to identify improvement areas,

– Keeping regular backups to compare and revert logic if needed

If you want to learn how to write error-free code, check out our course CODESYS 1 Introduction to PLC Programming.

This course shows you how to write and test your first PLC program in CODESYS using best practices that help to keep your code clean and error-free.


In this article, you learned the most common types of errors that you will encounter during your career as a PLC programmer as well as how to find and fix these errors.

You also learned some best practices to help reduce the number of errors that you find in your code.

What other tips do you have to help reduce the number of errors in your PLC programs? Let us know in the comments.

If you would like to learn more about writing error-free code, consider enrolling in our course, CODESYS 1 – Introduction to PLC Programming. This course guides you through writing and testing your initial PLC program with CODESYS, utilizing best practices to ensure your code remains streamlined and free of errors.

CODESYS 1: Introduction to PLC Programming

Join the Top 1% of Automation Engineers

Try for Free

Learn from
industry experts

Start your learning journey today!

with a free 7-day trial, then $25/month
Start Free Trial Now