Skip to main content

Creating a Device Project with Raspberry Pi

The following tutorial describes the procedure how to create a minimal project and download it to a Raspberry Pi.

The project consists of a push button and an LED. When the button is pressed, an LED lights up and switches back off after a time delay of 3 seconds. The input and output are connected via GPIO to the Raspberry Pi.

Note for Raspberry Pi 5

Due to a bug in the GPIO library, in Raspberry Pi Version 5, the GPIO inputs/outputs are no longer updated after the application is downloaded again. In this case, restart the runtime system or disconnect the Raspberry Pi from the power supply before downloading program changes to the controller.

Requirements

  1. You have a current Linux operating system (for example, Debian) installed on your Raspberry Pi.

    For more information, see the following: Preparation

  2. You have the CODESYS Control for Raspberry Pi SL runtime system installed on your Raspberry Pi.

    For more information, see the following: Establishing a Connection and Installing the Runtime on the Controller. The measures described there are done via the CODESYS Control SL Deploy Tool of CODESYS 3.

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 of CODESYS 4 will open.

Create Workspace

  1. Select the Menu → Create Workspace command or click the _c4_icon_create_workspace.png icon.

  2. In the Create Workspace dialog, choose the name and click the _c4_drei_Punkte_path_selection.png button to select the location.

  3. Click OK.

    The workspace will be created and opened.

    The Add Project to Workspace dialog will open. The name of the open workspace will be displayed in the menu bar.

Create Device Project

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

  2. In the next dialog, specify the data for your new project and then click OK.

    • Click the _c4_drei_Punkte_path_selection.png button to select the location.

    • Specify the project name, for example MyFirstRaspiProject.

    • Specify values for all other parameters.

      The version number must be a 3 or 4-part semver, for example 2.1.0 or 1.0.0.0.

  3. In the Add PLC dialog, select your CODESYS Control for Raspberry Pi controller.

  4. Specify a name for the controller, for example MyRaspi, and click OK.

  5. In the Add program dialog, select Structured Text (ST) as the implementation language and click Create.

    The wizard will close and the project items will be displayed in the workspace navigator.

Creating a program

  1. Double-click the PLC_PRG object.

    The PLC_PRG program will be opened in the editor.

  2. Enter the following program into the editor:

    PROGRAM PLC_PRG
    VAR
        xButton : BOOL;
        xLed : BOOL;
        hold : Standard.TOF;
    END_VAR
    
    // implementation here...
    hold(IN:=xButton, PT:=t#3s, Q=>xLed);
    END_PROGRAM

Adding the device GPIO and mapping the variables

  1. In the workspace navigator, right-click the inserted controller and run the Add Device command.

  2. Select the GPIOs A/B device. Choose a name for the device, for example MyGPIO, and click OK.

  3. Open the GPIO device in the editor and switch to the Configuration tab.

  4. With GPIO, you define whether a pin should be an input or an output. The parameterValue parameter is used for this definition. The value 2#000 defines an input and the value 2#001 defines an output.

    Change the following entries:

    {   "parameterValue": {
          "value": "2#000"
        },
        "name": "GPIO17",
        "id": 17,
        "type": "local:GPIOType"
    }
    ...
    {   "parameterValue": {
          "value": "2#001"
        },
        "name": "GPIO27",
        "id": 27,
        "type": "local:GPIOType"
    }
  5. Open the I/O Mapping tab. Create the variable xButton at input 17 and the variable xLed at output 27:

    _c4_raspbi_gpio.png

Resolving libraries and creating an application

  1. Right-click the Libraries.json item and run the Resolve All Libraries command.

  2. Check if errors are displayed in the LIBRARY RESOLUTION and PROBLEMS:LIBRARIES views.

    Note: The PROBLEMS:LIBRARIES view will be displayed only if errors have actually occurred.

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

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

Setting up communication to the controller

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

    The Communication Settings wizard will open in the editor.

  2. Click the Configure Device button.

    The Device Browser dialog will open. Your controller will be displayed in the left window.

  3. Click your controller to select it 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. Choose a user name and password. Confirm the password and click Apply.

    Note: The chosen password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one special character, and one digit.

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

    The notification Connection successful will be displayed.

  6. In the Device Browser dialog, click Select.

For more information, see the following: Communication.settings.json

Downloading and logging in to the application on the controller

  1. Right-click the Application.iecapp application and select the Download Application command.

    A notification will be displayed to indicate that existing applications on the controller will be overwritten. The application will be downloaded to the controller. The notification Download succeeded will be displayed.

  2. Click the _c4_icon_login.png icon (top right) or press the Alt+F8 shortcut to log in to the controller.

    The notification Login succeeded will be displayed.

  3. Open the PLC_PRG program in the editor.

  4. Click the _c4__start_application_play64px.svg icon (top right) to run the program.

    The inline monitoring in the editor will show the changing values.

  5. Click the _c4_stop_application_stop64px.svg icon (top right) to stop the program.

  6. Click the _c4_icon_logout.png icon (top right) or press the Ctrl+F8 shortcut to log out of the controller.

    The notification Logout succeeded will be displayed.