Every automation project starts the same way — a blank program, a new PLC, and a list of requirements that will change three times before commissioning. This is the first post on this blog, so I figured it makes sense to start with something fundamental: how I approach structuring PLC code before writing a single line.

Why structure matters

In my experience working on robotic cells and assembly lines, messy code is the number one reason commissioning takes longer than it should. When you have 5 robots, stations with different processes, vision systems and external communications with client IT system all talking to one PLC, a flat program structure becomes unmanageable fast.

The approach I use is based on splitting the program into clear layers — motion, safety, communication and HMI — each handled by dedicated function blocks.

Basic state machine example


      // Simple state machine in Structured Text
      CASE eState OF
        STATE_IDLE:
          IF xStart THEN
            eState := STATE_RUNNING;
          END_IF;

        STATE_RUNNING:
          xMotor := TRUE;
          tTimer(IN := TRUE, PT := T#5S);
          IF tTimer.Q THEN
            eState := STATE_DONE;
          END_IF;

        STATE_DONE:
          xMotor := FALSE;
          tTimer(IN := FALSE);
          eState := STATE_IDLE;
      END_CASE;
    

What's next

In the upcoming articles I'll be writing about topics I deal with daily — robot-PLC integration, vision system deployment, OPC UA communication and lessons learned from real projects at the factory floor.

If you work in industrial automation and want to exchange ideas — feel free to reach out on LinkedIn. Always happy to talk shop.