Skip to main content

Creating a Device Project with CODESYS Control Win V3 x64 in Structured Text and Ladder Diagram

In this chapter, you will create a small CODESYS 4 project. You will start with a blank screen and at the end you have a working program that you can watch live. You will learn how to create a workspace with a device project and afterwards know where these are located in the new environment.

The project describes a fan control unit. When the temperature exceeds a maximum value, a fan is switched on and the temperature falls. When the temperature falls below a minimum value, the fan switches off and the temperature rises. The temperature is simulated by the program.

Steps in this tutorial

  1. Create a workspace with a device project

  2. Program code in Ladder Diagram (LD) and Structured Text (ST)

    . Included programs
    • FB_FanControl (LD): Control unit which switches the fan on above 30 °C and switches off below 25 °C

    • PLC_PRG (ST): Simulates the temperature using a timer and calls the control unit

  3. Create the application

  4. Download the application to the controller CODESYS Control Win V3 x64

  5. Observe the application in online mode

Requirements

  • CODESYS 4 is installed on your computer. The setup already includes the "Ladder" extension.

    For more information, see the following: Installation as a Desktop Application for Windows

  • The SoftPLC CODESYS Control Win V3 x64 is installed and running.

Starting CODESYS 4

  1. In the file explorer, open the installation directory of CODESYS 4.

  2. Run the CODESYS-4.exe program.

    The programming interface will open.

Create Workspace

Note

In this tutorial, all files belonging to the project will be created in D:\TemperatureControl\ as an example. You can choose any location.

  1. Select the Menu → Create Workspace command or click the _c4_icon_create_workspace.png button in the workspace navigator.

  2. In the Select a Path for the Workspace File dialog, specify the following:

    • Workspace name: TemperatureControl

    • Path: Click the _c4_drei_Punkte_path_selection.png button.

      • For Entry point, choose the "D" drive.

      • Use the New Folder command to add the TemperatureControl folder, then select it, and click the Select Folder button.

  3. Click OK.

    The workspace will be created and opened. The Add Project to Workspace dialog will open.

Create Device Project

  1. In the Add Project to Workspace dialog, select the Create Device Project command.

    Note

    If the Add Project to Workspace dialog is not open, then select the Menu → Create Device Project command.

  2. In the Create a new Device Project dialog, specify the following:

    • Path: D:/TemperatureControl/

    • Project name: FanControl

    • As an option, you can add more information.

  3. Click Create.

  4. In the Add PLC dialog, select the CODESYS Control Win V3 x64 controller.

  5. Click OK.

  6. In the Add program dialog, select Structured Text (ST) as the implementation language. Click Create.

    The wizard will close and the workspace navigator will display the FanControl.fbsdev project with the Devices and Application items. The PLC_PRG.prg.st program will be created below Application.

Resolving libraries

For temperature simulation, we need a timer which is included in the Standard library. A new device project references the Standard library by default. The libraries will be automatically resolved one time while the project is being created. For this project, we do not need to resolve the libraries again because we are not including any other libraries.

However, we will explicitly resolve the libraries again in order to observe the notification output.

  1. In the navigator, right-click Libraries.json and select the Resolve All Libraries command.

  2. In the lower area, open the LIBRARY RESOLUTION view to check the notifications.

Creating the fan control unit

Procedure. Create the control unit in Ladder Diagram (LD)
  1. Right-click the Application item and select the Add Item → Function Block command.

  2. Choose the name FB_FanControl and select Ladder Diagram (LD) as the implementation language.

  3. Click Create.

  4. In the declaration part of the function block, declare the interface:

    FUNCTION_BLOCK FB_FanControl
    VAR_INPUT
        // current temperature
        temperature : LREAL;
        // switch ON  at/above this value
        onLevel : LREAL;
        // switch OFF at/below this value
        offLevel : LREAL;
    END_VAR
    VAR_OUTPUT
        // indicates whether the fan should be turned on
        fanOn : BOOL;
    END_VAR
  5. Create a network named Fan Control as shown in the following figure.

    Tip

    If you are not familiar with the LD editor, then read chapter Programming in the Ladder Diagram first.

    _c4_img_network_ladder.png

    The network corresponds to the following expression:

    fanOn := (temperature >= onLevel OR fanOn) AND (temperature > offLevel);
    . Components of the network:
    • A comparison contact with the condition temperature >= onLevel

    • Parallel to that and below, a self-holding contact fanOn

    • In series with this, a comparison contact with the condition temperature > offLevel

    • A coil fanOn

    The network works in such a way that the fan will switch on at a temperature greater than onLevel (30 °C). Via the self-holding contact fanOn, the fan will remain switched on until the temperature drops below offLevel (25 °C).

