October 27, 2026

Prompt Engineering for Ladder Logic and Structured Text: A Practical Guide

Prompt Engineering for Ladder Logic and Structured Text: A Practical Guide

The quality of AI-generated PLC code depends heavily on the quality of the prompt. Ask a language model to “write a conveyor program” and you will get something that looks plausible and fits no real machine. Give it the platform, the I/O, the required behavior and your coding rules, and the first draft gets much closer to usable.

This guide shows how to write prompts for Structured Text and ladder logic. One rule applies throughout: the output is a draft for you to review and simulate, and safety functions never belong in AI-generated code.

Why PLC Prompts Are Different

General coding prompts can rely on a model’s broad knowledge. PLC work can’t. A model doesn’t know your I/O list, your drives, your scan time or your plant’s conventions, so it fills every gap with an assumption. Vendor dialects add another trap: instruction names, data types and library blocks differ between platforms, and models sometimes invent ones that don’t exist.

Ladder logic adds a further problem, because it is graphical and language models work in text. And the stakes are higher than usual, since a wrong assumption can move a machine in the wrong way. Good prompts remove those assumptions up front.

The Anatomy of a Strong Prompt

A reliable PLC prompt carries six parts. The diagram below shows them and where the result goes next. Skipping any one invites the model to guess.

Prompt anatomy · 6 parts, then draft and review

Prompt anatomy · 6 parts, then draft and review

The model’s answer is a draft and a list of assumptions; the human review and simulation step comes last and is never skipped.

Prompting for Structured Text

Structured Text suits language models well because it is plain text with familiar programming structure. To get clean results:

•       Ask for a function block, with named inputs, outputs and internal variables, rather than loose code.

•       Specify data types and naming conventions, such as PascalCase names and a prefix for function blocks.

•       Describe state machines explicitly, listing each state, its entry conditions and its exits.

•       Make timers configurable, with presets as inputs instead of hard-coded numbers.

•       Forbid what you don’t want, such as global variables or jumps.

•       Request comments on non-obvious logic, especially anything that depends on scan order.

Prompting for Ladder Logic

A language model can’t draw a ladder diagram reliably, so don’t ask it to. Instead, ask for a rung-by-rung description in text: for each rung, the contacts, coils and timers by tag name, plus a one-line purpose. You then build the rungs in your programming software.

Another route is to request Structured Text first, review it, and translate the logic yourself. If your toolchain supports an exchange format such as PLCopen XML, you may be able to import generated logic, but test any import carefully before trusting it.

When reviewing ladder-style output, look for the classic traps: seal-in circuits that never drop out, one-shots that fire twice, duplicate coils writing the same output, and timers that never reset.

A Worked Example

A weak prompt reads: “Write PLC code for a conveyor.” A strong one looks like this:

Act as a senior controls engineer. Platform: [PLC model and software version], IEC 61131-3 Structured Text.

Task: write one function block, FB_ConveyorControl, for a single-motor conveyor.

Inputs: StartPB, StopPB (stop is normally closed), OverloadOK, JamSensor, all BOOL.

Behavior: Start latches the motor. Stop, an overload fault, or a jam lasting longer than T_JamDelay stops it. A fault must be reset before restart.

Constraints: no global variables, PascalCase names, a comment on every state, TON timers with the preset as an input.

Output: the code only, then a list of assumptions and open questions.

Every line closes a gap the model would otherwise fill with a guess.

Techniques That Improve Results

•       Work in small units. Prompt for one function block at a time, not a whole program.

•       Ask for assumptions. A list of open questions shows you where the model guessed.

•       Write tests first. Ask for test cases derived from the specification before the code exists.

•       Show your house style. Include a short example of your naming and layout.

•       Iterate with specifics. Say exactly what is wrong (“the jam timer never resets”) instead of “fix it.”

•       Start fresh when it drifts. A long conversation can accumulate confusion; restate the spec in a new one.

Common Mistakes

•       Pasting confidential logic or plant details into a tool your organization hasn’t approved.

•       Accepting the first answer without review or simulation.

•       Requesting an entire machine program in one prompt.

•       Leaving out scan-cycle and timing context.

•       Trusting instruction names without checking them against your platform’s documentation.

The Bottom Line

Prompt engineering for PLCs is mostly good specification writing. Spell out the platform, the signals, the behavior and the rules, and ask the model to expose its own assumptions. Then review, simulate and test as you would any code you didn’t write yourself.

No comments: