IMDHMIControls LibraryShop main store

How-to

How to verify AI-generated PLC code before you trust it

PLC programming Published 5 min read

Short answer

A clean compile proves your AI-generated rung is syntactically valid for the project, not that it behaves correctly. Write a behavior specification first — states, startup, fault response, reset — then run normal, abnormal and restart tests in an isolated environment before any download to operating equipment. Generated logic earns trust the same way hand-written logic does: by passing a test written before the code was accepted.

On this page
  1. What a clean verify proves, and what it does not
  2. Constrain the assistant to the target controller
  3. Write the behavior specification before generating
  4. The verification routine
  5. Worked example: a totalizer that compiles and over-counts
  6. The decision
  7. Questions asked

Compiling AI-generated PLC logic proves the syntax is valid for your project; it proves nothing about what the machine does. A routine that verifies clean can still run a motor at the wrong moment, let a sequence skip a step, or restart in a state nobody chose. Verification is a separate job: constrain the assistant to your controller and language, write a behavior specification, then run normal, abnormal and restart tests in an isolated environment before anything is downloaded. The origin of the code changes nothing about what proves it correct.

What a clean verify proves, and what it does not #

A verification pass in your programming application checks three things: the syntax is valid for the selected language, the instructions exist for the target controller and revision, and the data types and tag references resolve. IEC 61131-3 standardizes the PLC programming languages — ladder diagram, function block diagram, structured text and SFC — and the verify enforces the rules of that language. That is where its authority ends.

No compiler checks whether a motor stops on the condition the process needs, whether a sequence can skip a step, what happens when a permissive drops mid-cycle, or what state the outputs are in after a power cycle. Those are machine requirements, and they are settled only by review and test.

One execution behavior deserves its own sentence: PLC logic runs repeatedly, every rung on every scan. A condition that stays true executes its action on every scan unless the code limits it to one execution per rising edge — a one-shot instruction (ONS) in ladder, the R_TRIG function block in IEC 61131-3 structured text. A compiler accepts both the limited and the unlimited form. This is where generated code most often diverges from what its author meant.

Constrain the assistant to the target controller #

Hand the assistant the environment before it writes a line:

  • The programming application and revision you actually use — for the Logix line, that is Studio 5000 Logix Designer, the current name of the product formerly called RSLogix 5000.
  • The controller family and firmware revision.
  • The language the routine must arrive in: ladder diagram, structured text or function block diagram.
  • The permitted instruction set, as a closed list.
  • The behavior specification from the next section.

Structured text drafted for another vendor's editor — SCL, for instance — does not compile unchanged in a different manufacturer's application. When the result arrives, check every instruction and every tag name against the project and the application's instruction help; assistants produce plausible mnemonics that do not exist for your revision. You also cannot review code in a language you cannot read: if the instruction set is still new to you, start with learning PLC, SCADA and DAQ from scratch before you accept generated logic from anyone.

Write the behavior specification before generating #

The specification is the contract the code will be tested against, and it is written before generation, not reverse-engineered after. Six entries at minimum:

  • States: every operating state and every permitted transition between them.
  • I/O: each input and output, its data type and its physical meaning.
  • Startup: the state and output values required at first scan after power-up.
  • Faults: what each fault stops, and what it must not stop.
  • Reset: what reset does in each state, and from which states it is accepted.
  • Persistence: which values must survive a power cycle and which must be reinitialized.

If you cannot write the specification, you cannot verify the code — that is the stopping point for the project, regardless of who or what writes the rungs.

The verification routine #

  1. Write the expected results first, one per specification entry: what each output and state variable must show in normal operation, on each abnormal event, at reset, and at restart.
  2. Read every rung or statement. For each output and shared variable, find every place in the program that writes it. Any write you cannot tie to a specification line is a defect.
  3. Move the routine to an isolated environment: an offline controller, a trainer, or a virtual PLC test bench. Generated logic does not go to operating equipment before this routine passes.
  4. Run the normal sequence. Drive every permitted transition, watch the inputs, the internal states and the outputs, and compare each transition against the written expectation.
  5. Exercise the abnormal branches: drop a permissive mid-operation, hold a command true, and command from a state that should refuse it.
  6. Apply reset in every state and check each result. Then cycle power and check the startup state and every value that must or must not persist.
  7. Record the result with the project revision, the code revision and the full test list, and keep that list as the regression set for future edits.

Worked example: a totalizer that compiles and over-counts #

An illustrative example with stated values. Specification excerpt: when Level_Switch and Fill_Permit are both true, add one 25.0 L dose to Batch_Total once per fill. Declared types: Level_Switch and Fill_Permit are BOOL inputs; Batch_Total is a REAL initialized to 0.0 L.