Procedure. Create the temperature simulator in structured text
  1. In PLC_PRG.prg.st, the simulation of the temperature over time will be implemented, as well as the passing of the current value to the control unit.

    Tip

    The autocomplete feature will help you as you type. For example, if you type tick : TON, a list of suggestions will be offered. When you click the arrow on the right of a suggestion, help text will be displayed which describes the timer.

    In the implementation, if you type fanControl(, the autocomplete feature will show its parameters and the descriptions which you specified in the function block. In this way, you always know which value belongs where.

    Open PLC_PRG and specify the following program:

    PROGRAM PLC_PRG
    VAR
        // 500 ms heartbeat
        tick : Standard.TON;
    
        // two-point fan controller (LD)
        fanControl : FB_FanControl;
    
        // simulated temperature [°C]
        temperature : LREAL := 22.0;
    
        // fan command
        fanOn : BOOL;
    END_VAR
    
    // implementation here...
    // 1) Heartbeat: TRUE for one cycle every 500 ms
    tick(IN := NOT tick.Q, PT := T#500MS);
    
    // 2) On each tick the temperature drifts: the fan cools, otherwise it heats up
    IF tick.Q THEN
        if fanOn THEN
            temperature := temperature - 1.0;
        ELSE
            temperature := temperature + 1.0;
        END_IF
    END_IF
    
    // 3) The controller decides whether the fan runs (ON at 30 °C, OFF at 25 °C)
    fanControl(temperature:= temperature, onLevel:= 30.0, offLevel:= 25.0, fanOn=> fanOn);
    
    END_PROGRAM

    The clock tick generates a pulse every 500 ms. With each pulse, the temperature changes by one step. When the fan is running, it will drop. Otherwise, it will rise.

  2. Right-click the Application.iecapp item and select the Build Application command.

    CODESYS 4 compiles the project.

  3. In the BUILD output, check that the project has been compiled successfully.

Downloading a program to the controller

  1. Double-click the Communication.settings.json item.

    The Communication Settings will open in the editor.

  2. Click the Configure Device button.

    The Device Browser dialog will open, displaying the available controllers.

    Tip

    If you do not see a device in the list, then the SoftPLC is most likely not running. Check the tray icon of CODESYS Control Win V3 x64 and start the controller if necessary.

    Click the Refresh icon next to the search bar in order to scan the network again. The Connect Device command tests the connection to the selected controller and reports whether it can be reached. Your controller should now be listed.

  3. Select your controller and click the Connect Device button.

    Because a user management has not been set up on your device yet, the Create Initial User for Device dialog will open.

  4. In the Log In to Device dialog, provide your credentials and click Log In.

    The notification Connection successful will be displayed.

  5. In the Device Browser dialog, click Select.

  6. Right-click Application.iecapp and select the Download Application command.

  7. Right-click Application.iecapp and select the Log In command.

    Enter your credentials if necessary.

  8. In the Confirm Download dialog, click Download.

    A notification Login successful will be displayed. The display in the upper right corner changes as follows:

    _c4_gui_display_right_active_application.png
  9. Open the PLC_PRG program in the editor.

    The inline monitoring in the editor will display the current values directly after the variable.

  10. Watch the values in the WATCHLIST: in the lower view, switch to the WATCHLIST tab. The WATCHLIST is still empty.

  11. Add 2 expressions and select each of the Application entries in the Application column:

    • PLC_PRG.temperature

    • PLC_PRG.fanOn

  12. In the top-right corner in the window, click the _c4__start_application_play64px.svg button to start the active application.

    The values of monitored variables will be displayed.

What you have achieved in this tutorial

  • You have created a workspace with a device project.

  • You have programmed an ST program and included a function block from a standard library.

  • You have programmed an LD function block.

  • You have compiled the application without errors and downloaded it to the controller.

  • You have observed the variables in the WATCHLIST.

Continue with the Outsourcing Function Blocks to a Library tutorial.