Gains:
- Ability to produce Ladder logic and Structured Text (ST) structures as drafts with AI
- Ability to configure safety circuits, interlocks and state machines with the help of AI
- Ability to verify AI-generated PLC logic against I/O map and safety requirements
The name of reliability in the factory field is PLC (Programmable Logic Controller). Conveyors, robot cells, filling lines, presses; all controlled by PLCs. PLC programming is a world apart from desktop software: Ladder Logic (LD) is a visual language that mimics relay logic, while Structured Text (ST) is a Pascal-like textual language; both are defined in the IEC 61131-3 standard. In this environment, the most important thing is not functionality but safety; because a logic error can shut down a press at the wrong time, endangering an operator. AI helps in generating ladder and ST drafts, building state machines and documenting logic. However, safety circuits, interlocks and final verification are the responsibility of the engineer and cannot be delegated to AI. In this unit, we cover how to construct and verify PLC logic with AI.
Working Logic of PLC: Scanning Cycle
PLC doesn't just run the code once and it's done; It performs a continuous scanning cycle. In each loop: (1) reads all inputs, (2) executes logic from top to bottom, (3) updates all outputs. This cycle lasts milliseconds and repeats endlessly. This behavior has critical consequences when writing PLC logic:
- The outputs are updated at the end of the loop; If you assign a value to an output in more than one place within the loop, the last assignment wins.
- Logic works from scratch in each cycle; so latch or state variables are used to "remember" state.
- Fast events may be missed if scanning time is extended; critical events are captured by hardware interrupt or high-speed counter.
Tip: When writing PLC logic to the AI say "take into account scan loop behavior, no conflicting assignments to the same output, use latch/state machine for state". AI sometimes thinks like desktop logic and skips scanning behavior.
Ladder Logic and Structured Text
The same logic can be written in both languages. A simple example: a motor starts when the start button is pressed, stops and self-latch when the stop button is pressed.
LADDER (text display): Start Stop Engine---| |------|/|-------------( )--- | Engine |---| |------+ (Motor contact keeps itself -> seal-in)Explanation: When Start is pressed, the Engine is energized; The motor maintains current through its own contact (seal-in). When Stop (NC) is pressed, the chain is cut.
(* Same logic as STRUCTURED TEXT *)IF Start AND NOT Stop THEN Engine := TRUE;END_IF;IF Stop THEN Engine := FALSE; (* Stop always takes priority *)END_IF;(* Note: The Stop button is physically connected to NC (normally closed); so if the cable breaks, Stop=FALSE and the system switches to the safe side. *)
The safety detail here is critical: The stop button is physically connected normally closed (NC). Thus, in case of cable breakage or contact failure, the signal is cut off and the system goes to "stop" direction (fail-safe). If NO (normally open) is connected, the stop will not operate if the cable breaks—a dangerous silent fault. Even if the AI code seems logically correct, this physical connection decision is up to the engineer.
Safety: E-Stop, Lockout and Fail-Safe
Safety in industrial automation is not an issue that can be "handled in software"; It is a layered collaboration of hardware and software. Basic principles:
principle
Meaning
Application
fail-safe
Failure should lead to the safe side
NC contact, fault=stop
Redundancy
Critical function dual channel
Two contacts, cross monitoring
Interlocking
Prevent dangerous situation
The machine does not operate when the door is open
e-stop
Independent emergency stop
At hardware level, independent of PLC
Critical point: E-stop is not left to the software alone. Safety-related emergency stop is often also interrupted at hardware level by safety relays or safety PLC; standard PLC software is an additional layer on top of this. ISO 13849 (machine safety) and IEC 62061 provide the framework for these designs.
Caution: Do not assume that an AI-generated E-stop or locking logic is "software sufficient". Safety functions are designed according to relevant standards (ISO 13849 PL, IEC 62061 SIL), include a hardware layer and are verified by risk assessment. This evaluation is the responsibility of the engineer and the organization; AI can only produce outlines and checklists.
State Machines
Complex automation logic is organized with a state machine rather than a messy if-else: the system finds certain states (Idle, Starting, Running, Stopping, Error) and proceeds through defined transitions. This structure is both readable and verifiable.
(* Simple filling station state machine *)CASE Case OF 0: (* BOSTA *) IF StartButton AND CoverClosed THEN Case := 10; END_IF; 10: (* FILL *) Valve := TRUE; IF LevelFull THEN Valve := FALSE; Status := 20; END_IF; IF NOT CoverClosed THEN Valve := FALSE; Status := 90; END_IF; (* safety *) 20: (* DRAIN *) PURGE := TRUE; IF LevelEmpty THEN Empty := FALSE; Status := 0; END_IF; 90: (* ERROR *) Valve := FALSE; Empty := FALSE; (* all actuators are safe *) IF ResetButton THEN Status := 0; END_IF;END_CASE;
In this structure, safety transitions (go to error state if the cover is opened, close all actuators) are clearly included in every situation. AI generates state machine skeleton quickly; But which safety incident will come out of which situation comes from your risk analysis.
Weak Prompt / Strong Prompt
WEAK:"Write PLC code for a conveyor."(No I/O, no safety, unclear language. Unable to field draft.)STRONG:"Write a conveyor control with IEC 61131-3 Structured Text. Inputs:Start (NO), Stop (NC), PhotocellProductVar (NO), CoverOpen (NC safety).Outputs: ConveyorMotor, WarningLamp. Use state machine (Idle, Running, Error) Stop always takes precedence, go to Error when Lid is Open and shut down the engine. Assume the safety evaluation of this code.
Mini Case
Automation engineer Tolga requests ST code from AI for a packaging line. The AI produces code that appears to work, but when Tolga validates it against the I/O map, he finds two problems. First: AI has treated the Stop button with NO (normally open) logic; Tolga turns this into NC fail-safe logic, because if the cable breaks, the line must stop. Second: the locking, in which the machine must stop when the protective cover is open, is checked only at startup, not during operation; Tolga corrects it so that it is checked in every scanning cycle. It also confirms that the actual E-stop is not left to the software, but is also interrupted in hardware with the safety relay. AI gave its logic skeleton quickly; But the engineer's verification secured three safety decisions: fail-safe connection, permanent locking and hardware E-stop.
Common Mistakes
- Connecting the Stop/E-stop button to NO and losing the fail-safe behavior in case of cable breakage.
- Checking the interlocking (lid, light curtain) only at startup and skipping it in operation.
- Leaving the security function entirely to the software and neglecting the hardware layer.
- Forgetting the scan loop behavior and making conflicting assignments to the same output.
- Making complex logic unverifiable by writing it with messy if-else instead of a state machine.
- Accepting AI's interpretation of the security standard without verifying it with the official text.
In summary
- PLC operates with a continuous scan cycle; The outputs are updated at the end of the loop.
- Ladder and Structured Text express the same logic in two different IEC 61131-3 languages.
- Stop/E-stop buttons NC and fail-safe are connected; failure should lead to the safe side.
- Lockouts are checked every scan cycle; Includes security hardware layer.
- State machine makes complex logic readable and verifiable.
- Safety assessment (ISO 13849/IEC 62061) is the engineer's responsibility; AI generates draft.
Application task
Choose a simple automation scenario (e.g. start/stop self-latch motor, a two-position actuator, or a small filling station). Write down your I/O list and safety requirements (which button is NC, which is interlocking). Have the AI generate a Structured Text state machine with this context. Then verify the output for safety: (1) does Stop provide safe exit from each state, (2) is interlocking checked in each scan cycle, (3) do all actuators go to the safe state in case of error? Make note of any safety deficiencies and corrections you find.