Computer engineer is typing console script code by hand

How can generative AI simplify complex PLC programming?

March 19, 2025
Retrieval-augmented generation (RAG) tools help large language models improve code program organization units

Before you employ an artificial intelligence (AI) solution, make sure you’re not trying to use AI tools, where machine learning (ML) algorithms are all you need. (Read more about the difference between AI and ML in this article.) OEM and integrator engineers are experimenting with the benefits of generative AI for programming code. AI tools to assist with programmable logic controller (PLC) code need to be highly customized, but can work as an assistant engineer for code generation and error detection, and automated documentation.

In this article, learn about a specific tool for making large language models (LLMs) more accurate, techniques to simplify your PLC programming and why that matters to industrial AI.

Get your subscription to Control Design’s daily newsletter.

How RAGs improve LLMs for PLC coding

For now, caution is still advised for any large language model (LLM) tool, says Chris Gibson, director of emerging technology growth at A&E Engineering, a system integrator and CSIA member. “Caution is essential when using generative AI for PLC programming because it can and will hallucinate, meaning it may generate incorrect or misleading information,” he adds. He recommends mitigating this risk through retrieval-augmented generation (RAG) AI systems for code creation. RAG is an AI framework that works with LLMs to be more accurate and relevant, by searching more external data sources and pre-processing information and prompts before they’re integrated into the LLM.

“RAG allows you to train AI with specific knowledge, essentially putting guardrails around its responses. By feeding it approved libraries and best practices, you can ensure that AI-generated code aligns with your standards,” Gibson says. “With RAG, AI can learn machine specifications and coding practices to assist in generating PLC, HMI and SCADA code. This emerging trend will only grow, significantly reducing tedious, error-prone and repetitive programming tasks.”

With traditional AI foundation models, they are pre-trained off-line and do not include data or information that has come into existence after that training. RAG mitigates these shortcomings by retrieving external data and information. RAG also uses that information to enrich the prompt, taking relevant information and data and updating the original prompt, and the enriched prompt is passed to the LLM.

How do program organization units improve PLC coding and how can generative AI help?

Complexity is also an important consideration for all programming languages, says Aaron Dahlen, applications engineer at DigiKey, and AI can assist here. “Parsing a program into smaller program organization units (POUs) is the gold standard for today’s PLC programmer. Instead of constructing a 100-line ladder logic diagram (LD) serpent, we break the code into smaller, more manageable pieces,” Dahlen says.

The long, serpent code is difficult to construct, troubleshoot and maintain. “It’s a poor programming practice that will cost you a considerable amount of money over the lifetime of the machine,” he adds.

Instead, if the code is broken into several smaller POUs, where each one performs a dedicated function, the code is easier to build and troubleshoot. “This is where the AI excels as a partner to help us explore the inner working and the boundaries between POUs,” Dahlen says. “Knowing that any given POU is small, the AI can generally comprehend the POU’s function and purpose within the larger program.”

With this capability, programmers can use generative AI to optimize individual POUs or the full program. They can clarify the POU’s purpose and optimize the scope, structure and name of the variables, Dahlen says.

Using known programming metrics, AI can also estimate program complexity or identify methods for reducing POU complexity. For any given POU, complexity can be defined by measuring the number of decision points, nesting or hierarchical arrangement and total number of operators, Dahlen adds.

AI can help programmers make code easier to read and maintain, by helping in refactoring or improving the code. “The depth of refactoring depends on the specific project. Sometimes, it’s as simple as changing the variable names for clarity. At other times, the programmer will make a key discovery that changes the structure of the entire project. This could be part of a formal code review or an individual programmer exploring the code. On a related note, refactoring is challenging in an industrial environment as a change in PLC code may require extensive verification tests to work out unintended bugs. Sometimes refactoring ends with a TODO statement to identify code that should be updated in the future,” Dahlen says. AI can assist along many steps of this process.

Dahlen notes that he prefers ladder-logic programming for PLCs, in part because ladder logic diagrams are one of the best PLC troubleshooting methods. However, these AI tools work better with structured text (ST). “Today, these tasks are easily accomplished by using ST, as the code may be copied and pasted between the AI and PLC development environment. In the future, we may be able to use LD,” Dahlen says. “Imagine the day when we can talk to the AI and then see the changes incorporated into the ladder logic.”

About the Author

Anna Townshend | Managing Editor

Anna Townshend has been a writer and journalist for 20 years. Previously, she was the editor of Marina Dock Age and International Dredging Review, until she joined Endeavor Business Media in June 2020. She is the managing editor of Control Design and Plant Services.

Sponsored Recommendations

2025 State of Technology Report: HMIs, IPCs and Enclosures

Industrial manufacturing equipment often relies on human-machine interfaces, industrial PCs and enclosures to ensure system reliability and optimal performance. These components...

Custom Encoder Created for Large Rotational Applications

Large rotational applications like MRI machines, excavators, mobile equipment, forklifts and stagecraft require precise motion feedback for optimal performance, safety and efficiency...

See How One Company Customized Motion Feedback for Material Handling Applications

Encoders can be used in material handling on sorters, conveyors, in automated storage retrieval systems, on mobile equipment, automated mobile robots and more. See how one company...

Absolute vs Incremental Encoders: Which One Does Your System Need?

The right encoder makes all the difference. Incremental encoders are perfect for tracking speed and direction in dynamic motion. Absolute encoders? They remember exact positioning...