ExamRobotic

Good vs bad PLC programming

Level: Advanced 5 min read 34 questions

A PLC program is written once and read hundreds of times: during commissioning, at every breakdown and with every change, often in the middle of the night with the line stopped. That is why a good program is not just one that works, but one that any technician can understand and troubleshoot quickly. The difference between a good programmer and a bad one is hardly ever about knowing obscure instructions: it is about order, names, alarms and respect for whoever comes next.

What sets them apart

Bad programmerGood programmer
Everything in OB1, in hundreds of networks in a rowOB1 calls blocks organised by function and station
Variables like M10.3 or “Aux7”Names that say what they are: Gripper_Closed, Conveyor1_Run
The same code copied for each motorA reusable “Motor” block with one instance per motor
“General fault” as the only alarmAlarms that say what is happening, where, and what to check
Loose numbers scattered through the code: 3000, 27, 0.85Named parameters with units in a data block
Online changes nobody wrote downSaved versions with the date and a description of each change
Sequences built from memory bits that set each otherSequences with clear steps that show on the HMI

Structure: everything in its place

OB1 should read like a table of contents: it calls the blocks for each part of the machine in a logical order. A common structure:

OB1 Modes manual / auto Station 1 sequence Station 2 sequence Alarms and HMI Motor FB one instance each Cylinder FB one instance each
A tidy program: OB1 calls blocks by function, and each repeated piece of equipment uses the same block with its own instance.

Names and comments

A well-chosen name saves a comment. Simple rules that work:

Comments should explain the why, not repeat what is already visible. “Turns the output on” adds nothing; “Wait 500 ms so the gripper finishes closing before lifting” does.

Alarms that help

The alarm is the first thing the technician sees when the machine stops. A good alarm says what happened, where, and what to check: “Station 2 gripper not closed within 2 s: check sensor B23 and air pressure”.

The key tool is motion monitoring: every time a movement is commanded, a timer checks that the end-position sensor arrives in time. If it does not, a specific alarm is raised instead of the machine sitting there waiting with no explanation:

// The gripper must close in less than 2 s
#T_Close(IN := "Gripper_Close" AND NOT "Gripper_Closed", PT := T#2s);
IF #T_Close.Q THEN
    "Alarm_Gripper_Not_Closing" := TRUE;
END_IF;

It also helps to record which was the first alarm: when something fails, others usually follow, and the one that matters is the first.

Parameters without magic numbers

A “3000” in the middle of the program says nothing: milliseconds? parts? revolutions? Times, setpoints and limits go in a parameter data block, with a name, units and a comment. That way they are adjusted in one place, even from the HMI, without touching the logic.

Operating modes and start-up

Respect the scan cycle

A good programmer does not wait inside loops, does not write the same output in two places, uses edges where they are needed and gives each timer and each edge its own memory. These are the mistakes you already saw in the scan cycle topic, and they are the ones that waste the most time during commissioning.

Versions and changes

Test before you get to the machine

With a PLC simulator, such as PLCSIM for Siemens, a good part of the logic can be tested in the office: sequences, alarms and fault cases. Every bug found in the office is one hour less of machine downtime during commissioning.

Mistakes that give away a bad programmer

Summary

Test what you have learned

34 practical questions with explained answers. Free, no sign-up.

Start the test