Software

PLC Software Programming Best Practices

The brain of an industrial automation platform, a Programmable Logic Controller (PLC) monitors system components and makes decisions regarding the control and optimization of their output. 

That is, of course, assuming it’s set up properly. 

To that end, implementing these PLC software programming best practices will help ensure your automation efforts perform as desired. They will also help you develop a scalable system and make finding problems easier. 

Here’s what you need to know. 

Planning And Documentation Are Key

It’s crucial to get clear on the purpose of the system, the components you’ll need and the processes involved — before you start programming. It’s also prudent to work with an eye toward the future. Do you anticipate growth? Will that evolution require bringing in additional equipment with which your PLC will need to interact? 

To that same end, creating a detailed state machine sequential flowchart at the beginning of the project will help you envision the processes you need to accomplish more vividly. It will also reduce the number of errors you encounter and help you make your configuration more flexible. Having a listing of inputs, modifiable parameters and alarms at hand will also make troubleshooting easier. 

Employ A State Machine Structure

Predicated as it is upon events, state machine programming is comprised of a series of states and a case selector to tell the program what to do next. This structure is also easily maintained, readily documented and effortlessly reused.

Use Pages to Optimize Scan Cycles

Start with the main page, then create one that reads inputs, another one that executes a state machine based upon the input and a final one that writes outputs — and always set them up in that order. 

Aggregate Outputs 

Set outputs control in one place with direct output writing. Do not use set or reset for outputs. Should an output vary according to different conditions, using OR/AND in the branch will give you a way to see where control of the output state is coming from. 

Structure Reset Conditions Carefully

Reset conditions should always be placed in a branch preceding the set condition. It’s better for the rest of the program to have an opportunity to notice the fault before it is reset. This is of particular concern with fault bits. 

Eliminate Loops

One open loop could send your program into an infinite cycle and inhibit the execution of everything downstream of the loop. Don’t use them; you won’t have that problem.

Allow For The Division Of Zero

Incorporating division of zero condition in the PLC software code eliminates the possibility of a division for zero error. In other words, provide an instruction along the lines of do A/B only if B!=0.

Set Value Limits

Values inserted from the operator in configuration screens should have limits in case a user makes an input mistake. 

Be Clear About Downloads vs. Uploads

Data sent from the PC to the PLC is downloaded. Data sent in the opposite direction is uploaded. 

Employ Field Buses

Interfacing external devices like multi-meters, sensors and motor inverters using discrete hardwired digital inputs and outputs is more expensive and less safe than using field buses to accomplish the same thing. 

Establish And Adhere To A Naming Convention

Naming files and designating versions of them makes backtracking and troubleshooting easier to accomplish. Similarly, instituting a naming convention for function blocks, variables, I/O devices, and controllers makes it easier to search the program.

Cover Your Six

Anything created by human beings is subject to a failure at some point. Consider what should happen if various components go bad and write fail-safes into your program to cover them. As an example, you can instruct the PLC to maintain a pre-set temperature if a temperature sensor goes bad until the sensor is replaced. Another good idea along these lines is limiting the access of external users to crucial functions. 

Test As You Go

Review your variables, function blocks and code segments as you go along. This way, when you’re done, you’re less likely to have to go back to resolve issues. And, things are more likely to run smoothly during the commissioning process.

PDF The Finished Ladder Program

Create a PDF of the ladder program for future reference, once everything is in place and functioning properly. That way, you’ll have a guide to follow if you ever have to revisit it in the future. 

To recap, following these PLC software programming best practices will help ensure your automation efforts perform as desired. These insights will also help you develop a scalable system with an eye toward the future, as well as make any potential problems easier to find. 

Got any tips we didn’t cover here? 

Please list them in the comments section below.