Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

ChatGPT for Ladder Logic

Save for later
  • 17 min read
  • 22 Aug 2023

article-image

Introduction

ChatGPT is slowly becoming a pivotal player in software development.  It is being used by countless developers to help produce quality and robust code.  However, many of these developers are using ChatGPT for text-based programming languages like C++ or Java.  There are few, if any, tutorials on how ChatGPT can be utilized to write Ladder Logic code.  As such, this tutorial is going to be dedicated to exploring how and why ChatGPT can be used as a tool for traditional Ladder Logic programmers.

Why use ChatGPT for Ladder Logic?

The first step in learning how to leverage ChatGPT is to learn why to use the system.  First of all, ChatGPT is not a programmer, nor is it designed to replace programmers in any way, shape, or form.  However, it can be a handy tool for people that are not sure how to complete a task, need to produce some code in a crunch, and so on.  To effectively use ChatGPT, a person will have to know how to properly produce a statement, refine that statement, and, if necessary, write subsequent statements that have the right amount of information for ChatGPT to effectively produce a result.  In other words, a ChatGPT user still has to be competent, but when used correctly, the AI system can produce code much faster than a human can, especially if the human is inexperienced at a given task.

In terms of industrial automation, ChatGPT can be an especially attractive tool.  It is no secret that many PLC programmers are not formally trained developers.  It is common for many PLC programmers to be maintenance technicians, electricians, or other types of engineers.  In any case, it is common for many people who are forced to write complex PLC software to have little more than previous experience guiding them.  As such, when faced with a complex situation with little to no resources available, the programmer can often be lost with no hope of finding a solution.  This is where ChatGPT can be utilized as a user can pose questions and task the system with finding solutions.  With that, how do we use ChatGPT at a basic level?  

How to use ChatGPT?

The key behind using ChatGPT is writing what are called prompts.  In a lay sense, a prompt is a query or command that ChatGPT runs.  In other words, a prompt can be thought of as a task you ask ChatGPT to do.  For example, a person could input the following prompt into ChatGPT:

Tell me how to install a Beckhoff PLC.

In this case, ChatGPT will give you a high-level overview of the basics of installing a Beckhoff PLC.  However, a prompt like this will usually not return any useful results unless you want general, high-level information.  Generally, to get a viable answer, you’ll need to hone the prompt with detailed information and exceptions.  Learning to write effective prompts is a skill that is in the early stages of development.  It is important to remember that ChatGPT is a novel tool. This means that the IT and automation industry as a whole is still learning how to use it effectively.  However, a general flow of writing and refining prompts can be summarized in Figure 1.

chatgpt-for-ladder-logic-img-0

Figure 1 – Prompt Refining Flow

This is just a general logic flow for massaging a prompt to get the desired result.  However, sometimes it is necessary and easier to run subsequent prompts instead of refining existing ones.  Either way, it may take a couple of tries to get what you need out of ChatGPT.  Regardless, to explore how to use ChatGPT, let’s start by creating a basic AND gate. 

AND Configuration

As everyone knows, an AND configuration is simply two contacts in a series.  For the coil to turn on, both contacts have to be on at the same time.  This a relatively simple Ladder Logic program to write and understand, so we’re going to use it as a test for ChatGPT. 

To begin, we’re going to use the following as a prompt:

Write a ladder logic program for an AND gate

When this prompt is run, it’ll generate the following network. 

 NETWORK

--| |--[ ]---[ ]---( )

                                                                                                                                  |          |

     |----[ ]---( )

chatgpt-for-ladder-logic-img-1

Figure 2 – AND Gate: Attempt 1 With Description 

In this example, an and gate is produced, but it also generates an extra rung that wasn’t asked for or necessary for the task at hand.  In this case, it is not easy to determine what the extra run does or why it was included; as such, this is a prime example of needing to refine the prompts to only produce what is needed.  To do this, let’s refine the prompt a little more to match the following:

Write only the necessary contacts and coils for an AND gate in Ladder Logic

When this query is run, it will produce the network in Figure 2:

NETWORK

--| |--[ ]---[ ]---( )

chatgpt-for-ladder-logic-img-2

 Figure 3 – Refined AND Gate: Attempt 2 With Description

In this refined attempt, ChatGPT produces a very simple network of contacts in series.  There are no extra rungs or ambiguous instructions. 

In short, the moral of this exercise is that we started with a general prompt that produced a working result but a result that needed to be refined. We then refined that prompt and produced a high-quality network with only the necessary instructions for a working AND gate.  With that, let’s look at something a little more complex, timer networks. 

Timer Programs

