Using Codesys soft PLC#

We run the Codesys (“COntroller DEvelopment SYStem”) runtime on a Raspberry Pi, and the setup is done using a Windows-based software (Codesys Development System). Your Windows laptop is used to make changes to the PLC (IO-controller) settings, but it does not act as an IO-controller.

Downloading and installing Codesys Development System on a Windows PC#

The program can be downloaded from A trial version is available. Registration is required.

  1. Download “CODESYS Development System V3”, in the latest available version. The file is named CODESYS 64 <VERSION>.exe.

  2. Install it on a Windows machine by double clicking the icon.

  3. From within Codesys, install the “Codesys control for Raspberry Pi” by using the menu Tools ‣ Codesys installer.

  4. In the AddOns section use Browse and search for “raspberry”.

  5. Select the relevant row in the results, and click Install. When completed, there should be an entry Update Raspberry Pi available in the Tools menu.

  6. Restart the program after the installation.

Scanning the network to find your PLC Raspberry Pi, and installing the Codesys runtime onto it#

  1. Make sure your Windows machine and the Raspberry Pi are connected to the same local network.

  2. In Codesys on Windows, use the menu Tools ‣ “Update Raspberry Pi”.

  3. Click Scan to find the IP address. In this tutorial the IP address of the PLC Raspberry Pi is

    It is possible to install the runtime on the PLC Raspberry Pi as long as you know its IP address, even if it doesn’t show up during the scan.

  4. Click Install for the Codesys Runtime package. Use “Standard” runtime in the pop-up window.

    The runtime will be installed in the /opt/codesys/ directory on the Raspberry Pi.

Creating a project in Codesys#

  1. On the Windows machine, first create a suitable project area on your hard drive. For example Documents/Codesys/Democontroller.

  2. From the Codesys menu, create a new project. Use the recently created directory, and Standard Project.

  3. Name it “Democontroller”.

  4. Select device Codesys Control for Raspberry Pi SL, and select to program in “Structured Text (ST)” It is important that you select the same version of the runtime (single core = “SL” or multicore = “SL MC”) both in the runtime and in the project, otherwise the controller will not be found when you try to use it.

  5. Verify that the Device in the left hand menu shows as “Codesys Control for Raspberry Pi SL”.

  6. Double-click the Device. Click Scan network tab, and select the Raspberry Pi. The marker on the image should turn green. Use tab Device and Send Echo Service to verify the communication.

  7. In the Codesys menu Tools, select Device Repository.

  8. Click Install and select the GSDML file from your hard drive. For the sample application the GSDML file is available in the samples/pn_dev folder in the repository that you have cloned, or from rtlabs-com/p-net

  9. On the Device (CODESYS Control for Raspberry Pi SL) in the left hand panel, right-click and select Add Device. Use Ethernet adapter, Ethernet.

  10. On the Ethernet, right-click and select Add Device. Use Profinet IO master, PN-Controller.

  11. On the PN_Controller, right-click and select Add Device. Use P-Net Sample App.

  12. On the P_Net_Sample_App, right-click and select Add Device. Use DIO 8xLogicLevel.

  13. Double-click the Ethernet node in the left menu. Select interface “eth0”. The IP address will be updated accordingly.

  14. Double-click the PN_controller node in the left menu. Adjust the IP range using First IP and Last IP to both have the existing IP-address of your IO-device (for example a Linux laptop or embedded Linux board running the sample_app). For this tutorial we use the First IP and also the Last IP

  15. Double-click the P_Net_Sample_App node in the left menu. Set the IP-address to the existing address of your IO-device. In this tutorial we use

Structured Text programming language for PLCs#

Structured Text (ST) is a text based programming language for PLCs. Read about it on

A tutorial is found here:

Creating a controller application#

  1. Click on PLC logic ‣ Application ‣ PLC_PRG` in the left hand panel and enter the program.

    Variables section:

        in_pin_button_LED: BOOL;
        out_pin_LED: BOOL;
        in_pin_button_LED_previous: BOOL;
        flashing: BOOL := TRUE;
        oscillator_state: BOOL := FALSE;
        oscillator_cycles: UINT := 0;

    Program section:

    oscillator_cycles := oscillator_cycles + 1;
    IF oscillator_cycles > 200 THEN
        oscillator_cycles := 0;
        oscillator_state := NOT oscillator_state;
    IF in_pin_button_LED = TRUE THEN
        IF in_pin_button_LED_previous = FALSE THEN
            flashing := NOT flashing;
        out_pin_LED := TRUE;
    ELSIF flashing = TRUE THEN
        out_pin_LED := oscillator_state;
        out_pin_LED := FALSE;
    in_pin_button_LED_previous := in_pin_button_LED;
  2. On the DIO_8xLogicLevel node in the left-side menu, right-click and select Edit IO mapping.

  3. Open the Input 8 bits row by clicking the small + sign.

  4. Double-click the icon on the row that you would like the edit.

  5. Map “Input Bit 7” to “in_pin_button_LED” (found via Application/PLC_PRG), and “Output Bit 7” to “out_pin_LED”.

  6. In the Application ‣ MainTask select Cyclic with 4 ms.

  7. In the Application ‣ Profinet_CommunicationTask select Cyclic with 10 ms. Use priority 14.

Transferring the controller application to a (controller) Raspberry Pi#

  1. In the top menu, use Build ‣ Generate Code.

  2. Transfer the application to the Raspberry Pi by using the top menu Online ‣ Login. Press Yes in the pop-up window.

  3. In the top menu, use Debug ‣ Start`

    You can follow the controller log by using the top menu Tools ‣ “Update Raspberry Pi”. Click the System info button, and look in the Runtime Info text box. It will show an error message if it can’t find the IO-device on the network.

    Use Wireshark to verify that the controller sends LLDP packets every 5 seconds. Every 15 seconds it will send an ARP packet to ask for the (first?) IO-device IP address, and a PN-DCP packet to ask for the IO-device with the name “rt-labs-dev”.

  4. Once the Codesys softplc running on the Raspberry Pi has been configured, you can turn off the personal computer (running the Codesys desktop application) used to configure it.

    Remember that you need to power cycle the Raspberry Pi running the softplc every two hours, if using the trial version.

Codesys Troubleshooting —————- ——- If you receive errors claiming there are missing libraries, click on PLC logic ‣ Application ‣ Library Manage in the left hand panel. Codesys should automatically detect if there are any missing libraries. Click on Download missing libraries under the Library manager tab to download any missing libraries.