Skip to content

First steps with CODESYS


This chapter uses the MFN 100 as an example.

This chapter will give an introduction on how to start working with the integrated soft PLC in the MFN 100. First, some configuration and installation of files and libraries are required.


  • Download the CODESYS Development System V3 from for this chapter.
    We recommend version 3.5 SP14 (32 bit) or newer.
  • Connect the workstation to the console port P1 of the MFN 100.

Installing the device descriptions

After downloading and installing the CODESYS Development System on the workstation, install the device description of the MFN 100 in the CODESYS Development System. Make sure to download the latest device description file. The device description has the following file name:

  • Nerve_V3.5.XX.X.devdesc.xml


XX.X stands for the current version of the CODESYS Development System

The device description of the MFN 100 is available at the Nerve Software Center. Remember where the device description is saved for the following steps.

  1. Start the CODESYS Development System.
  2. Go to Tools > Device Repository.

    Device repository

  3. Select Install.

    Install device description

  4. Go to the directory of the previously downloaded device description.

  5. Select the device description of the MFN 100. The device description will look like this: Nerve_V3.5.XX.X.devdesc.xml
  6. Select Open.

When the installation was successful, the MFN 100 will appear in the list of device descriptions in the middle of the window.

Device description installed

After installing the device description, work with the CODESYS Development System can be started. However, libraries and device descriptions of generic devices might be missing so that the CODESYS Development System can work properly. The following chapters cover the download process.


The device description might need to be updated if this is not the first time working with MFN 100 and the CODESYS Development System:

  1. Follow the steps above to install the newest device description.
  2. Open a CODESYS project.
  3. Right-click Device (Nerve) on the left side.
  4. Select Update Device....
  5. Select the current device description in the new window.
  6. Select Update Device in the lower-right.

Downloading missing libraries

The error message for missing libraries might appear when opening or creating a CODESYS project. The CODESYS Development System identifies the missing libraries automatically but the following process might need to be repeated a few times.

Missing libraries error message

  1. Open or create a CODESYS project.
  2. If the error message about missing libraries appears, select OK.
  3. Double-click Library Manager in the tree view on the left.
  4. Select Download missing libraries.

    Library Manager

  5. Select Download in the new window.

    Downloading libraries

  6. Select Close when the download is finished.

  7. Repeat steps 3 to 5 until no more libraries appear in the download window.

Downloading missing device descriptions

Apart from the device description for the MFN 100 that have been installed manually before, device descriptions of generic devices may be missing for the CODESYS Development System to function as intended. The CODESYS Development System will identify the missing device descriptions automatically but this time it will not generate an error message unless a CODESYS application is being loaded into the MFN 100.

  1. Select Tools > Device Repository.
  2. Select Download missing descriptions.

    Missing device descriptions


    The button for downloading missing descriptions will not appear if no device descriptions of generic devices are missing. Close the window and continue if that is the case.

  3. Select Download in the new window.

  4. Select Close when the download is finished.

Creating a new CODESYS project

This example shows how to create a new project in the CODESYS Development System. The easiest way to get started is to create a Standard project.

  1. Start CODESYS
  2. Go to File > New Project.
  3. Select Standard project on the right side among the templates.
  4. Enter a name for the project.
  5. Choose a Location where the project will be saved.
  6. Select OK to save the project.

    New Project

  7. Select Nerve (TTTech) as the device.

    MFN 100

  8. Select OK.

The result is an empty project that is open in the main view of CODESYS.

Empty Project

I/O settings for fieldbus configuration

To connect to a fieldbus, an I/O interface needs to be selected. The I/O interface in Nerve is called io0. As a demonstration, the instructions below add an Ethernet Adapter (used for Profinet connections for example) to the device in the CODESYS project in order to select the io0 interface.

  1. Right-click Device (Nerve) on the left side.
  2. Select Add Device....

    Select Add Device

  3. Expand Ethernet Adapter in the new window.

    Expand Ethernet Adapter

  4. Select an Ethernet adapter from the list.

  5. Select Add Device.

    Select Add Device

  6. Select Close to close the window.