As every PLC programmer knows, most PLC programs will usually include a timer of some type.  So, with that in mind, let’s explore how ChatGPT handles timer programs.  To begin, let’s have ChatGPT generate a simple 10-minute timer.  To do this, we can use the following query:

Write a ladder logic program for a 10 minute timer.

Depending on your usage of ChatGPT, you may get a result without the variables labeled or even in Structured Text.  However, the prompt can be adjusted to produce the desired output by using subsequent prompts that are akin to the following:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

I need this in ladder logic

or,

assign the variables in the diagram

As stated before, you won’t have to re-write the initial prompt. All you have to do is run these statements as if they were your initial prompt.  However, depending on the prompts you run, your result should be something akin to the following:

VAR
    Timer: TON;         (* Timer for the 10-minute duration *)
    StartButton: BOOL;  (* Input for starting the timer *)
    Done: BOOL;         (* Output to indicate when the timer is done *)
 
END_VAR
 
NETWORK
-----[StartButton]----[/Timer]-----
   |                            |
   |                            |
   |                            |
---|----------------------------|----
  NO                           |
                               |
                               |
                              ---[Done]---

In the above ladder logic program:

  • StartButton represents the input contact that starts the timer.
  • /Timer represents the timer coil, indicating that the timer is active and counting.
  • Done represents the output contact, which becomes closed (energized) when the timer reaches 10 minutes.

When the StartButton input contact is activated, and the timer starts counting. Once the timer reaches 10 minutes, the Done output contact will close, indicating that the timer is done. When the StartButton input contact is deactivated, the timer stops counting, and the Done output contact opens.

Please note that the specific syntax and representation of timers may vary depending on the PLC programming software or the manufacturer's conventions. Adjust the syntax and parameters as per the specific requirements of your PLC system.

As anyone who has programmed in Ladder Logic before can see, this isn’t standard notation for a timer function.  However, you probably noticed that each prompt will generate a relatively short description of how the snippet is supposed to work.  If you read the subsequent output, you can piece together the symbolic nature of the snippet.  With that, let’s move on to something a little more advanced.  Let’s explore building a cascading timer network that will have one timer set for 10 minutes and another set for 15, and when the final timer turns on, it will power an LED.  To do this, we can input the following prompt,

Write a cascading timer Ladder Logic Network that has one timer set for 10 minutes and the other set for 15 minutes. Turn on an LED when both timers turn on. Give the necessary variables as well.

This prompt will produce the following:

VAR
    Timer1: TON;     // Timer 1 (10 minutes)
    Timer2: TON;     // Timer 2 (15 minutes)
    LED: BOOL;       // Output LED
END_VAR
 
