Programming in Ladder using CODESYS and raspberry PLC

First steps with the Ladder language in CODESYS for RPI PLC
November 11, 2024 by
Programming in Ladder using CODESYS and raspberry PLC
Boot & Work Corp. S.L, Francesc Nosàs Moliner

Ladder Logic is a programming paradigm originally focused on relay racks and industrial machine control design. Its design, akin to the hardware connections on a rack, simplifies big portions of sequential instructions into digestible representations of the code.

Over time, it has evolved until it's now considered a full-on programming language, mainly used for programming PLCs.

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

Setup

Check out the setup article for setting up CODESYS for raspberry pi PLC in the following link:

Setting up CODESYS for raspberry pi PLC in industrial automation


Starting a Ladder project

We need a new Codesys project with a PLC_PRG in Ladder Logic Diagram (LD). This will give us an expanded Ladder toolbox which will be needed for us to program the PLC.

We also need the code snippets that will allow us to use the GPIOs. To do that, we'll have to copy the POUs (Program Organization Units) from the Ladder preset project in the Github and paste them in the analogous POUs window of our program. Along those, we will also need the rpiplc folder, available in the same preset project on the Devices view.

https://github.com/Industrial-Shields/Projecte-CodeSys/tree/main/Presets


Method #1: execution blocks

A first method is to use "Execute" blocks to run code in Structured Text syntax. This block is found in ToolBox > General > Execute.

In the following example, the block is executing the mandatory i2c_init() function just once, at the beginning of program execution. The init function must be executed once at the beginning of the code, each time the code starts.The following is a common implementation.

The code used is the same as in the Structured Text tutorial:

Setting up CODESYS for raspberry pi PLC in industrial automation

Here's an example of an execution block that writes a digital value, operates on a variable and has a digital read.


Method #2: Ladder POUs

If we open the Ladder template project from the GitHub, there's a folder named "rpiplc" in the Devices view. This set of functions allows us to add blocks to the Ladder editor which will interact with the GPIOs of the PLC. Careful! Interruption pins aren't managed by them.

The inner workings of those blocks differ from the previous execution example. We need an extra input with a WSTRING that defines the port name.

In the following example, we are reading the port "I0.8", and storing the value to the "analog_reading" variable.


List of Functions for interacting with the GPIOs

Generic functions found in the "rpiplc" folder:

  • i2c_init()
    Initializes the i2c bus, responsible for communicating values to the GPIOs. Requires no input, and always returns a BOOL: FALSE. It must be executed once at the beginning of the code, each time the code starts. The following image pictures a common implementation.

Init block example

Functions found in the "rpiplc/GPIOs" folder, managing the ports of the analog layers of the PLC:

  • a_analog_read(WSTRING port)
    Analog read. Requires a port name, returns a UINT.
  • a_analog_write(UINT value, WSTRING port)
    Analog write. Requires a port name and a UINT. Returns a BOOL.
  • a_digital_read(WSTRING port)
    Digital read. Requires a port name. Returns a BOOL.
  • a_digital_write(BOOL value, WSTRING port)
    Digital write. Requires a port name and a BOOL. Returns a BOOL.

Functions found in the "rpiplc/GPIOsR" folder, managing the ports of the relay layers of the PLC:

  • r_relay_write(WSTRING port, BOOL value)
    Relay write. REquires a port name and a BOOL. Returns a BOOL.
  • r_analog_read(WSTRING port)
    Analog read. Requires a port name, returns a UINT.
  • r_analog_write(UINT value, WSTRING port)
    Analog write. Requires a port name and a UINT. Returns a BOOL.
  • r_digital_read(WSTRING port)
    Digital read. Requires a port name. Returns a BOOL.
  • r_digital_write(BOOL value, WSTRING port)
    Digital write. Requires a port name and a BOOL. Returns a BOOL.


The following example shows a digital write to the port "Q0_4"(Q0.4), using the value "i". It returns an acknowledgement variable "ack".




​Search in our Blog

Programming in Ladder using CODESYS and raspberry PLC
Boot & Work Corp. S.L, Francesc Nosàs Moliner November 11, 2024
Share this post
Tags

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.

PLC Comparison