With the Ethernet Adapter added, the io0 interface can be added as an I/O interface. Note that a connection to a Nerve Device needs to be established first. Refer to Connecting to the MFN 100 before continuing.

  1. Double-click the previously added Ethernet adapter on the left. A new tab opens on the right side.
  2. Select the ellipsis symbol next to the Network interface field.

    Select ellipsis symbol

  3. Select io0 from the list.

    Select io0

  4. Select OK.

Working with the default applications

To work with existing applications first, modify the default applications app1.project and app2.project. They have been sent as part of the delivery.

  1. Start CODESYS.
  2. Go to File > Open Project.
  3. Select the location where the default applications are saved.
  4. Select the application to work with.

    Default Applications

  5. Select Open.

If the default applications are opened for the first time, some libraries and device descriptions will be missing. Follow the instructions above to see how to download the missing files. 

Connecting to the MFN 100

Before downloading CODESYS applications to the MFN 100, make sure that the device description of the MFN 100 is installed in the CODESYS Development System.

  1. Open or create a CODESYS project.
  2. Double-click Device (Nerve) in the tree view on the left.
  3. Go to Communication Settings > Scan network....

    Scan network

  4. Select the MFN 100 (here nerve-rtvm [XXXX.XXXX]) in this window.



    When more than one network is active on the workstation, it sometimes happens that the MFN 100 cannot be found. Continue reading if the MFN 100 does not appear in this window.

  5. Select OK.

Typically the MFN 100 will be found automatically. If the MFN 100 cannot be found, enter the IP address and port of the CODESYS runtime manually.

  1. Double-click Device (Nerve) in the tree view on the left.
  2. Go to Communication Settings in the middle of the window.
  3. Enter in the text box under the device on the right.

    Connecting manually

  4. Press Enter.

The CODESYS Development System is now connected to the MFN 100 and applications can be downloaded into the CODESYS runtime. For connecting to the Nerve Device remotely, refer to Connecting to a device remotely in CODESYS.

Downloading an application to the MFN 100

CODESYS applications can be loaded directly into the MFN 100. However, before downloading an application into the MFN 100 it needs to be free of errors.

The process of downloading an application is slightly different if an entirely new application is downloaded into the MFN 100 or if an application is being updated that has already been downloaded into the MFN 100. If updating an application that has been downloaded to the MFN 100 before, continue with Downloading an Updated Application to the MFN 100 further down below.

Downloading a new application to the MFN 100

Once a project has been created and programming is finished, the CODESYS application can be downloaded to the MFN 100 directly.

  1. Open the CODESYS project to load into the MFN 100.
  2. Select the Login symbol in the CODESYS menu bar.

    Login symbol

  3. Select Yes in the pop-up window.

    Confirm application download

  4. The application is stopped now. Select the Play symbol in the CODESYS menu bar.

    Play symbol

The application is now loaded to the MFN 100.

Downloading an updated application to the MFN 100

If updating an application after loading it into the MFN 100, it needs to be downloaded into the MFN 100 again. The download process is slightly different from downloading a new application into the MFN 100.

  1. Stop the CODESYS application that has been loaded into the MFN 100 through the Local UI.
  2. Select the Logout button in the CODESYS toolbar.

    Logout button

  3. Expand Device (Nerve) > PLC Logic > Application.

  4. Double-click PLC_Program (PRG).
  5. Perform changes.
  6. Select the Login symbol in the CODESYS menu bar.

    Login symbol

  7. In the pop-up window, select one of the options.

    Pop-up window options

    Item Description
    Login with online change. The updated application will be loaded into the MFN 100. Variable values will not be reset. If the application was running before, it will be running after the download.
    Login with download. The updated application will be loaded into the MFN 100. Variable values will be reset. The application is stopped.
    Login without any change. The updated application will not be loaded into the MFN 100 but the code will keep the changes.
  8. Select OK.

The application is now loaded to the MFN 100.


For more help with programming PLC applications in the CODESYS Development System go to

Allocating variables to inputs or outputs

