7 tips and tricks for PLC programming

Home Blog 7 tips and tricks for PLC programming Back
Blog

Are you a newcomer to the field of automation, or are you an experienced user intending to learn more about the topic? 

Using the correct method for programming your PLC will allow you to use the full potential of both your controller and your system. Creating, editing, and improving the PLC functionality can be made significantly easier by following basic coding techniques. Below, you can find a list of practical tips and tricks for easier PLC programming, allowing you to find more space for improvements to your project. 

Mentioned tips are generally applicable to any PLC; here, the UniPi programmable logic controllers were used for the showcase along with the Mervis - UniPi's official supported software platform offering its users a comprehensible development interface, HMI editor, SCADA interface, and many other useful features. 

1) Plan in advance

Thorough preparations and planning both on the hardware and the software level will save you a lot of time during the programming itself, reduce the number of errors, and improve the overall flexibility of your project. We also recommend keeping in mind that in the future you may need to extend your project by adding new features or components. The more detailed your plan is, the less difficult the realization of your project will be. 

2) Choose the right programming method

Mervis supports a pair of programming methods - Function Block Diagram (FBD) and Structured Text (ST). Which one to choose? The choice should depend not only on your experience level but also on the type of your project, as both methods have their own pros and cons and are suitable for different kinds of projects. 

Function Block Diagram is based on so-called function blocks - segments of the pre-defined code made to perform a specific function. By using the FBD you can easily define the controller behavior routines, set conditions, create input/output counters, etc. Thanks to their intuitive design function blocks are a good choice for beginners or smaller automation project. FBD is, however, less suited to complex projects, as the high number of function blocks required may cause performance drops and longer response times. 

The Structured Text method, having a form of text code, is, on the other hand, suitable for complex mathematical functions or for repetitive conditional tasks. The main advantage of the ST is its ability to encompass even very complex functionality into only a few lines of code. The text code is, however, less user-friendly and is suited rather for the more experienced users. 

An example of a project using the Function Block Diagram 


An example of the Structured Text (ST) 

3): Follow the tried and tested coding methods and structural techniques

During the PLC programming, try not to create overly complex codes, no matter which method you use. For example, an unnecessarily high number of function blocks will make harder not only the orientation in the project but also the search for any potential errors. We thus recommend decomposing the code as much as possible by looking for repeating segments of the code and re-using them in other parts of the project. A practical example: if you are programming an automated heating system in several rooms, you can create a single code structure using the FBD and then simply paste it for each individual room. By doing that, you will save a large amount of time and the resulting code will have a high level of consistency. 

An example of repeated use of a single function block for multiple similar applications

A detailed look into the contents of one of the function blocks shown on the previous picture

4) Make notes, leave comments

During PLC programming, it is easy to lose track of the code, especially when working on complex projects containing dozens of function blocks or hundreds of lines of code. For easier orientation in the code, we recommend to write down notes describing I/O designations, the functionality of a function block, a short guide for the code, etc. With such notes, you can easily orient yourself in the code even after a longer time period and also make the project accessible to other users such as colleagues, maintenance crews, or customers. When used frequently, a string of comments can then form up a comprehensible "manual" for the project, describing in detail its functionality.  In the case of function blocks, you can insert comments into the Mervis IDE interface. ST users then can write their comments directly into the code. 

An example of a comment in the Mervis IDE

5) Create and follow a naming convention

The random naming of valuables or function blocks, especially in complex projects, can make the work much harder not only for you but also for any other person working on the project. We thus strongly recommend creating a unified template for names of variables and/or function blocks and following that template during the entire work on the project. The Mervis IDE allows you to easily re-name variables, I/Os or controllers - this feature is especially valuable when searching through dozens of variables, where a unified naming convention can save you a lot of time and stress. The latest Mervis release also introduced the Search function, allowing you to search for a specific variable and all of its occurrences across the project. 

6) Anticipate issues and create fail-safes

Even the best system will sooner or later experience some kind of error. What will your heating system do if a temperature sensor fails? What will happen if a counter on a controller input overflows? Anticipate such issues in advance during the planning phase, from simple component failure to a serious functionality error caused by unauthorized access, and then create hypothetical scenarios for solving such problems. With these scenarios, you can then easily implement fail-safes, fall-backs, or emergency modes. For example, if a temperature sensor fails in a heating system, you can program the controller to automatically maintain a pre-defined temperature. You can also use these scenarios to prevent human errors by limiting the access of external users to critical system functions. 

7) Test and check your code

In any phase of the project, it is a good practice to regularly check variable values, function blocks, or code segments. By doing that you can prevent any potential errors when starting the system, such as lighting switches not working due to a faulty variable setting, or a failure to switch heating due to a mistake in one of the function blocks. For the purpose of debugging, the Mervis IDE features a dedicated Debugging Mode; in this mode, all variable values are displayed in the IDE without the need of checking them individually, allowing you to quickly look for typos, wrong values or other types of mistakes. The system will also automatically highlight function blocks with incorrect configuration, preventing the system from working as intended. Last but not least, the debugging mode will allow you to display a graph of variable value changes over time. 

Mervis IDE with the Debugging Mode enabled, displaying both variable values and variable value change graph.

Conclusion

The above-mentioned tips and tricks will allow you to use the full potential of your PLC, optimize your system and make the work with the system easier for you and all other users. For further info, you can visit the new Knowledge Base containing Mervis step-by-step tutorials and info about all Mervis components. You can also visit the Mervis product page for more info about the system.

Useful links

Our parts

UniPi 1
Neuron
Axon
Patron
Gate
Mervis

Mervis

51,00 €
42,15 € excl. VAT

In stock > 50 pcs

Subscribe to our newsletter