Setting up CODESYS for Raspberry Pi PLC in industrial automation

Introduction to CODESYS and Raspberry Pi PLC
August 30, 2024 by
Setting up CODESYS for Raspberry Pi PLC in industrial automation
Boot & Work Corp. S.L., Frances Nosàs Moliner


CODESYS is a comprehensive automation software suite used for developing and programming control systems in line with IEC 61131-3 standards. It supports multiple programming languages and offers robust tools for visualization, motion control, and fieldbus configuration.

In this CODESYS Raspberry Pi tutorial, we will guide you through setting up CODESYS for Raspberry Pi, specifically for Industrial Shields Raspberry Pi PLCs, highlighting its application in industrial automation with Raspberry Pi.


Initial setup of CODESYS environment

We will be using CODESYS V3.5 SP19 Patch 3 + (64-bit) with the Raspberry PLC programming package, which is essential for interfacing with the hardware. 

For those interested in using Raspberry Pi as a PLC, we will also need the Raspberry controller for CODESYS. Download the following package:

CODESYS Control for Raspb​erry PI 4.11.0.0

Installing CODESYS Control for Raspberry Pi

It is a CODESYS extension (packet) that allows the software to communicate, control, and interact with a Raspberry Pi. The extension allows the raspberry to interact with the CODESYS software and run compiled code from it. 

The extension is linked to a license if the user wants to have a runtime of more than 2 hours. If there’s no active license, the software works without restrictions but stops after said time. We use version 4.11+ due to previous versions containing fatal bugs. In these versions the Raspberry driver application stopped after a few seconds of running.

It is also relevant to mention that this driver version is programmed to work with CODESYS V3.5 SP19. Lower CODESYS versions return code compilation errors. Future versions have currently unknown consequences.

Step-by-step installation guide

1. Download and install CODESYS:

  • To start with PLC programming on Raspberry Pi using CODESYS, download the desired distribution. In this case, 3.5.19 Patch 3, and follow the installation steps. 
  • Next, install the CODESYS control for Raspberry Pi in the relevant version, run the .package file, and follow the installation process.

2. Install Raspberry PI Controller:

  • Download CODESYS Control for Raspberry PI in the relevant version.
  • Run the .package file and follow the installation process.

Template projects for Raspberry Pi PLCs

Since a Raspberry Pi PLC is a highly customized product, it cannot be programmed as if it were a plain Raspberry Pi. Some characteristics are needed in a project for it to be used effectively in industrial automation. The following repository contains template projects for a raspberry PLC code:

https://github.com/Industrial-Shields/Projecte-CodeSys

All of them are written in Structured Text.


CodeSys interface for Raspberry Pi PLCs

In a project template we can see the text editing windows, Devices and POUs, and Messages. 

  • Devices contains program code, declared Raspberry device, task configuration, external communication interfaces, associated devices, etc.
  • POUs. Program Organization Unit. Contains source code that can be called by the main code.
  • Messages is the debugging and execution log. It will show us syntax errors and inconsistent statements. Since compiling an erroneous code is not possible, it shows us where the errors are.

In "Tools → Update Raspberry PI" we find the Raspberry PI controller window, which is grouped with Devices and POUs.

Windows of Digital.project CodeSys


Connecting the Raspberry Pi PLC to the PC

We connect a Raspberry Pi PLC to our computer. The IPs on the PLC are:

  • 10.10.10.20 (Side Ethernet port, recommended)
  • 10.10.11.20 (Front Ethernet port)

We need to configure the Ethernet network to 10.10.10.0/24 to be able to talk with the PLC.

The following tools are recommended to test the connection:

  • PING (ex: ping 10.10.10.10)
  • NMAP (ex: nmap 10.10.10.10/24)


Installing the Driver to the Raspberry

On CODESYS Tools, open the tab "Update Raspberry PI". Then, enter the ip of the raspberry, select the target PLC IP address, choose the Runtime Package version and press "Install". Follow the installation process.

In this same tab we can find relevant information regarding the runtime and the Raspberry. 

Windows of update Raspberry Pi

It will ask for also installing the "CODESYS Edge Gateway for Linux". It's not required, you can learn more about it here to check if it aligns to your needs.


Logging into the Raspberry Pi

This is what will allow us to ad our code to the PLC. This requires a series of steps. First, we need to open the "Device" window and scan the network. We select the Raspberry device.

Devic windows for logging into the rapsberry pi

We can now log in the PLC using the "Login" button. Furthermore, we will have to add the username to be able to connect to the Raspberry. It must be the same username and password of a User the Raspberry has.

Example to log in the PLC using Login buttonAdd the username to be able to connect to the Raspberry

We are now in the PLC, and we can start / stop the program at will.


Common troubleshooting in CODESYS

Rarely, CODESYS might not start. The probability of the cause being this issue is not low. A solution to this is to delete the "C:\ProgramData\CODESYS\CODESYSProjectDefaults.opt" file from the PC every time we encounter this problem. It is an auto-generated file, so we don't have to worry about losing data by deleting it. Our project preferences will simply be erased.