After connecting new sensors and actuators, assign variables to the I/O channel in CODESYS.

  1. Open a CODESYS project.
  2. Expand Device (Nerve) > PLC Logic > Ethernet_1 > PN_Controller > siemenset200 (IM 155-6 PN ST V4.1) in the tree structure on the left.
  3. Double-click DI_8x24VDC_ST_V0_0_QI (...) for digital inputs.
    Double-click DQ_8x24VDC_0_5A_ST_V0_0_QI (...) for digital outputs.
  4. Select PNIO Module I/O Mapping.

    Variable Allocation

  5. Fully expand the tree view.

  6. Double-click the variable slot to assign.


    The inputs in this view do not match the physical inputs of the I/O module on the kit. The inputs here go from 0 to 7. The physical inputs go from 1 to 8. Therefore input 0 in this view represents the physical input 1 on the I/O module. This also applies to outputs.

  7. Select the three dots next to the variable slot.

    Ellipsis menu

  8. Expand Application > GVL in the new window.

    Global Variables

  9. Select the variable to assign.


    Make sure to select a variable of the same type as the input, i.e., a BOOL variable for a BOOL input or output.

  10. Select OK.

Use the assigned variables to read data from connected sensors or to control actuator functionality.


For more help with programming PLC applications in the CODESYS Development System go to

Enabling retain variables

NerveCodesysRetainVar is a library for storage and restoration of retain variables with prevention of data loss in case of system crash (e.g. power outage). To prevent data loss, the retain variables are stored in two file copies under /opt/data/var/lib/nerve-codesys/PlcLogic/.

The retain variables library (NerveCodesysRetainVar.compiled-library) and an example project showcasing typical use (demo.project) are available at the Nerve Software Center. Follow the instructions below to install and import the retain variables library into a project.

Installing and importing the NerveCodesysRetainVar library

Download the retain variables library (NerveCodesysRetainVar.compiled-library) from the Nerve Software Center first before following the instructions below.

  1. Open or create a CODESYS project.
  2. Select Tools > Library Repository in the toolbar.
  3. Select Install... in the new window.
  4. Navigate to the folder containing the retain variables library and select the NerveCodesysRetainVar.compiled-library file.
  5. Select OK.

The library will appear in the Application element after it has been installed. Close the library repository window by selecting Close in the lower right. Next the library needs to import into the project.

  1. Double-click Library Manager in the tree view on the left.
  2. Select Add library in the Library Manager tab that opened on the right.
  3. Enter NERVE Retain in the search field to search for the library.
  4. Select the NERVE Retain Variables Library.
  5. Select OK.

The Nerve Retain Variables Library now appears in the Library Manager tab.

Example project and use

The example project (demo.project) is meant to show how the library functions can be used. It includes two variants of a simple program counting up multiple counters stored as permanent variables:

  • PLC_PRG1 stores and restores the retain variables just once based on a trigger activated by boolean flags (store and restore).
  • PLC_PRG2 shows a typical use case, where the retain variables are restored once during the initialization phase and then periodically stored every cycle after all the counters are updated.

The NerveCodesysRetainVar library provides two functions:

  • StoreRetains to store retain variables
  • RestoreRetains to restore retain variables

The namespace of the library is NerveCodesysRetainVar.

Functions have a return value of type RTS_IEC_RESULT defined in the SysTypes Interfaces library. It is required to either include this library or define the return variables as type NerveCodesysRetainVar.RTS_IEC_RESULT. The possible error codes are defined by CmpErrors2 Interfaces.

Note that RestoreRetains may fail with error code 0x32 when the application runs for the first time, before any retain variables were stored. This is because the files with stored retain variables do not exist yet.

During the startup of the device, the CODESYS application uses the function RestoreRetains. This loads the last stored state of retain variables.

During execution, the CODESYS application periodically saves the current state of retain variables with the function StoreRetains. This could be done in a periodically executed task or at certain points during calculations, when it is meaningful to store the current state.
In case of a sudden crash of the CODESYS application (e.g. power failure), the last saved state of the retain variables will be restored.


The type of memory must be taken into consideration for periodical storing of retain variables. Writing too often for longer periods of time may result in damage to disks.