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
You have a current Linux operating system (for example, Debian) installed on your Raspberry Pi.
For more information, see the following: Preparation
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
In the file explorer, open the installation directory of CODESYS 4.
Run the
CODESYS-4.exeprogram.The programming interface of CODESYS 4 will open.
Create Workspace
Select the Menu → Create Workspace command or click the
icon.
In the Create Workspace dialog, choose the name and click the
button to select the location.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
In the Add Project to Workspace dialog, select the Create Device Project entry.
In the next dialog, specify the data for your new project and then click OK.
Click the
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.0or1.0.0.0.
In the Add PLC dialog, select your
CODESYS Control for Raspberry Picontroller.Specify a name for the controller, for example
MyRaspi, and click OK.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
Double-click the PLC_PRG object.
The PLC_PRG program will be opened in the editor.
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
In the workspace navigator, right-click the inserted controller and run the Add Device command.
Select the GPIOs A/B device. Choose a name for the device, for example
MyGPIO, and click OK.Open the GPIO device in the editor and switch to the Configuration tab.
With GPIO, you define whether a pin should be an input or an output. The
parameterValueparameter is used for this definition. The value2#000defines an input and the value2#001defines 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" }Open the I/O Mapping tab. Create the variable
xButtonat input 17 and the variablexLedat output 27:
Resolving libraries and creating an application
Right-click the
Libraries.jsonitem and run the Resolve All Libraries command.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.
Right-click the
Application.iecappitem and select the Build Application command.In the BUILD output, check that the project has been compiled successfully.
Setting up communication to the controller
Double-click the
Communication.settings.jsonitem.The Communication Settings wizard will open in the editor.
Click the Configure Device button.
The Device Browser dialog will open. Your controller will be displayed in the left window.
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.
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.
In the Log In to Device dialog, provide your new credentials and click Log In.
The notification Connection successful will be displayed.
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
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.
Click the
icon (top right) or press the Alt+F8 shortcut to log in to the controller.The notification Login succeeded will be displayed.
Open the PLC_PRG program in the editor.
Click the
icon (top right) to run the program.
The inline monitoring in the editor will show the changing values.
Click the
icon (top right) to stop the program.
Click the
icon (top right) or press the Ctrl+F8 shortcut to log out of the controller.The notification
Logout succeededwill be displayed.