Generated structured text:

IF Fill_Permit AND Level_Switch THEN
    Batch_Total := Batch_Total + 25.0;
END_IF;

This compiles clean. The condition stays true for the whole fill, so the add executes on every scan. Assume a 10 ms scan and a switch held true for 3.0 s: 3.0 s ÷ 0.010 s = 300 executions, and 300 × 25.0 L = 7,500 L added where the specification allows 25.0 L — a 300× over-count no compiler flags. The multiplier is hold time ÷ scan time; with your controller's last-scan time, the same division gives your over-count.

The fix is one rising edge. Declare FillEdge as an R_TRIG instance:

FillEdge(CLK := Fill_Permit AND Level_Switch);
IF FillEdge.Q THEN
    Batch_Total := Batch_Total + 25.0;
END_IF;

The test that proves the result: force Level_Switch true for 3.0 s in the isolated environment and read Batch_Total — it must be 25.0 L, not 7,500 L. Drop it false, make it true again, and confirm exactly one more 25.0 L. That second step proves the edge, not the signal level, drives the add.

The decision #

Four cases cover every outcome:

  • Compiles only: not verified. Write the specification if it is missing, then run the routine; no download until it passes.
  • Passes the normal sequence only: half tested. Abnormal and restart branches are where generated code fails, so the routine is not done.
  • Fails any branch: fix it, then re-run the entire set, not just the failed case — an added edge bit or reset change breaks previously passing cases regularly.
  • Passes every branch with every output write traced to a specification line: accept it into the project with the test list attached, the same as accepted hand-written logic.

Questions asked

Is compiling AI-generated PLC code enough before a download?

No. A successful verify proves the syntax, data types and instruction availability are valid for the target project. It does not prove machine behavior: stopping on the right condition, refusing a command from the wrong state, or restarting in a defined state after power loss. Those are only proven by the specification review and the normal, abnormal and restart tests, whoever wrote the code.

Which instructions need the closest review in generated code?

Edge and one-shot instructions (ONS, R_TRIG), timers and counters whose enable input stays true, math rungs without a trigger, outputs written from more than one routine, and anything stored where it survives a power cycle. In these families the scan-cycle execution and the power-cycle behavior diverge most from the plain-English description the assistant was given.

The assistant wrote RSLogix 5000 code — is that the same as Studio 5000?

RSLogix 5000 is the former name of Studio 5000 Logix Designer, so references to either describe the same product line. Say the current application name, your controller family and your firmware revision in the prompt, then check every generated instruction against that revision's instruction help. Structured text drafted for another vendor's editor, such as SCL, will not compile unchanged in Studio 5000.

How many test cases does a generated routine need?

Count them from the specification: one case per permitted state transition for normal operation, one per way reality can deviate — a permissive lost mid-operation, a command held on, a command from the wrong state — a reset test in every state, and one restart test per group of values that must or must not persist. A two-state routine typically lands at eight to ten cases.

Can I trust test cases the AI writes for its own code?

Treat them as a draft checklist, not as the test plan. The assistant checks its own understanding of the code it wrote, which is exactly the bias you are testing against. Write the expected results yourself from the behavior specification before anything runs, merge in any generated cases that match it, and run every case you accept.

Keep reading

  • Explainer

    What STEP 7 is in a PLC project, and which one you need

    STEP 7 is the engineering software you use to program, configure, diagnose and download to SIMATIC PLCs — a workstation tool, not a part of the PLC itself. SIMATIC Manager, also called STEP 7 Classic, configures S7-300 and S7-400 CPUs; STEP 7 inside TIA Portal configures S7-1200 and S7-1500. Read the CPU family from the module label or the existing project, open the matching environment, and confirm the project's hardware configuration matches the installed CPU before you edit or download.

    PLC programming

  • How-to

    Latch two level inputs for pump start and stop control

    Use one reset-dominant latch: set the pump when the low level probe reads dry and reset it when the high probe reads wet, so the pump holds its state between the probes. A rung that only compares the inputs drops out the moment water covers the low probe; the latch turns the probe spacing into hysteresis.

    PLC programming

  • Troubleshooting

    Start a PID ramp soak profile from ladder logic

    The profile never starts from ladder because the JMP stage handoff leaves the old stage on for three more scans, which swallows the one-scan PD pulse that must set the ramp/soak start bit. Replace the jump with SET and RST stage logic, then verify bit 11, the +33 octal control word and the +35 error flags before touching the PID instruction or table values.

    PLC programming