U​sing GPIOs on Raspberry Pi PLC

For the GPIOs of an Industrial Shields PLC to be active, the following is required:

  • I2c interface. It shows in CODESYS as a Master i2c device.
  • Dedicated GPIOs Function Blocks. They can be found on the project presets in the Github repository.

The PLC expansors require the bus I2C to be active.The function blocks must be placed in the POUs window, and will be accessible all throughout the project. All methods use the I2C_master device. It is necessary to add the "I2C_master" device to the I2C interface in "Devices".


Function Block: GPIOs

Use for Analog/Digital PLCs. It contains the different methods:

  • FB_init(WSTRING string)
    Initialization field, is responsible for configuring the addresses of the internal I2C chips of the PLC with the corresponding registers. A WSTRING is entered with the name of the GPIO in “Q0.0” format. The string must have 4 or 5 characters. (For example: “A2.7” or “I2.10”).
  • read_analog()
    Analog read. Requires no inputs, returns a UINT.
  • read_digital()
    Digital read. Requires no input, returns a BOOL. 
  • write_analog(UINT value)
    Analog writing. Requires an input UINT. Returns a BOOL. 
  • write_digital(BOOL value)
    Digital writing. Requires an input BOOL. Returns a BOOL.


Function Block: GPIOsR

Use for Relay PLCs. It contains the different methods:

  • FB_init(WSTRING string)
    Initialization, is responsible for configuring the addresses of the internal I2C chips of the PLC with the corresponding registers. A WSTRING is entered with the name of the GPIO in “Q0.0” format. The string must have 4 or 5 characters. (For example: “A2.7” or “I2.10”).
  • read_analog()
    Analog read. Requires no inputs, returns a UINT.
  • read_digital()
    Digital read. Requires no inputs, returns a BOOL.
  • read_relay()
    Check the status value of a relay. Requires no inputs, returns a BOOL. This is not a reading on the voltage between terminals of the relay, just a check for the relay status.
  • write_analog(UINT value)
    Analog writing. Requires an input UINT. Returns a BOOL.
  • write_digital(BOOL value)
    Digital writing. Requires an input BOOL. Returns a BOOL.
  • write_relay(BOOL value)
    Relay writing. Requires an input BOOL. Returns a BOOL.


Declaration of function block GPIOs:

The declaration of a GPIO is made by instantiating a function block, providing the type, zone/layer and pin as described before.

In the PLC application code, read and write commands can be executed using the corresponding methods. Here's an example for a digital PLC, thus using the GPIOs function blocks:

– Application Header –
PROGRAM PLC_PRG
VAR
Var1 : GPIOs := GPIOs(“I0.7”);
Var2 : GPIOs := GPIOs(“Q0.0”);
i : UINT;
I2: BOOL := TRUE;
END_VAR

– Application Body –
i := Var1.read();
Var2.write(i2);

Some pins cannot be declared. This is the case of direct GPIOs. Those are IX.5 and IX.6 in Digital PLCs, and IX.0 and IX.1 in Relay PLCs. 
In said occurences, the user must declare them as explained in the next section.


Declaration of direct GPIOs

The variable must first be declared in the application header.

- Application Header -
Direct_pin : BOOL;
i : BOOL;

First, update the device "GPIOs A/B" to "GPIOs B +/Pi2".

Access GPIOs_A_B. In GPIOs Parameters, edit the desired GPIO by setting its "Value" to "Input" or "Output".

In GPIOs I/O Mapping, edit the GPIO in the dropdown (inputs or outputs) and assign the project variable. We write in "Variable", press the [···].
An interface will display all variables existing on the project. Those are categorized in a file-like structure. Follow Categories → Application →  <PLC_PRG> → <Direct_pin>. Press the variable and it will be assigned.

We can now check or write values to this variable, which will behave accordingly to the pin mode. Here's an example for an input check using the variable we declared before:

- Application Body -
i := Direct_pin;

The GPIOs and their associations are as follows.

Direct pin equivalence table

Analog PLC Pin

Relay PLC pins

Channel / bit

I0.5

I0.0

13

I0.6

I0.1

12

I1.5

I1.0

27

I1.6

I1.1

5

I2.5

I2.0

26

I2.6

I2.1

4



Conclusion: Optimizing CODESYS for Raspberry Pi PLC in industrial automation

In summary, setting up CODESYS for Raspberry Pi provides a versatile and powerful solution for industrial automation. By leveraging CODESYS software and its compatibility with Raspberry Pi PLCs, users can create customized control systems that adhere to IEC 61131-3 standards. This guide has walked you through the essential steps, including installing the necessary software, configuring the Raspberry Pi PLC, and utilizing template projects to get started. With these tools and knowledge, you can effectively integrate Raspberry Pi PLCs into your industrial automation projects, optimizing performance and efficiency.


​Search in our Blog

Setting up CODESYS for Raspberry Pi PLC in industrial automation
Boot & Work Corp. S.L., Frances Nosàs Moliner August 30, 2024
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>