In this article, I won’t delve deeply into the IEC standard describing graphical and textual programming languages for controllers, as there are many such texts in industry journals or online. My main goal is to give you a straightforward overview of the languages available to you when you embark on programming an automation system using a PLC controller. We have graphical languages at our disposal, and if you haven’t worked with automation before, these languages are a good place to start your journey! Next, it’s essential to explore textual languages (as they speed up project execution).
Programming Languages for programmable logic controllers
IEC 61131-3 is an international standard for programming in the automation industry. It was developed by the International Electrotechnical Commission (IEC) and defines standards for programming languages, development environments, and user interfaces for industrial automation systems. This standard ensures interoperability of various automation systems by establishing common standards for programming and communication. As a result, it enables easy creation and integration of different automation systems into a single, cohesive whole. The IEC 61131-3 standard is widely used in the industrial automation industry worldwide.
The IEC 61131-3 standard defines five programming languages for industrial automation systems. These are:
- LD (Ladder Diagram): Electricians favor this language for its simplicity and intuitive design, as it uses ladder diagrams to create programs.
- FBD (Function Block Diagram): Electrical and electronic engineers use this language to program with function blocks.
- SFC (Sequential Function Chart): Process engineers and automation professionals rely on this language to create programs with sequential diagrams.
- ST (Structured Text): Programmers and software engineers use this structured textual language for programming.
- IL (Instruction List): Electricians and electronics technicians use this language to create programs with instruction lists.
All these languages comply with the IEC 61131-3 standard and can be used for programming PLC controllers in industrial applications. Each of these languages has its own advantages and disadvantages, so the choice of language depends on the individual needs and preferences of the programmer.
Ladder Logic in Automation
In automation jargon (yes, such a thing exists!), this is commonly referred to as the “ladder.” After all, the English word “ladder” translates to “drabina” in Polish 🙂 The name of the language is actually quite fitting because, when looking at the structure of a program written in this language, you can notice a resemblance to a ladder. I don’t know the exact history of programming, as I’m a bit of a futurist and don’t look back much, but this was probably the first language created for a new type of worker emerging on the job market—automation specialists. The first automation specialists evolved from electricians, and someone smart thought to create a language that would resemble an electrical diagram! Here, we have contacts at our disposal—just like with contactors, we can create automation systems—and we have coils available, just like in contactors. This means that someone who previously created automation systems using only contactors and electrical diagrams could seamlessly transition to PLC controllers.
Apart from the PLC controller itself, someone smart also had to develop a graphical programming interface for users. Do you remember the DOS system? I’m from the 90s generation and had some experience with it (you could even run games on it). If you remember DOS, you know that the GUI was very limited, but that’s where the first PLC programming systems were created, so they used characters available on the keyboard—lines, brackets, dashes, etc. This was enough to graphically represent NO contacts, NC contacts, coils, timers, and other basic elements.
Evolution of PLC Software and the OR Logic Gate
If you open modern software for PLC programming, like Codesys 3.5, you’ll notice that this convention has remained to this day.
I remember that I also started with this language, and it was simply about mapping an input to an output on the PLC, which basically meant lighting up an LED with a button. I remember the incredible joy when I flipped the switch, and the LED lit up, and after modifying the program, it even started blinking. Anyway, every automation specialist, regardless of whether they are old or young, must know the Ladder language—this is the starting point.
In Ladder language, programmers divide a program into so-called Networks, which are sequentially numbered lines of code. This setup is very convenient because it lets us break the program into sections, making it much easier to analyze the structure during machine maintenance.
Fun fact: the largest program I encountered in Ladder had 1,200 Networks and no subprograms. It came from an imported machine from Asia, where they particularly favor Ladder language. In Europe, the approach is a bit different—programmers typically divide programs into function blocks, functions, and subprograms, which, in my opinion, makes it more readable.
This version uses active constructions to enhance clarity and readability.
What program can you write to start? Try writing a program for a star-delta starter for an induction motor.
FBD (Function Block Diagram) Language
Another graphical language frequently used by automation specialists. When analyzing the syntax of this language, we can find many similarities to the Ladder language. There’s a division into program lines, called Networks. In Ladder language, logical functions are implemented by arranging elements like coils and contacts in specific ways. In FBD, we use the appropriate logic function blocks to build the program’s structure. Importantly, depending on the PLC manufacturer, certain languages are more or less developed. I feel that in Codesys 2.3, FBD is much more user-friendly than Ladder.
If you get the chance to program large production lines, you’ll notice that most companies write their main programs in this language. Large companies usually establish a programming standard for their plants. A friend of mine worked in several BMW factories worldwide, and he mentioned that they used FBD for main programs. Remember, in most cases, companies choose a graphical language for the main program, as it makes it easier for technicians to analyze the program, especially during a machine breakdown. The maintenance department often works under time pressure, frequently during night shifts—something to keep in mind 🙂
SFC (Sequential Function Chart) Language
This language is primarily for automation specialists working in process automation, such as water and wastewater systems. Of course, it can be applied in any other area of automation as well. So, what does programming in this language involve? It’s a bit different from the other languages. The key features of SFC are:
- The program consists of a flowchart.
- The program is divided into individual steps, with each step executing specific instructions contained in blocks. The blocks in each step can be written in any language, such as LAD, FBD, or ST.
- The blocks in consecutive steps contain two actions: an entry action, which is executed only once upon activation of the step, and an exit action, which is executed only once before exiting the step. These actions can be written in any language.
- Before moving to the next step, a transition condition is checked. When this condition is met, the program proceeds. A transition is essentially a control flow condition.
For practice, you can, as usual, write a simple program for a pedestrian crossing—an obvious sequence for changing traffic lights. Another option is to write a program for a rotary table with drilling and lubrication of the drilled material. A final example could be an automated coffee machine 🙂
ST (Structured Text) Language
In my opinion, we now come to one of the most important programming languages for an automation specialist—ST (Structured Text). It’s a textual language that closely resembles Pascal in its syntax. That might not mean much to you, but what’s important is that once you learn this language, the time needed to write your programs can drastically decrease.
From my experience, a program that took me twice as long to write in graphical languages took only half the time in ST. It’s well known that if you can do something faster and better, you earn more—and believe me, this rule is especially true for automation system programmers.
The advantages of ST are:
- Simple syntax
- Easier program analysis
- Faster programming
If you take a look at ST syntax, you’ll quickly notice similarities to high-level languages like Python, Pascal, or C. You’ll find loops, conditional statements, operators, etc. I recommend learning ST to both advanced users and beginners alike.
IF iTemperatura < -20 AND iWilgotnosc > 100 THEN (* sprawdź warunek dla temperatury i wilgotności *)
xGrzalka := TRUE; (* włącz grzałkę *)
xWentylator := TRUE; (* włącz wentylator *)
ELSE
xGrzalka := FALSE; (* wyłącz grzałkę *)
xWentylator := FALSE; (* wyłącz wentylator *)
END_IF;
IL (Instruction List) Language
I won’t spend too much time on this plc programming language, although some automation specialists who service older machines should know it. I encountered this language a few times at a Bosch factory; some of the machines dated back to the 1980s. Currently, no automation manufacturer trains or promotes programming in this language. Programming in IL is too tedious. IL is similar to assembly language—it allowed for memory optimization and management on PLC controllers. In the 21st century, I don’t recommend using this language—unless you have a different opinion, in which case feel free to leave a comment on this article.
Can I Use High-Level Languages?
A good automation specialist, in addition to knowing the IEC standard languages, should also know one or more high-level languages. This wasn’t always required, but now it’s essential for entering new projects and automating certain repetitive tasks. Automation manufacturers increasingly highlight the additional possibilities that come with knowledge of high-level languages. For example, the operating system of WAGO controllers is Linux, allowing you to write your own libraries in C or C++ and then call them from Codesys. This is a great option for saving time and engaging automation specialists with knowledge of high-level languages, especially for repetitive or scalable projects.
Advantages of High-Level Languages in Automation
Linux, of course, means that you can also write scripts in Python! At the operating system level of WAGO controllers, you can write scripts to handle specific tasks, such as the MQTT protocol or creating CSV files. Some may ask, “But why? I can do this in Codesys.” True, but Python can perform some tasks faster—it’s a high-level language with many libraries for data analysis, file handling, communication, and more. In Codesys 3.5, Python can also automate repetitive tasks within the graphical user interface (GUI), like automatically adding controllers to a project, I/O cards, tags, and other elements.
Another important plc programming language to remember is Visual Basic, primarily for writing macros in Excel. Good old Excel 🙂 It’s an invaluable tool in many cases. Spreadsheets, charts, and VBA scripting offer valuable solutions for automation specialists. These tools are useful in every phase of an automation project. In Excel, you can create cost estimates for components and develop custom pricing lists or catalogs with VBA scripts. I’ve even seen a mini SCADA system written entirely in VBA! Finally, I’ll mention C++. It’s an essential language, as many SCADA system providers allow scripting in C++. So, if you want to expand your automation skills into SCADA systems, C++ will undoubtedly help you a lot.
Integrating Matlab and Simulink for Advanced Control
Here’s a fun fact: if you studied automation, you’ve likely encountered Matlab and Simulink. I didn’t like either program and had serious issues with them. Then I discovered that with Simulink, you can program the X-20 PLC from B&R. Here’s how it works: you implement the entire control algorithm in Simulink. Then, using a special plugin for Automation Studio, you generate C code and upload it to the controller. Initially, I was skeptical because the process seemed complicated. After all, we typically use Ladder for PLCs! But it turned out everything worked perfectly. And this wasn’t a simple project with basic input-output control. It was a complex algorithm for estimating parameters based on motor speed and current measurements. B&R’s “Target for Simulink” solution is mainly used by R&D departments, for instance, in aerospace or automotive industries.
Our company, ControlByte, keeps up with the times, constantly exploring new programming possibilities in the automation market. One interesting solution is the Finder Opta controller, which can be programmed in a variant of C. Below is an example code for controlling blinds in a smart home.
Here’s the translation of the text:
/**
Home Automation with Opta (Application Note)
Name: window_shade_roller_opta.ino
Purpose: Open or close a roller window shade based on a programmed scenario
@author: José Bagur and Taddy Chung
@version: 1.3 (15/02/23)
*/
// Libraries used in the sketch
#include <WiFi.h>
#include <NTPClient.h>
#include <mbed_mktime.h>
// Wi-Fi network credentials
char ssid[] = "YOUR_WIFI_SSID";
char pass[] = "YOUR_WIFI_PASSWORD";
int status = WL_IDLE_STATUS;
// NTP client configuration and RTC update interval
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", -6*3600, 0);
unsigned long interval = 60*30*1000UL;
unsigned long lastTime = 0;
// Programmed scenarios for opening or closing the shade
int local_hour;
int local_minutes;
int programmed_hour_1 = 6;
int programmed_hour_2 = 18;
int programmed_minutes_1 = 0;
int programmed_minutes_2 = 0;
bool shade_state = false;
// Opta initialization
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
delay(5000);
// Attempt Wi-Fi connection
while (status != WL_CONNECTED) {
Serial.print("- Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(500);
}
// Display Wi-Fi network information
Serial.println();
Serial.println("- NETWORK INFORMATION");
Serial.print("- You're now connected to the network ");
printCurrentNet();
printWifiData();
delay(5000);
// NTP client object initialization and time update, display updated time on the Serial Monitor
timeClient.begin();
updateTime();
// Digital inputs, digital outputs, built-in LEDs initialization
pinMode(LED_D0, OUTPUT);
pinMode(LED_D1, OUTPUT);
pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
// Stop the roller window shade from moving
digitalWrite(LED_D0, LOW);
digitalWrite(LED_D1, LOW);
stop_shade();
}
void loop() {
// Check and verify if it's time to move up or down the roller window shade
local_hour = getLocalHour();
local_minutes = getLocalMinutes();
if (programmed_hour_1 == local_hour && programmed_minutes_1 == local_minutes) {
Serial.println("- Rolling down shade!");
roll_down_shade();
} else if (programmed_hour_2 == local_hour && programmed_minutes_2 == local_minutes) {
Serial.println("- Rolling up shade!");
roll_up_shade();
} else {
stop_shade();
}
// Update time client periodically
unsigned long currentTime = millis();
if (currentTime - lastTime >= interval) {
updateTime();
lastTime = currentTime;
}
}
/...
/**
Applying PLC Programming Languages in Real-World Projects
I’ve always said to monetize the skills you learn immediately. Many people learn a skill but don’t earn from it! If you acquire knowledge of a plc programming language like Ladder, try to find a real project right away where you can test your skills and get paid for it. When developing your automation project, it’s essential to define your programming standard.
Ask yourself these questions:
- In which language will you build the main programs?
- In which language will you create function blocks?
- In which language will you develop functions, etc.?
A well-constructed programming standard will bring future success.