This post is part 3 of a series.
Most software engineers are used to writing text-based code using a text editor of their choice. Things work a little bit differently in the world of PLCs. To program a Programmable Logic Controller (PLC), you can’t just open up your favorite text editor and start typing away PLC code. First, PLC logic isn’t programmed in text (exceptions apply, see below). Second, you can’t use your favorite programming software; instead you will be using the one programming software that the vendor of your PLC provides.
For my Pycon 2019 presentation about PLCs I brought a PLC on stage for a live demo of PLC programming. Like I wrote above, each brand of PLC has its own custom programming software. Since I wanted to live-code some ladder logic on the presentation screen, I chose the brand of PLC for this demo (Automation Direct’s “Productivity” product line) specifically because the programming software that comes with it is comparatively simple user interface.
Ladders, rungs, hot rails, coils, …
The screenshot below is of Automation Direct’s software, showing the program I used to make my traffic light flash red during the onstage demo:
Ladder logic is designed to mirror “ladder diagrams” that electricians of yesteryear were familiar with. As a result, a lot of the terminology has a distinctly electrical bend:
The name ladder logic comes from the fact that when you write a longer program, it looks like a ladder. Sticking with the ladder analogy, the horizontal lines are called rungs (even if they end up having branches or snake back and forth as it is the case in some editors). The vertical lines on left and right are called rails. The left vertical line is electrified, i.e. has a voltage applied, i.e. feels hot when you touch it and get zapped, hence hot rail. The right vertical line is where all those imagined electrons from the hot rail are trying to get to, hence it’s called the neutral rail.
There are two basic things to know for reading ladder logic:
- Imagine that current wants to travel from the hot rail to the neutral rail. The circuit elements are like switches that either allow or prevent this.
- Always read the ladder from top to bottom, and each rung from left to right. That’s also how (most) PLCs execute the ladder logic.
The circuit elements we place on the rungs are contacts and coils.
Contacts are the equivalents of conditional statements that selectively allow the imagined current from the hot rail to pass. In order to decide, whether current can pass a contact or not, each contact is tied to a boolean variable, which you may consider an input variable into the logic program.
Coils don’t make decisions, instead they are the outputs of the logic program.
If the combination of input variables and ladder layout result in a coil receiving current, the variable associated with the coil becomes
true, otherwise it is
When the PLC is running, the ladder logic program gets evaluated over and over again, usually at fixed intervals. The input variables that drive the contacts might change between ladder evaluations, maybe due to a sensor value changing. After each evaluation, the output variables generated by the coils are used to change the output signals that the PLC is controlling, or maybe they are just stored in memory. What exactly drives input variables, and what exactly output variables are used for is another aspect of PLC programming/configuration not covered in this article (because it’s not done in ladder logic).
History: Relay Logic
The reason this terminology and the electrical analogy with hot and neutral rails and coils and contacts get used in ladder logic, is that this language tries to mirror how control systems were programmed before PLCs existed.
If you go to the top of an old building and figure out a way to get into the elevator control room1, you might still run into something that looks like this:
(This is a better picture of an elevator control room but its license doesn’t allow me to embed it in the blog post or my presentation, so you’ll have to click the link.)
What you see in the pictures are mechanical relays. In the days before PLCs, programming a control system involved arranging relays and connecting them electrically with wires such that the physical arrangement resembles the control logic you desired. The fact that “re-programming” the production line of car manufacturing plants (i.e. rewiring a huge wall of relays) took months or years and delayed the release cycles of US auto manufacturers is, in fact, what provided the original impetus for the invention of the Programmable Logic Controller.
If you have ever worked with a relay before, you might remember that the places where you wire up your control signal are called the contacts. And if you switch the control signal on, a coil gets energized which creates an electromechanical field to flip the mechanical switch which controls the output signal of the relay.
And that is the, very abbreviated, history of how this mysterious programming language called ladder logic came about in the 1960s!
You can have two views on this dinosaur of programming languages.
On one hand, there is what I call the “Hackernews view”. In the world of Hackernews, everything is seen as a startup opportunity and needs disrupting. Take this snippet from a 2008 comment:
A good way to find startup ideas might be to simply go to forums/sites for particular communities, such as the firefighting community, or cnc machine community, and simply ask what types of software they really want, which tasks could be automated by computers/robots.
Many other industries are stuck in technological dark ages - the industrial automation community still programs in ladder logic!
Ironically, this is the only mention of “ladder logic” in 14 years of Hackernews archives.
On the other hand, there’s the anthropological approach of trying to understand why this particular dinosaur is still alive.
The anthropologist will find that ladder logic is the least common denominator between many PLC brands, and that it is part of the curriculum for many automation engineering, technician and electrician degrees. That last part becomes important when you consider the lifecycle of most industrial automation systems: Once it has been assembled and programmed by an integrator, it gets delivered to the factory and ownership transfer to the maintenance department of the factory. This department is staffed with technicians who are now on the hook for keeping this equipment running with minimal downtime, sometimes for decades!
Ladder logic is a language that is primarily designed for debugging and not at all for the person writing it! That is really quite unique, none of the programming languages I work with on a regular basis have this trait (but many could use a small dose of it).
This blog post is not meant to be a tutorial for ladder logic. You can watch the four minutes of my Pycon 2019 talk recording starting at 8:28 for a quick taster. Or take a look at these more structured introductions to ladder logic:
- The Wikipedia page contains, as usual, a good overview.
- This text + video tutorial is one of the many intro level tutorials.
- A list of ladder logic design patterns.
- PLC Fiddle is a web based ladder logic simulator.
Unfortunately, I am not aware of a good comprehensive guide to ladder logic in general. Given that every PLC vendor implements their own flavor of ladder logic with custom logic elements and extensions, vendor sponsored materials and pricey training sessions are where many engineers learn about ladder logic.
Among the PLC vendors, I found AutomationDirect worth looking at for hobbyists who are trying to learn ladder logic. They sell several PLC brands at the low end of the price range, all of which include free programming software. For the live demo in my presentation, I used their Productivity Suite software which has a comparatively easy to use ladder logic editor with detailed documentation.
Languages other than Ladder Logic
Ladder logic is not the only language available for programming PLCs3. This (skipped) slide covers the most noteworthy other languages:
The most important thing to know is that there exists a well-established standard that defines what a PLC is and what requirements it must satisfy: IEC 611314. Part 3 of this standard, IEC-61131-3 specifies five programming languages for PLCs, including ladder logic. IEC-61131-3 was first published in 1993 and has mostly weathered the progress of technology. Of the five languages it specifies, three are still in widespread use:
|Ladder Logic (LL)||graphical||widespread|
|Function Block Diagram (FBD)||graphical||widespread|
|Structured text (ST)||text||widespread|
|Instruction list (IL)||text||deprecated|
|Sequential function chart (SFC)||graphical||rare|
While researching PLC vendors and programming softwares, I got the impression that while ladder logic is the most popular IEC-61131-3 language in the United States, Structured Text is the most common in Europe5. If you want to tinker with Structured Text, you can download a trial version of the CODESYS programming software for free. CODESYS is used either directly or “under the hood” by many European PLC vendors as their programming tool, but also tries to address the hobbyist market with tools like CODESYS for Raspberry Pi.
Compared to other programming languages, usage of the IEC-61131-3 languages seems like the Wild West. The PLCopen organization (not to be confused with the openPLC project) has tried to address this by publishing a set of coding guidelines and a library of generic building blocks.
On the higher end of the PLC market, you will find some brands that allow programming in C++ and even in Matlab. The latter makes a lot of sense to me: Matlab and its Simulink plugin are a powerful tool for developing control strategies (e.g. bytuning PID loops), and PLCs commonly execute control strategies as part of the programmable logic.
On the very low end of the market, some PLCs and PLC-like systems come with non-standard programming tools aimed at beginners and projects with basic requirements.
I didn’t mention any PLC programming languages besides ladder logic during my presentations at all. Both times when I gave this presentation I only had 25 minutes, and the slide covering other languages got cut in my effort to shrink the presentation into the allocated time slot. I did get the appropriate amount of comments and complaints for doing so. ↩
As with most technical standards, you have to pay the copyright holder (in this case: the IEC) a big chunk of money just to get access to the standard text. PLCopen has a short summary of what the other parts of IEC-61131 are about. ↩
What about other parts of the world? I don’t know. Japan is home to several PLC vendors but I wasn’t able to get enough information about their tools to see any trend. Trends in other parts of the world might mostly be driven by which PLC-vendor-containing country they primarily trade with. I would be very curious about China, drop me a message if you know about PLC programming trends there! ↩