Attention

IronPLC supports IEC 61131-3 Structured Text excluding I/O mapping.

Your First Program

Now it’s time to write and run your first IEC 61131-3 program. By the end of this chapter, you will have a working doorbell program running in the IronPLC virtual machine.

Create a Project Directory

Open a terminal and create a new folder for your project:

Create Project Directory
mkdir doorbell
cd doorbell

Then open the folder in your development environment:

code .

Tip

If you are using Cursor, use cursor . instead of code ..

Write the Program

In your development environment:

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

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

  3. Enter the following code into the Editor:

    main.st — Doorbell Program
    PROGRAM main
       VAR
          Button : BOOL;
          Buzzer : BOOL;
       END_VAR
    
       Buzzer := NOT Button;
    
    END_PROGRAM
    
  4. Save the file with the name main.st.

If the IronPLC extension is installed, you should see no errors highlighted in the editor.

VS Code editor showing the doorbell program in main.st with syntax highlighting and no errors

The doorbell program with syntax highlighting and no errors.

What This Program Does

Let’s break it down:

  • PROGRAM mainEND_PROGRAM defines a program named main. A program is the basic unit of control logic in IEC 61131-3, similar to a main function in other languages.

  • VAREND_VAR declares two variables of type BOOL. Button represents the sensor input and Buzzer represents the actuator output.

  • Buzzer := NOT Button; is an assignment statement. The := operator assigns the value on the right to the variable on the left. When Button is FALSE (not pressed), Buzzer is TRUE (sounding).

Run the Program

Look for the Run Program link that appears above the PROGRAM main line in the editor. This is a code lens provided by the IronPLC extension.

  1. Click Run Program.

  2. The IronPLC Run output panel opens automatically. It shows the current scan cycle number and the value of every variable, updating as the program runs. You should see output like:

    Scan cycle: 1
    ---
      Button : BOOL = FALSE
      Buzzer : BOOL = TRUE
    
  3. Click Stop above the PROGRAM line to end execution.

VS Code editor showing the Run Program code lens and the IronPLC Run output panel with scan cycle results

The IronPLC Run panel showing scan cycle output with variable values.

Button starts as FALSE (the default for BOOL), so NOT Button evaluates to TRUE, and the buzzer sounds. This is exactly the sense-control-actuate cycle in action — even though there is no physical hardware connected yet.

Tip

You can also check, compile, and run from the command line. See Check, Compile, and Run from the Command Line.

Next Steps

You now have a working program. In the next chapter, you will add a timer to make the buzzer pulse automatically and learn how the configuration block controls the scheduling.

Continue to Configuring Your Application.