NETWORK
    // Timer 1 (10 minutes)
    Timer1(IN:= TRUE, PT:= T#10M);  // Set Timer 1 to 10 minutes duration
 
    // Timer 2 (15 minutes)
    Timer2(IN:= Timer1.Q, PT:= T#15M);  // Start Timer 2 when Timer 1 completes
 
    // LED turns on when both timers complete
    LED := Timer1.Q AND Timer2.Q;
END_NETWORK

In this ladder logic program:

  • Timer1 is a timer variable of type TON set for 10 minutes duration.
  • Timer2 is a timer variable of type TON set for 15 minutes duration.
  • LED is a boolean variable representing the output LED.

Here's a breakdown of the logic:

  • Timer1 is triggered to start with the input IN:= TRUE and is set to a preset time of 10 minutes (PT:= T#10M).
  • Timer2 is started when Timer1 completes its timing sequence (IN:= Timer1.Q), and it is set to a preset time of 15 minutes (PT:= T#15M).
  • The LED is turned on (LED := Timer1.Q AND Timer2.Q) when both Timer1 and Timer2 have completed their timing sequences.

You can adjust the timing durations of the timers according to your specific requirements in your PLC programming software. Additionally, you can connect the LED output variable to the appropriate LED indicator in your setup.

As can be seen, this isn’t our desired output.  We want Ladder Logic, and this is Structured Text.  In short, ChatGPT got confused.  If you read the description, ChatGPT thinks its result is Ladder Logic.  In all, this isn’t what we were looking for, so we can add a subsequent prompt that reads the following:

I need this program in Ladder Logic

This prompt will produce the following output:

NETWORK
--|----[ ]----[/]---[ ]----[/]---[ ]----[/]---[ ]--+
  |              |              |              |
  +---[ ]----[/]---[ ]----[/]---[ ]---[/]---[ ]--+
    Timer1         Timer2
    +---[ ]---+    +---[ ]---+
    |         |    |         |
    +---[/]---+    +---[/]---+
      LED            LED

In this ladder logic program:

  • Each [ ] represents a normally open (NO) contact.
  • / represents a normally closed (NC) contact.
  • The timers Timer1 and Timer2 are cascaded to achieve the desired timing sequence.
  • The LED output is controlled based on the completion of both timers.

Here's a breakdown of the logic:

  • Timer1 is a timer with a preset time of 10 minutes.
  • Timer2 is a timer with a preset time of 15 minutes.
  • When Timer1 completes its timing sequence, it energizes the output coil [/], and the LED associated with Timer1 turns on.
  • Timer2 is enabled when the output coil [/] of Timer1 is energized.
  • When Timer2 completes its timing sequence, it energizes the output coil [/], and the LED associated with Timer2 turns on.
  • The LED output is controlled by the parallel combination of Timer1's output coil [/] and Timer2's output coil [/]. The LED turns on only when both timers have been completed.

Please note that the ladder logic diagram provided is a visual representation, and the actual implementation may vary depending on the PLC programming software or hardware you are using.

This output is closer to what we’re looking for, but depending on a person's experience level, this probably won’t be much help.  This particular diagram isn’t very descriptive of how everything is supposed to fit together, so if a person had to translate this to a specific programming software, this diagram would probably take a while to fully digest and implement.  As such, we’re going to have to add another prompt that will be as follows,

I need the variables assigned to the proper contacts.

This subsequent prompt will produce the following:

NETWORK
--|----[StartButton]----[/Timer1.Enabled]---[Timer1.TimerDN]----[/Timer2.Enabled]---[Timer2.TimerDN]--+
  |                                                                                             |
  +---[Timer1.Timer.Q]----[/LED1]---+---[Timer2.Timer.Q]----[/LED2]---+
                                    |                                   |
                                    +---[/LED]--------------------------+

In this ladder logic program:

  • StartButton is a boolean variable representing the input contact [StartButton] that starts the timer sequence.
  • Timer1.Enabled and Timer2.Enabled are boolean variables representing the input contacts [/Timer1.Enabled] and [/Timer2.Enabled] that enable the respective timers.
  • Timer1.TimerDN and Timer2.TimerDN are boolean variables representing the output contacts [Timer1.TimerDN] and [Timer2.TimerDN] that indicates the completion of the respective timers.
  • Timer1.Timer.Q and Timer2.Timer.Q are boolean variables representing the output contacts [Timer1.Timer.Q] and [Timer2.Timer.Q] that indicates the running state of the respective timers.
  • LED1 and LED2 are boolean variables representing the output contacts [/LED1] and [/LED2] that control the LEDs associated with Timer1 and Timer2.
  • LED is a boolean variable representing the output contact [/LED] that controls the final LED indicating the completion of both timers.

Please note that the actual variable names may vary depending on the PLC programming software or hardware you are using. You can assign these variables in your PLC programming software and connect them to the appropriate input and output terminals to implement the desired cascading timer logic

This result is much better as it shows how to wire the network together while the description gives a clear overview of how everything works.  With this, a person with a basic knowledge of Ladder Logic will be able to piece this together and port the rungs to other PLC programming programs. 

Summary

ChatGPT can be leveraged as an excellent tool to help Ladder Logic developers with concocting specialized programs.  However, ChatGPT is NOT a replacement for programmers, and to effectively use the system, a person must be skilled enough to write descriptive prompts and interpret the results.  This means that though ChatGPT is an excellent tool, it does not have the intuition nor the skill to fully replace a programmer.

A big part of using ChatGPT is learning to write and refine prompts as well as subsequent follow-up prompts.  These prompts are a developing art form that probably will be the next iteration of software development.  For now, the art of using ChatGPT and similar systems is novel, and there aren’t any definitive standards that govern how to effectively use these yet, especially when it comes to graphical programming such as Ladder Logic.  When used by a knowledgeable person that has a basic idea of PLC programming and ChatGPT, it can be a great way of getting over hurdles that could take hours or days to solve. 

Author Bio

M.T. White has been programming since the age of 12. His fascination with robotics flourished when he was a child programming microcontrollers such as Arduino. M.T. currently holds an undergraduate degree in mathematics, and a master's degree in software engineering, and is currently working on an MBA in IT project management. M.T. is currently working as a software developer for a major US defense contractor and is an adjunct CIS instructor at ECPI University. His background mostly stems from the automation industry where he programmed PLCs and HMIs for many different types of applications. M.T. has programmed many different brands of PLCs over the years and has developed HMIs using many different tools.

Author of the book: Mastering PLC Programming