Industrial control programming and protocols on a Raspberry Pi
Small Industry
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.
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).
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.
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.
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).
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.)
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.