Industrial control programming and protocols on a Raspberry Pi

Small Industry

© Lead Image © Nelli Valova, 123RF.com

© Lead Image © Nelli Valova, 123RF.com

Article from Issue 256/2022
Author(s):

Create automation projects with ladder logic, function blocks, structured text, and Modbus TCP.

Home automation projects can take advantage of a number of good software packages (e.g., Home Assistant and Node-RED) to manage and monitor sensors and controllable devices. If you are interested in looking at an industrial controls approach to your automation projects, then OpenPLC [1] is a good package to consider. A programmable logic controller (PLC) is a hardened industrial hardware device that manages I/O and logic according to the IEC 61131-3 standard [2].

The OpenPLC open source software runs on a Raspberry Pi, Linux, or Windows PC, and it offers users a great way to learn industrial control concepts, programming languages, and communications protocols.

In this article, I introduce IEC 61131-3 programming by creating three small OpenPLC programs that use ladder logic, function blocks, and structured text programming languages. The programs in the projects presented here connect to the Raspberry Pi general purpose input/output (GPIO) pins for basic read and write functions; then, the PLC project passes data by Modbus TCP to a Node-RED [3] dashboard.

Getting Started

The OpenPLC software comes in three packages: a logic editor, the runtime component, and a graphic builder. You can find specific instructions for your installation online [4]. For my installation, I put the OpenPLC editor on my Ubuntu PC for some standalone configuration and testing, but you could also load both the editor and OpenPLC runtime on a Raspberry PI. The OpenPLC runtime has a web interface, so logic can be uploaded through a web browser. I didn't install the OpenPLC graphic builder; instead, I used Node-RED dashboards as my final user interface.

OpenPLC has a good number of optional communications packages and subsidiary I/O components (Figure 1). For this application, I created an OpenPLC project with three programs: a ladder program, a function block program, and a structured text program.

Figure 1: OpenPLC on a Raspberry Pi.

The resource object (Res0) defines global variables that can be used by all programs (e.g., pushbutton PB1) and the task cycle times (Figure 2). One of the benefits of a PLC is that it allows for easy management of task scheduling and program cycle times. Because this project is small, I put all the programs into the same task execution (task0). For a larger project, I might put all my digital logic into a fast task execution (20ms) and my analog logic into a slower task execution (250ms).

Figure 2: OpenPLC project setup.

I wanted OpenPLC to do some basic Raspberry Pi pin reads and writes, so I used a setup with a pushbutton on pin 17 and an LED on pin 23 (Figure 3). OpenPLC defines the Raspberry Pi GPIO pins by IEC 61131-3 addressing (Figure 4). For this project, the pushbutton at Broadcom SOC channel (BCM) pin 17 (physical pin 11) is addressed by %IX0.3, which means an input bit on bus 0 at bit 3. The LED at BCM pin 23 (physical pin 16) is addressed by %QX0.2, an output bit on bus 0 bit 2.

Figure 3: Raspberry Pi hardware setup.
Figure 4: IEC 61131-3 addressing for Raspberry Pi pins.

Note that OpenPLC has allocated all the left side (odd) pins of the Raspberry Pi as inputs and all the right side (even) pins as outputs.

The first step in this project is to create some IEC 61131-3 programs to connect to the Pi pushbutton and LED. Once the programs are created, they are loaded and compiled on the OpenPLC runtime; the web interface has a monitor page to view the logic variables.

Ladder Diagrams

Ladder logic was the first IEC 61131-3 programming language, developed as a graphic representation for circuit diagrams of relay logic hardware. The term "ladder diagram" (LD) describes the appearance of the logic, which looks a little like a ladder (Figure 5), with the left side having a vertical power rail and the right side a vertical ground rail; a series of horizontal lines, or "rungs," are wiring hardware components between the rails.

Figure 5: AND/OR logic in a ladder diagram.

Most electricians feel very comfortable with ladder logic, and it is an effective programming method for managing digital logic. If you come from a programming background, ladder logic might feel a bit strange at first.

Figure 5 is an example of AND/OR logic to light two LEDs with two pushbuttons. In this example, both buttons need to be pushed to light LED 1, whereas pushing either button lights LED 2

For my ladder program, I want to light an LED for three seconds with a single push of a button. In the OpenPLC editor, I referenced an external pushbutton variable, PB1 (defined in Res0), and I created two local variables: LED2, my output LED, and TOF0, an off-delay timer.

IEC 61131-3 has a wide range of functions that can be used in ladder rungs. In this example, a timer-off delay (TOF) function was inserted after the pushbutton, and the time parameter (3,000ms) is wired in as a variable (Figure 6).

Figure 6: Ladder logic to light an LED with a pushbutton.

Function Block Diagrams

One limitation of ladder logic is that managing analog logic can be a little messy; therefore, function block diagrams (FBDs) were developed. If you are comfortable with graphic programming applications like Node-RED, you shouldn't have any problems working with FBDs.

For my FBD program (Figure 7) I wanted to count the number of times the LED was lit and output the value to a Modbus holding register. As in the ladder program, the external PB1 variable is referenced. A new output, CNT_FB, is defined as %QW100, an output word that can be accessed by the Modbus protocol at address 100. (I'll explain more about Modbus when I connect Node-RED to OpenPLC.)

Figure 7: Function blocks to count pushbutton presses.

The FBD uses a rising edge trigger (R_TRIG) to catch when the LED turns on. The output from R_TRIG is a boolean, so the value is converted to an INT and added to the value of CNT_FB.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • PiXtend V2

    The PiXtend board extends the Raspberry Pi with many useful interfaces and functions for new target groups.

  • Node-RED on Android

    We show you how to control devices connected to Rasp Pi GPIO pins with text messages from an Android phone.

  • Monitoring Old Devices

    Create monitoring dashboards with SSH, command-line tools, and Node-RED.

  • Node-RED

    Node-RED lets you connect ready-made code building blocks to create event-driven applications with little or no code writing.

  • OPC Unified Architecture

    Use Python and Node-RED to create an industrial client-server test system.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News