Attention

These docs are a bit ambitious. The steps described are accurate but IronPLC cannot yet run programs.

Hello, World!#

Now that you’ve installed IronPLC, it’s time to write a first program.

If you’ve ever learned another programming language, you likely started by writing a “Hello, World” program to display that text. Don’t worry if you haven’t learned another programming language or haven’t written a “Hello, World” program.

IEC 61131-3 is designed for real-time automation controllers that often do not have a display. In other words, there is often no place to show “Hello, World”. Other solutions to get feedback, such as creating a file are also unusual.

In short, a “Hello, World” program in IEC 61131-3 is different.

The Sense-Control-Actuate Cycle#

Controllers normally operate as part of a sense-control-actuate cycle. We’ll start with a simple example to illustrate the idea: a door bell system. Our door bell system contains a button (the sensor) and a buzzer (the actuator).

../_images/button-buzzer.svg

Pressing the button triggers the buzzer.#

We desire that the buzzer makes noise when the button is pressed. To do that, we use a controller to check the button state and if pressed then enable the buzzer.

Note

It is possible to design a simpler door bell system. This example designed to illustrate how to use IEC 61131-3.

Create a Program with Structured Text#

Run Visual Studio Code, then in Visual Studio Code:

  1. In the main menu, select File ‣ New File….

  2. In the New File… dialog, select the Structured Text File option.

  3. Enter the code in Hello World into the Editor.

    Hello World#
    PROGRAM main
       VAR
          Button AT %IX1: BOOL;
          Buzzer AT %QX1: BOOL;
       END_VAR
    
       Buzzer := NOT Button;
    
    END_PROGRAM
    
    CONFIGURATION config
       RESOURCE res ON PLC
          TASK plc_task(INTERVAL := T#100ms, PRIORITY := 1);
          PROGRAM plc_task_instance WITH plc_task : main;
       END_RESOURCE
    END_CONFIGURATION
    
  4. Save the file with the name main.st.

Anatomy of the Hello, World Program#

Let’s review this program. IEC 61131-3 applications are structured from blocks called Program Organization Units (POUs). The PROGRAM is a top level block and similar to the main function in other languages. The section indicated by

PROGRAM main

END_PROGRAM

defines a PROGRAM having the name main.

Unlike the main function in other languages, a program does not run by default. We need to tell the PLC runtime how we want to run the program. The piece indicated by

CONFIGURATION config

END_CONFIGURATION

defines how we want the program to run. The configuration declares we want to execute the main program once every 100 ms and this task is the highest priority task. This task executed on the hardware element named res.