PLC Programming with the Raspberry Pi and the OpenPLC Project
PLC Programming with the Raspberry Pi and the OpenPLC Project
books books
>
PLC Programming
with the Raspberry Pi
PLC Programming
PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt
and the OpenPLC project with the Raspberry Pi
ModbusRTU and ModbusTCP examples
with the Arduino Uno and ESP8266
and the OpenPLC project
Introduction to PLC programming with OpenPLC, the fi rst fully
Josef Bernhardt became
ModbusRTU and ModbusTCP examples
open source Programmable Logic Controller on the Raspberry
Pi, and Modbus examples with Arduino Uno and ESP8286
interested in electronics
at a very young age, when
with the Arduino Uno and ESP8266
he built his first detector
PLC programming is very common in industry and home automation. receiver, followed over
the years by many other
This book describes how the Raspberry Pi 4 can be used as a Program-
circuits.
mable Logic Controller. Before taking you into the programming,
the author starts with the software installation on the Raspberry Pi He gained his first
and the PLC editor on the PC, followed by a description of the programming experience
hardware. in the 1980s with the
CommodoreVC20.
He became familiar with
You'll then find interesting examples in the different programming assembler programming
languages complying with the IEC 61131-3 standard. on the 8088 processor.
This manual also explains in detail how to use the PLC editor and
how to load and execute the programs on the Raspberry Pi. Josef can look back on more
than 30 years of electronics
All IEC languages are explained with examples, starting with LD
and software development
(Ladder Diagram) over ST (Structured Control Language) to at University of Regens-
SFC (Special Function Chart). All examples can be downloaded burg.
from the author's website.
With his own SMD
production, he also
Networking gets thorough attention too. The Arduino Uno and the
implements electronic
ESP8266 are programmed as ModbusRTU or ModbusTCP customer projects, always
modules to get access to external peripherals, reading sensors and driven by the pleasure
switching electrical loads. I/O circuits complying with the 24V of electronics and
industry standard may also be of interest for the reader. programming.
Josef Bernhardt
Josef Bernhardt
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Declaration
The Author and the Publisher have used their best efforts in ensuring the correctness of the information contained
in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other
cause.
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops
and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social
media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Content
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5.1 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.5.9 Combining structures with arrays using the OpenPLC Editor . . . . . . . . . . . 103
●5
PLC Programming with the Raspberry Pi and the OpenPLC Project
5.2 Modbus TCP module with the ESP8266 and WLAN . . . . . . . . . . . . . . . . . . . . . . 158
5.3 Web server application with the ESP8266 I/O module . . . . . . . . . . . . . . . . . . . . 168
6.4.1 Circuit diagram for the test board with buttons and LEDs: . . . . . . . . . . . . 181
6.4.2 Circuit diagram for the test board with 24V input outputs: . . . . . . . . . . . . 183
6.4.3 Circuit diagram for the ESP8266 board with 24V input outputs: . . . . . . . . 185
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
●6
Preface
Preface
This book is intended to provide readers with a practical introduction to using the Raspberry
Pi computer as a PLC (programmable logic control) for their projects.
The project is indebted to programmers Edouard Tisserant and Mario de Sousa. They start-
ed the "Matiec project" after the introduction of IEC standard 61131-3 in 2003. This made
it feasible to translate the programming languages introduced in the standard into C pro-
grams.
Later, when the Raspberry Pi became increasingly popular, Thiago Alves started the "open-
plcproject". He extended the editor from the "Beremiz" project and wrote a runtime library
and a web interface for the Raspberry Pi and the PC. From then on, it was possible to write
programs on the PC and install them on the Raspberry Pi.
Many Raspberry Pi users are now able to realize their own controls and regulation systems
using their own hardware. The hardware and software are also excellent for training pur-
poses because it abides by the IEC standard.
Beginners will also learn everything about installation and programming in the five pro-
gramming languages in order to build their own control systems.
Circuits with the Arduino and ESP8266, which are necessary for Modbus, are also explained.
●7
PLC Programming with the Raspberry Pi and the OpenPLC Project
Introduction
"A PLC is a digitally operating electronic system for use in industrial environments with
a programmable memory for internal storage of user-oriented control instructions to
implement specific functions such as logic control, sequence control, timing, counting,
and arithmetic functions to control various types of machines and processes through
digital or analog input and output signals."
Various ready-made PLCs based on the Raspberry Pi are available on the market.
A big advantage of PLC programming is that the programmer does not have to learn the
hardware details of I/O lines. The analog and digital outputs and inputs are like variables.
This is also possible with modules that are connected to the Raspberry Pi via a network
using a protocol such as Modbus-TCP.
Figure 0.1 pictures a PLC with 24 V inputs and relay outputs attached to a Raspberry Pi.
●8
Introduction
Most PLC systems support graphical and textual programming languages. The "openplcpro-
ject" fully supports the IEC 61131-3 standard, which defines basic software architecture
and programming languages for PLCs.
The system consists of a runtime component, which is basically the software installed on
the Raspberry Pi. This executes the PLC program. The program editor is installed on the PC
under Windows or Linux to write the PLC program according to the IEC 61131-3 standard.
Ladder Logic LD
Instruction List IL
Structured Text ST
●9
PLC Programming with the Raspberry Pi and the OpenPLC Project
Graphical languages KOP and FUP are translated to ST by the Matiec compiler.
Chapter 1 starts with the installation of the runtime component on the Raspberry Pi. After
downloading the installation program, you will create a micro SD card with the operating
system. After commissioning, you will install the PLC Runtime and perform the first test.
Next, you will deal with the editor and its user interface in Chapter 2. You'll get to download
a finished example and translate it into a program for transferring to the Raspberry Pi.
In Chapter 3 you start programming with the PLC editor from "openplcproject". You will
create your programs in various programming languages, translate, and upload them to
the Raspberry Pi for testing.
Visualization should not be neglected in the process, and Chapter 4 examines the Ad-
vancedHMI project which allows you to visualize processes running on the PLC via Modbus,
on a PC.
Chapter 5 examines the possibility of communicating with external modules. You'll be us-
ing the popular Modbus/RTU protocol for Arduino UNO and the Modbus/TCP protocol for
ESP8266 via WLAN. Also, circuits and layouts for this hardware are presented.
All program examples can be downloaded from the author's website. The links to the web-
site are in the Appendix under "Web Links".
● 10
Chapter 1 • Installing the Raspberry Pi 4
The RPi 4 has enough interfaces to be used as a PLC. In addition to standard interfaces
such as HDMI, USB, Ethernet, and audio, it has a 40-pin GPIO connector strip to connect
to the outside world. Relays, buttons, switches, etc. can be connected to this connector via
suitable interfaces. Hardware aspects are not discussed further for now — the Raspberry Pi
website is host to several detailed tutorials about this minicomputer.
If you are looking for a more compact solution, you could also use the Raspberry Pi Zero
W. After a first test by the author, everything turned out to work fine. If you can tolerate
the lengthy installation process which takes several hours, the "W" is a good, low-priced
alternative to the Raspberry Pi 4.
To use the Raspberry Pi as a PLC, first install the operating system, which can be found on
the Raspberry.org website. Link: Raspberry Pi OS – Raspberry Pi.
● 11
PLC Programming with the Raspberry Pi and the OpenPLC Project
https://www.youtube.com/watch?v=J024soVgEeM
Link: https://downloads.raspberrypi.org/imager/imager_1.5.exe
After clicking on "Install", the installation of the Imager program will commence. The Im-
ager is used to install our Micro SD card.
● 12
Chapter 1 • Installing the Raspberry Pi 4
With a click on the checkbox "Run Raspberry Pi Imager" the program launches after suc-
cessful installation.
Select the Raspberry Pi OS (32-bit) as the operating system. Then select the drive where
the Micro SD card is connected. Here, that's drive G:
● 13
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now the operating system is downloaded and written to the micro SD card. This can take
about 30 minutes, depending on the computer. After the signal that writing to the card has
been completed, exit the Imager with "CONTINUE".
Documentation for installation and usage can also be found here. Link: Raspberry Pi Doc-
umentation
The next step is to set up the Raspberry Pi. Insert the programmed Micro SD card into
the Raspberry Pi. Next, plug in a monitor, keyboard, and mouse for the first setup of the
Raspberry Pi. Now connect the 5 V power supply to the Raspberry Pi. The operating system
should boot.
After the initial start, the operating system prompts for the country and time zone selec-
tion. Enter your details here. Now the settings are installed. After this, the password is
requested. The default username is "pi", and the password is "raspberry".
If there is no Ethernet connection, set up the network access via WLAN. In the upper right
corner between the Bluetooth and the speaker icon, click on the WLAN icon. Select your
WLAN network and enter the access code. After a few seconds, the connection to the home
or office WLAN is established. Continue with the installation of the updates. This can take
several minutes. After this, a reboot is performed. When you click on the WLAN icon, you
will also see the IP address. Make a note of it for later use with VNC and the PLC software,
which is yet to be installed. Here, the IP is: 192.168.178.89.
Later, when the Raspberry Pi is used as a PLC, it will be easier to access it through VNC
Viewer.
Follow: Menu Settings Raspberry Pi Configuration. Next, under Interfaces, the VNC
Viewer can be enabled.
● 14
Chapter 1 • Installing the Raspberry Pi 4
Link: https://www.realvnc.com/en/connect/download/viewer/.
The appropriate viewer can be downloaded at this url. We're using the latest version for
Windows 10, but an Android version is also available.
● 15
PLC Programming with the Raspberry Pi and the OpenPLC Project
Continue the installation by clicking on "Next". Click on "Install". This will launch the instal-
lation of the Viewer.
Under "File", and then "New Connection", enter the access data from your Raspberry Pi.
● 16
Chapter 1 • Installing the Raspberry Pi 4
Enter the IP address and a suitable name. Afterwards, confirm the "Properties", "General"
window with "OK".
Enter the username "pi" and password "raspberry" and confirm with "OK". Shortly, the
Raspberry Pi desktop will appear on your PC screen.
● 17
PLC Programming with the Raspberry Pi and the OpenPLC Project
To get access, enable SSH on the Raspberry Pi under Settings Raspberry Pi Configuration
Interfaces.
● 18
Chapter 1 • Installing the Raspberry Pi 4
● 19
PLC Programming with the Raspberry Pi and the OpenPLC Project
Select the user interface "Commander", go to "Next" and confirm with "Install".
● 20
Chapter 1 • Installing the Raspberry Pi 4
Start WinSCP and set up a new connection with the Raspberry Pi's IP address. As an exam-
ple, the IP address was 192.168.178.89 with access data.
You can now access the file directories of the Raspberry Pi.
● 21
PLC Programming with the Raspberry Pi and the OpenPLC Project
Link: https://www.openplcproject.com/runtime/raspberry-pi/
The easiest way to copy OpenPLC files to your Raspberry Pi is to use Git. Normally, Git is
preinstalled on Raspbian. If for some reason you don't have Git installed on your system,
you can fix that by typing some commands. To be able to do that, open a Terminal box
either on the Raspberry Pi or via the VNC viewer and enter the following command:
● 22
Chapter 1 • Installing the Raspberry Pi 4
For the installation of the OpenPLC Runtime (PLC operating system) you need the following
commands:
These can also be copied and pasted into the terminal window.
On the PC, copy the command to the clipboard and paste it on the Raspberry Pi in the Ter-
minal window with the right-hand mouse button.
● 23
PLC Programming with the Raspberry Pi and the OpenPLC Project
This process takes some time. After the installation is finished, you should restart the
Raspberry Pi.
The Open PLC Runtime has an integrated web server allowing you to configure OpenPLC
and upload new programs. Subsequently, these can be started on the Raspberry Pi. This
web server can be accessed by opening a web browser on the PC and entering the IP
address of the Raspberry Pi through port 8080. For example, if your Raspberry Pi is at IP
192.168.178.89 in your network, you need to enter the following address in your browser:
192.168.178.89:8080
Of course, the web page can also be accessed on the Raspberry Pi with the web browser.
Just enter "localhost:8080" in the URL line to get to the OpenPLC Runtime page.
● 24
Chapter 1 • Installing the Raspberry Pi 4
The default username and password are both: "openplc". These credentials can be changed
after login.
After a successful login, the start menu pops up. Now various important settings for the
hardware can be made. Our own PLC programs are also uploaded and started here. Config-
uration of external modules is also possible as well as monitoring of the running program.
● 25
PLC Programming with the Raspberry Pi and the OpenPLC Project
This is where the extensive selection takes place of the hardware running the Open PLC
Runtime. Here, choose the Raspberry Pi. Save the setting with "Save Changes".
DNP3 and Ethernet/IP do not need to be selected for the time being, since you do not use
these field buses. We leave the Modbus server installed as it's needed later for visualization
using the AdvancedHMI software.
In case the Raspberry Pi should run the last program after a reboot, you can click on
Settings Start OpenPLC in RUN Mode. Now click "Save Changes" and the installation is
finished.
For an initial test, use the PLC program "PLC sample AWL running light", which can be found
in the download area. To test drive the PLC program, transfer it from the PC to the new
Raspberry Pi PLC by way of the web interface.
● 26
Chapter 1 • Installing the Raspberry Pi 4
For this, click on "Browse" for the file dialog, select the ST file from your PLC project, and
confirm with "Open".
● 27
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now enter a name at the top under "Name". Here it's "PLC Example Running Light" so that
the Upload button is enabled. Under "Description" a description of the program function and
possibly a version number, can be entered.
● 28
Chapter 1 • Installing the Raspberry Pi 4
Now the ST program gets compiled into a C program for the Raspberry Pi. After that it can
be started. Click on "Go to Dashboard" and change to the main menu. By clicking on "Start
PLC", the program kicks off.
Now three LEDs at the output pins %QX0.0, %QX0.1 and %QX0.2 should light up one after
the other. You should also be able to observe this under "Monitoring".
The complete schematic of the test board can be found in the Appendix.
If this test does not work, it's probably because of the "WiringPI" library this is responsible
for accessing the GPIO pins. A likely solution to the problem is to install an up to date,
Raspberry Pi 4-adapted, version. To do so, halt the running PLC program with the button
"Stop PLC".
● 29
PLC Programming with the Raspberry Pi and the OpenPLC Project
Next, in the terminal window enter the following commands for an update:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
After that, the installation can be tested. The version 2.52 should be displayed.
gpio –v
It should display
Raspberry Pi Details:
Type: Pi 4B, Revision: 01, Memory: 2048MB, Maker: Sony
* Device tree is enabled.
*--> Raspberry Pi 4 Model B Rev 1.1
* This Raspberry Pi supports user-level GPIO access.
This completes the installation, and the PLC program is ready for starting. Now the LEDs
should light up one after the other.
● 30
Chapter 1 • Installing the Raspberry Pi 4
Now you can exit and sign out of the Raspberry Pi by clicking on "Logout".
The next Chapter covers installing the editor on your PC and writing your first program.
● 31
PLC Programming with the Raspberry Pi and the OpenPLC Project
This Chapter aims to deal with the editor you'll be using to create your PLC programs. First,
the OpenPLC editor gets installed and then you will get to testing your first programs with
the proposed hardware.
After the download, unpack the archive into a folder, e.g., "OpenPLC Editor" (under Docu-
ments), then go to that folder. This already completes the installation. Double-click on the
"OpenPLC Editor" shortcut should launch the program.
● 32
Chapter 2 • OpenPLC Editor Installation
By default, the editor starts in the Windows system language. It's currently not possible
to change that in the editor. However, there is a simple way to make the change in the
"TranslationCatalogs.py" file. This is located in the directory "\OpenPLC Editor\editor\util".
If necessary, search the function "AddCatalog" here line 59 and modify the entry:
https://wxpython.org/Phoenix/docs/html/wx.Language.enumeration.html
https://openplc.discussion.community/post/language-for-the-user-interface-10618877
In case you run into problems, there's a lot of helpful information to be found there.
Now download a sample program from the OpenPLC website and save the directory under
Documents.
A project always consists of two files. If you open them with an editor like Notepad++, you
will see the XML structure.
https://www.openplcproject.com/runtime/raspberry-pi/.
In the editor You can then click on the directory under "FileOpen" and confirm with "OK".
Now the project gets loaded.
● 33
PLC Programming with the Raspberry Pi and the OpenPLC Project
After double clicking "Hello_World" in the structure tree on the left, you see the ladder di-
agram (LD) program. Here is a simple example with a button, a timer, and a relay. When
a button is pressed, the relay switches on and after 2000 ms have elapsed, it switches off
again.
● 34
Chapter 2 • OpenPLC Editor Installation
Now you want to translate the program and upload it to the Raspberry Pi.
First, you translate the program into an ST file by clicking on the red down arrow .
● 35
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here, you enter a name for the program, e.g., "HelloWorld". The extension ".ST" will be
added automatically.
In the console you can watch how the KOP program gets translated into an ST program by
the Matiec compiler. This compiler translates the graphical PLC programs into an ST file.
The file "HelloWorld.ST" has now been created inside the "HelloWorld" directory.
● 36
Chapter 2 • OpenPLC Editor Installation
The ingenious thing is, you can see which ST code the compiler generates from the KOP
program. As a result, you could write the ST programs for the Raspberry Pi with a simple
text editor like Notepad++.
PROGRAM Hello_World
VAR
my_button AT %IX0.0 : BOOL;
lamp AT %QX0.0 : BOOL;
END_VAR
VAR
TOF0 : TOF;
END_VAR
CONFIGURATION Config0
In lines 3 and 4 you can also see the assignment of the Boolean variables "my_button" and
"lamp" to the Raspberry Pi output pins.
● 37
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here you see the pin assignment of the variables to the pins on the GPIO header of the
Raspberry Pi. The pin "%QW0" is a PWM output. This can be used, for instance, to change
the brightness of a LED, or the speed of a motor.
Now you can upload the file as described in Chapter 1. You log in to the web server of the
runtime program with the Raspberry Pi's IP address "192.168.178.89:8080". Again, use
our credentials "openplc" for the "username" as well as the "password".
● 38
Chapter 2 • OpenPLC Editor Installation
Under "Programs", click the "Browse" button, then select the ST file you created and saved
with the Editor.
After opening, click on "Upload program" and enter a program name in the Name field.
Confirm with Upload "Program". Now the ST file gets translated into a C program and com-
piled. If everything is okay, you will see the message:
Return to the Dashboard (red button) and start the program. Now you can test it on the
Raspberry Pi. It's recommended to use a small test board with pushbuttons (see schemat-
ic). A setup on a breadboard is of course also possible.
● 39
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here is a photo of the board with dimensions 85 x 68 mm. The lines can be connected
through to the Raspberry Pi one-to-one.
● 40
Chapter 2 • OpenPLC Editor Installation
After pressing the left button "IX0.0", the LED "QX0.0" should be on for 2 seconds.
Under "Monitoring" you can also observe the function using the web interface on the Rasp-
berry Pi. After pressing a pushbutton, it looks like this:
When pressing the key, the variable "lamp" changes to green. Five seconds later it changes
back to red. The keystroke can also be observed nicely. These possibilities are highly inter-
esting and useful to test programs and to find errors in your own programs.
● 41
PLC Programming with the Raspberry Pi and the OpenPLC Project
The input circuit is suitable for 24 V as usual for PLC applications. It consists of a voltage
divider with resistors and Zener diode. If 24 V gets applied via a switch or a pushbutton, the
optocoupler forward biases the output transistor. Now the GPIO input receives a logic High
signal. The level is indicated by the LED at the input of the optocoupler.
The HITFET type BSP76 from Infineon is used as an electronic switch. It has various pro-
tection mechanisms such as overtemperature, current limiting and overvoltage. It can also
switch inductive and capacitive loads.
● 42
Chapter 2 • OpenPLC Editor Installation
A later chapter discusses the "AdvancedHMI" PC software too. Here you get to use the free
"Visual Studio" development environment to create an HMI program with a graphical user
interface to monitor and switch pins on the Raspberry Pi from the PC. The HMI software
also works on the Raspberry Pi provided you have installed the MONO framework.
The next Chapter offers deeper insights into the editor from the OpenPLC project.
● 43
PLC Programming with the Raspberry Pi and the OpenPLC Project
After launching the program, the main menu appears, where a new project can be created.
Here is a structure tree with an example which uses function blocks from different IEC
languages.
● 44
Chapter 3 • The OpenPLC Editor
The "File Menu" to create new projects as well as to open existing projects.
● 45
PLC Programming with the Raspberry Pi and the OpenPLC Project
The Edit menu is for copying and pasting objects as well as for searching objects.
Here are the possible project settings. Among other things: company name, author, prod-
uct version, etc. can be entered to document the project.
● 46
Chapter 3 • The OpenPLC Editor
The "View" menu to switch the view to full screen and back again.
The "Help" menu, here you see the community support web page and a list of the staff
involved in the translation of the user interface.
From the library menu on the right, numerous ready-to-use function blocks can be selected.
● 47
PLC Programming with the Raspberry Pi and the OpenPLC Project
This window for the function blocks selection menu appears when you right-click on the
current worksheet.
This is the "Debug" menu for testing your program on the PC and selecting the variables
to be monitored.
● 48
Chapter 3 • The OpenPLC Editor
Setting the program interval for the "MainTask" and possibly other programs of the project.
A program cycle of 20 ms was selected here. That means your program is executed fifty
times per second. It is important to make sure that the program runtime is no longer than
the program cycle.
● 49
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here, the right side shows the elements such as busbar, coil, and contact.
● 50
Chapter 3 • The OpenPLC Editor
Here you see on the right side the elements like Step, Transition, Action, Branch, Jump.
LD Ladder Diagram
IL Instruction List
ST Structured Text
● 51
PLC Programming with the Raspberry Pi and the OpenPLC Project
Using the programming language ladder diagram (LD), your programs are represented
graphically, similar to circuit diagrams with contacts and coils. A program can consist of
several such networks. There are also many function blocks that can be inserted into a LD
program.
● 52
Chapter 3 • The OpenPLC Editor
The function block language (FBD) is also a graphical programming language. Here blocks
like AND, OR, as well as timers etc. can be used. It is also possible to use function blocks
of the library as with LAD. Here, in contrast to LAD, no busbars, contacts, or coils are used.
The programming language Instruction List (IL) is one of the oldest languages for PLC
programming. It's text-based and largely resembles the assembly language of microcon-
trollers. The instructions are processed in the order in which they appear in the program.
Programs in IL can be used like all languages also in functions and function blocks.
● 53
PLC Programming with the Raspberry Pi and the OpenPLC Project
The programming language dubbed "Structured Text" ST or SCL is largely similar to the
programming language Pascal by the Swiss computer scientist Niklaus Wirth. It is a mod-
ern language for creating clear, structured programs. Besides "IF THEN" there are also "DO
WHILE" and "REPEAT UNTIL" control structures for the programs. You can find a detailed
overview of ST in Chapter 3.5.
● 54
Chapter 3 • The OpenPLC Editor
The Sequential Function Chart (SFC) is a graphical programming language commonly used
in sequential control systems.
There are steps, transitions, actions and branches to program sequences. For the actions,
it is also possible to execute them along with timers. An ideal language for applications like
conveyor belts, gate controls and production lines.
The next Chapter kicks off with an example of the "Ladder Diagram" (LD) programming
language.
We now write a LD (ladder diagram) program to switch an output "%QX0.0" of the Rasp-
berry PI. The switching is done with a push button "%IX0.0" for On and a "%IX0.1" for Off.
● 55
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here is a brief explanation of the circuit. After pressing the "ON" button, the relay coil gets
its voltage, and the relay energizes. Now the "LED" contact is closed, which is connected to
the relay. After releasing the "ON" button, the relay continues to be supplied with voltage
via the "LED" contact. By pressing the "OFF" button, the electric circuit is interrupted, and
the relay drops out.
● 56
Chapter 3 • The OpenPLC Editor
Using "File à New", you create a new project, let's call it "PLC-Example-LD".
In the file dialog, enter the project name. Here you select "PLC-Example-LD" in the subdi-
rectory of your installed OpenPLC Editor.
● 57
PLC Programming with the Raspberry Pi and the OpenPLC Project
In this query "New POU", select the POU name "PLC_Example_LD", the POU type "Pro-
gram" and the programming language "LD" for ladder diagram. Then confirm with OK. The
underscore in the name is necessary because a – sign (i.e., minus) is not accepted.
The height and width of the fields can be adjusted at any time by holding down the left
mouse button.
Now we enter the variables in the middle field with under Name, Class and Type. Select
the appropriate data type and the assignment to the outputs.
● 58
Chapter 3 • The OpenPLC Editor
Please make sure to select the data type BOOL and enter the corresponding PIN from the
Raspberry PI under IEC address.
Now continue with inserting the IEC symbols from the library on the right or above. Select-
ing is also possible with a click of the right mouse button on the worksheet.
● 59
PLC Programming with the Raspberry Pi and the OpenPLC Project
We first select the "New Power Line" icon and click in the grid-structured worksheet field
where the KOP program is entered.
New coil
New contact
New variable
New connection
Here, confirm with OK. The symbol now appears on the worksheet.
● 60
Chapter 3 • The OpenPLC Editor
Next, you need one contact for "On" and one for "Off". Select "Normal" for the modifier and
the value "On" from the Variable combo box.
● 61
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now you can put the switch on your worksheet next to the left-hand power line.
For the opener "Off", select the modifier "Negated" and from the combo box "Variable", the
value "Off".
● 62
Chapter 3 • The OpenPLC Editor
Then select the power supply on the right and place it on the worksheet.
● 63
PLC Programming with the Raspberry Pi and the OpenPLC Project
In order to get a latching when the pushbutton is pressed "On", you need an additional con-
tact with the variable name of the coil (LED). It's a pushbutton, but the value corresponds
to the Boolean value of the LED. This is principally the relay contact you saw in the circuit
diagram at the beginning of the chapter.
● 64
Chapter 3 • The OpenPLC Editor
Now you're set to establish the connections by clicking on the connectors of the respective
part and moving to the target object with the mouse button held down and the releasing it.
● 65
PLC Programming with the Raspberry Pi and the OpenPLC Project
Once all connections are made, you can try to translate the program into a ST file using
the "Create program " icon. This file contains the ST program you need to upload to the
Raspberry PI. Be sure to select the correct directory.
After compiling you'll be able to see the ST file in our project directory. Upload it to the
Raspberry PI as already shown in the previous chapters and test the program. Now you
can switch on the LED "%QX0.0" with the button "%IX0.0" and switch it off again with the
button "%IX0.1".
● 66
Chapter 3 • The OpenPLC Editor
If you now open the file "PLC-Example-LD.ST", you can see how the Matiec compiler has
translated the LD program into an ST program.
You're set to write a FBD program to switch an output "%QX0.0" on the Raspberry PI. The
switching is done with one button "%IX0.0" for On and one "%IX0.1" for Off. The program
functionality is identical to the previous chapter so you can see the difference between LAD
and FBD. Next, the program gets extended with an "AND" and an "OR" gate.
● 67
PLC Programming with the Raspberry Pi and the OpenPLC Project
With "File New" you create a new project, let's call it PLC-Example-FBD.
In the file dialog, enter the project name. Here, you select "PLC-Example-FUP" in the sub-
directory of your installed OpenPLC Editor.
● 68
Chapter 3 • The OpenPLC Editor
At this "New POU" request, select the POU name "PLC_Example_FBD", the POU type "Pro-
gram", and the programming language "FBD" (for function block diagram). Then confirm
with OK. The underscore in the name is necessary because a – sign (i.e., minus) is not
accepted. The project name now appears in the project tree.
Now, with , you enter the variables in the middle field under Name, Class and Type.
Select the appropriate data type and the assignment to the outputs as shown in the image.
Next, select an "SR flipflop" from the library under standard function block. This component
has two inputs, one for SET (set) and one for RESET (reset). You can see here already in
the overview that numerous function blocks are available for our programs. By clicking and
holding the left mouse button you can move the function blocks to your worksheet.
● 69
PLC Programming with the Raspberry Pi and the OpenPLC Project
After that, put the variables for "On", "Off" and "Led" on your worksheet and position them
in the right place.
● 70
Chapter 3 • The OpenPLC Editor
After that, you can create the connections between the blocks.
● 71
PLC Programming with the Raspberry Pi and the OpenPLC Project
Comment
New variable
New connection
After all connections are made, go ahead and try to use the "Create Program " icon to
translate the program into an ST file. This file contains the ST program you need to upload
to the Raspberry PI. Do make sure to select the correct directory.
● 72
Chapter 3 • The OpenPLC Editor
Here are the task settings for the program. You can access them by double-clicking on Res0
in the structure tree on the left.
After compiling you can view the ST file in your project directory. Upload it to the Raspberry
PI as already shown in the previous chapters and start the program. Now you can switch
on the "%QX0.0" LED with the button "%IX0.0" and switch it off again with the button
"%IX0.1".
● 73
PLC Programming with the Raspberry Pi and the OpenPLC Project
If you open the file "PLC-Example-FBD.ST", you can see how the Matiec compiler has trans-
lated the FBD program into an ST program. The Notepad++ editor is used here.
Now extend your program with "AND" and "OR" functions as well as further inputs. After
that, you're set to translate it again into an ST file.
You can find the gates in the library under "Bitwise operations".
● 74
Chapter 3 • The OpenPLC Editor
● 75
PLC Programming with the Raspberry Pi and the OpenPLC Project
https://de.wikibooks.org/wiki/Digitale_Schaltungstechnik/_Flipflop/_Einleitung
In this example, you'll get to control an "Led" at pin "%QX0.0" by pressing a button with
"Button" at pin "%IX0.0". Each time the button is pressed, the output state of the pin
changes, i.e., the LED switches on and off. For this, start your OpenPLC Editor again and
create a new project.
Let's call it PLC_Example_IL. Under block name, enter "PLC_Example_IL", select the block
type "Program" and for the language, "IL". Confirm with OK.
● 76
Chapter 3 • The OpenPLC Editor
● 77
PLC Programming with the Raspberry Pi and the OpenPLC Project
After compiling and saving the program, transfer it back to the Raspberry PI to test it. Now
the state of the LED should change with each keystroke.
Here is an example of a running lights where three LEDs light up one after another. This
example employs the function block "Pulse generator TP" — t his can be used to generate
a specified time duration.
(*
PLC_Example_IL_RunningLight
LED1 to LED3 switch one after another at intervals of 500ms
*)
ld t#500ms
st Pulse1.pt
st Pulse2.pt
st Pulse3.pt
● 78
Chapter 3 • The OpenPLC Editor
If you upload the program to the Raspberry Pi and start it, you can see how the LEDs are
switched via the web interface in the menu item "Monitoring".
● 79
PLC Programming with the Raspberry Pi and the OpenPLC Project
Command Description
R Reset value
S Set value
● 80
Chapter 3 • The OpenPLC Editor
The accumulator value is compared with the operand value (> (greater than)). The
GT
value (TRUE or FALSE) is loaded into the accumulator.
The accumulator value is compared with the operand value (> (greater than or equal
GE
to)). The value (TRUE or FALSE) is loaded into the accumulator.
The accumulator value is compared with the operand value (= (equal)). The value
EQ
(TRUE or FALSE) is loaded into the accumulator.
The accumulator value is compared with the operand value (<> (not equal)). The
NE
value (TRUE or FALSE) is loaded into the accumulator.
The accumulator value is compared with the operand value (<= (less than or equal
LE
to)). The value (TRUE or FALSE) is loaded into the accumulator.
The accumulator value is compared with the operand value (<less than). The value
LT
(TRUE or FALSE) is loaded into the accumulator.
JMP Jump to a label
JMPC Jump to a label if the accumulator value is TRUE
JMPCN Jump to a label if the accumulator value is FALSE
CAL Calling a program or function block
CALC Call of a program or function block if the accumulator value is TRUE
CALCN Calling a program or function block if the accumulator value is FALSE
RET Exit the POU and return to the calling program
RETC Exit POU and return to calling program if accumulator value is TRUE
RETCN Exit the POU and return to the calling program if the accumulator value is FALSE.
On pages 134–140.
http://mat.sourceforge.net/manual/logic/il.html
● 81
PLC Programming with the Raspberry Pi and the OpenPLC Project
3.5.1 Variable
Variable definition
Variable names can consist of any combination of up to 32 alphanumeric characters (num-
bers and letters) with the following restrictions:
Variable names are not case-sensitive. The variable names input_10, Input_10 and IN-
PUT_10 are identified as the same variable. Global variables are valid in the whole project
"local" variables only in the "block" where they are defined.
Important! Giving a meaningful name to the variable makes it easier for you and every-
one else to read and comprehend the project.
● 82
Chapter 3 • The OpenPLC Editor
The variable on the left side of the assignment operator is assigned the value of the right
side. The right-hand side can be an equation, a variable, or a constant (value).
Semicolon (;)
All expressions, assignments, function calls, function block calls, program calls, and decla-
rations must end with a semicolon ";".
Comments
Comments should be added to a program to make the program code more readable. Com-
ments are placed between two brackets and asterisks.
Brackets
In the ST language, parentheses are used to change the default evaluation order of vari-
ous operations within a complex expression. For example, in ST, the default evaluation of
"2 * 3 + 4", the multiplication operator "*" returns 10 because it has the highest priority.
Changing the expression to "2 * (3 + 4)" results in 14. Parentheses can be nested in a
complex expression.
The default evaluation order for ST language operations is as follows (the first one has the
highest priority):
Operation: Note
Multiplication / Division: *, /
Addition / Subtraction: +, -
Comparison: <, >, <=, >=, =, <>
Logical AND: &, AND
Logical OR: OR
Logical XOR: XOR
● 83
PLC Programming with the Raspberry Pi and the OpenPLC Project
Example Value := 0;
FOR Count := 1 TO 10 DO
Value := Value + Data[Count];
END_FOR;
Value := Value / 10;
WHILE loop
The commands between DO and END_WHILE are executed as long as the logic condition is
TRUE. The logic condition is evaluated before commands are executed. If the logic condition
is FALSE when the WHILE structure is first reached, commands are not executed.
Notice. Loop commands can result in an infinite loop condition that prevents the current
cycle from being completed. Never test the status of a digital input directly as a logical
condition in loop commands, because the status of the digital input is not updated before
the next cycle.
REPEAT Cycle
Instructions between REPEAT and UNTIL are executed until the logic condition is TRUE.
The logic condition is evaluated after the instructions have been executed. Commands are
executed at least once.
Syntax REPEAT
<Command>
UNTIL
< Logic condition >
END_REPEAT;
● 84
Chapter 3 • The OpenPLC Editor
Example Result:= 0;
REPEAT
Result:= Result + diData[count];
count := count - 1;
UNTIL
count = 0
END_REPEAT;
Caution. Loop commands can result in an infinite-loop condition that prevents the cur-
rent cycle from being completed. Never test the status of a digital input directly as a
logical condition in loop commands because the status of the digital input is not updated
before the next cycle.
IF Command
The execution of commands is conditioned by the logical expression. ELSIF and ELSE ex-
pressions are optional. There may be more than one ELSEIF instruction.
● 85
PLC Programming with the Raspberry Pi and the OpenPLC Project
CASE Command
This command switches between numbered commands. All values listed in the structure
correspond to the evaluation of the DINT expression and are possible states when execut-
ing commands. Commands specified after the ELSE keyword are executed when the DINT
keyword receives a value not defined in the structure. For each case, you must specify a
value, or a list of possible values separated by commas (","), or a range of values speci-
fied by the Min and Max range. You must enter spaces before and after the ".." separator
character.
● 86
Chapter 3 • The OpenPLC Editor
OR X := A OR B; Bit OR
+ X := A + B; Addition
- X := A - B; Substraction
* X := A * B; Multiplication
/ X := A / B; Division
= IF (A = B) THEN.. Equal
ROL X := ROL (A, B); Shifting the content of A by B bits to the left
ROR X := ROR (A, B); Shift the content of A by B bits to the right
● 87
PLC Programming with the Raspberry Pi and the OpenPLC Project
TargetVariable: = ANY_TO_ < New data type > (Variable or constant value);
● 88
Chapter 3 • The OpenPLC Editor
Definition //Timers are built-in function blocks with two input parameters and two
// output values.
// Timer1 : TON; create timer instance in Var
● 89
PLC Programming with the Raspberry Pi and the OpenPLC Project
EXIT Instruction
Exits a loop directly. The EXIT statement indicates that the current loop (WHILE, REPEAT
or FOR) is to be terminated. Execution continues after the loop with the END_WHILE,
END_REPEAT or END_FOR or EXIT keyword. EXIT terminates only one loop and cannot be
used to terminate multiple levels of nested loops simultaneously.
Code example // This program searches for the first element of an array that is not 0:
iFound := -1; (*Meaning: not found *)
FOR iPos := 0 TO (iArrayDim - 1) DO
IF iArray[iPos] <> 0 THEN
iFound := iPos;
EXIT;
END_IF;
END_FOR;
RETURN Instruction
During execution, the RETURN statement jumps to the end of the program block (POU) in
which it is located. Other program blocks (POUs) continue to execute their own tasks. The
input of the symbol must be linked to a valid BOOL logic condition. The jump is executed
only if the input is TRUE. Example: Block is not executed if the EN input pin of a FB program
block is not active.
Start your OpenPLC Editor again and create a new project with the folder name "PLC-Ex-
ample-ST-01". Under block name, enter "PLC_Example_ST_01", select the block type "Pro-
gram", and for the language, choose "ST".
● 90
Chapter 3 • The OpenPLC Editor
First, declare your variables again for the inputs and outputs, and assign the lines. The
function block "BlinkTimer" TON is new.
● 91
PLC Programming with the Raspberry Pi and the OpenPLC Project
Then use the text editor to enter your ST program without the line numbers. The line end
of each statement is terminated with a semicolon.
After compiling and saving the program, transfer it back to the Raspberry PI to test it.
Now LED1 should light up when button1 and button2 are pressed simultaneously., and
LED2 when button3 or button4 is pressed. Due to the XOR logic of pushbutton5 and push-
button6, LED3 lights up if, for example, pushbutton5 is pressed and pushbutton6 is not
pressed or vice versa. LED4 lights up after the program start and switches off when but-
ton7 is pressed. LED5 toggles each time 500 ms have elapsed. The TON Timer "BlinkTimer"
starts again and again by the instruction "IN := NOT BlinkTimer.Q" and sets the variable
"TimerReady". In the IF query, LED5 is then toggled.
● 92
Chapter 3 • The OpenPLC Editor
Now the workpiece is bar-coded. In our example, the barcode output is to be switched for
5 seconds.
Then the workpiece is moved back to the left position, which is detected by the sensor on
the left. The motor switches off and an "OK" lamp lights up. Now the workpiece can be
changed, and a new pass can take place.
A control lamp "BlinkLed", which flashes with 1 Hz, should indicate us that the plant is in
operation.
In this example, a conveyor belt is used whose parts came from the 3D printer. An overview
of the parts with pictures can be found in the download directory of the book files under
"Conveyor Belt". The STL files are also available to 3D-print the parts yourself.
● 93
PLC Programming with the Raspberry Pi and the OpenPLC Project
The motor is controlled with the two lines called "MotorOn" and "MotorDir". You first create
a new project called "PLC-Example-ConveyorBelt".
● 94
Chapter 3 • The OpenPLC Editor
● 95
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 3.5.6.5: ST example for the Conveyor Belt (ST program text).
After that, the program. After the translation has completed, upload it to the Raspberry PI
in the usual way and run a test.
● 96
Chapter 3 • The OpenPLC Editor
Start off with two array data types for a BOOL and an INT array with the array size of 10
each. Kick off the definition of array types by clicking on the plus symbol .
In the structure tree, enter the name for the relevant data type. For the sake of simplicity,
the array data types got designated as "ArrayTypeBool" and "ArrayTypeInt".
● 97
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now pick "Array" from the combo box Reference. It's where you set the data type "Bool" at
"Base type". At Dimension, enter the range "1..10".
Now repeat this process with the "ArrayTypInt". You should then have the two array data
types "ArrayTypBool" and "ArrayTypInt" at your disposal. The symbols in the Dimension
row have the following definitions:
Now you can enter the variables in the program. The two user-defined data types
"ArrayTypeBool" and "ArrayTypInt" are now available for the data type. Pick these for your
variables "PinValues" and "PWMValues" respectively.
● 98
Chapter 3 • The OpenPLC Editor
● 99
PLC Programming with the Raspberry Pi and the OpenPLC Project
Once again you start off with a new project. The definition of structures is started by click-
● 100
Chapter 3 • The OpenPLC Editor
Select Data type and enter the name "MeasuredValues" in the structure tree.
● 101
PLC Programming with the Raspberry Pi and the OpenPLC Project
With the plus symbol you enter in sequence, the elements you wish to get in your
structure.
Enter the elements here with the appropriate data type and an initialization value. For
"Real", the value 0.0 and not 0 should be entered, otherwise the program cannot be trans-
lated. Now the variables "Temperature", "Humidity" and "Counter" are available for the
structure.
● 102
Chapter 3 • The OpenPLC Editor
Now you can enter the variable in the program. The user-defined data type "Measured
values" is now available as a data type.
Now follows the sample program to access the structure variables. The access to the ele-
ments is done via the point.
First, define the arrays by clicking on the plus symbol . Select data type.
● 103
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now we have defined the two arrays for Real and Int. Start the definition of structures by
clicking on the plus symbol , then select data type again.
● 104
Chapter 3 • The OpenPLC Editor
Now create the structure with the array data types defined earlier.
In the program part, you define the variable Room1 with the user-defined structure "Struk-
tRoom" and the Int variable "Loop".
FOR Loop := 1 TO 24 BY 1 DO
Room.Temperature[Loop] := 22.5;
Room.Humidity[Loop] := 48.5;
Room.Number[Loop] := 12;
END_FOR;
● 105
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 106
Chapter 3 • The OpenPLC Editor
With the + symbol, select data type and confirm. For Reference, enter Enumeration.
At this point, use to enter the values MotorOff, MotorLeft and MotorRight. In the ST
program, the user-defined data type "MotorState" is now available for the variables.
● 107
PLC Programming with the Raspberry Pi and the OpenPLC Project
Next, enter the small test program and load it to the Raspberry PI to test it.
CASE State OF
MotorOff:MotorOn:=FALSE; MotorDir:=FALSE;State:=MotorLeft;
MotorLeft:MotorOn:=TRUE; MotorDir:=TRUE;State:=MotorRight;
MotorRight:MotorOn:=TRUE; MotorDir:=FALSE;State:=MotorOff;
END_CASE;
● 108
Chapter 3 • The OpenPLC Editor
Because it runs relatively fast, you set the program cycle to 1000 ms. That allows you to
better observe the program flow.
● 109
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here there are steps, transitions, actions, and jumps to program sequences. Using the
actions, it is also possible to execute them with timers. The menu bar in an SFC project
(sequential function chart).
The available elements of AS are seen on the right. Before entering your first AS program,
browse the brief explanations of the available symbols:
Symbol Description
New comment
New jump
New connection
New contact
You create a new program with the project name "PLC-Example-SFC-01". A pushbutton is
intended to toggle an LED at each button press. This is a simple state machine correspond-
ing to a T-flip flop.
● 110
Chapter 3 • The OpenPLC Editor
Let's again create a new project with the name "PLC-Example-SFC-01" and choose the
language SFC.
As before, define your variables Button and LED with the appropriate inputs/outputs.
With the first step you set the start condition and the connections for an output as well as
the action on your worksheet.
● 111
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here, select the type Inline and enter as a value "LED:= 0;". For a qualifier, use the default
value "N". The values can also be seen in the action block on the worksheet.
Here is an overview of the available qualifiers. There are many options for the way the
action should be executed.
● 112
Chapter 3 • The OpenPLC Editor
Qualifier Description
L The action is executed for a predefined time interval and then terminated.
D The action is executed after a specified time when this step is activated.
P The action is executed once, once the step has become active.
P0 The action is executed once, once the step has become inactive.
P1 The action is executed once, once the step has become active.
SD The action will be executed after some time, even if the step is no longer active.
DS The action is executed only after a certain time if the step is still active.
The "Duration" field is necessary to define the time interval required for some of the "qual-
ifiers" described in the table. "Value" defines the code or specific manipulation that will be
executed during the activation of the action. Here "LED:= 0;".
Here, you query in the Inline field with "Button = 1" whether the button was pressed. Next,
again, comes a step "Step1" with the connections "Input", "Action" and Output.
● 113
PLC Programming with the Raspberry Pi and the OpenPLC Project
After this, the next action follows to the right of it. It allows you to turn on the LED.
Next, proceed again with a transition, where you query whether the switch has been re-
leased again. Now, one by one, put the next steps, transitions, and action blocks on your
worksheet as shown in this image, and enter the appropriate values for them.
● 114
Chapter 3 • The OpenPLC Editor
Finally, you need the "Jump" icon permitting the program to start again at "Start".
Now you can connect your blocks and comment the program.
● 115
PLC Programming with the Raspberry Pi and the OpenPLC Project
Here is your finished program with the comments. You're set to compile it again and upload
it over to the Raspberry PI for testing. After each keystroke with Button, the LED should
now toggle. Here is the program as generated by the compiler:
PROGRAM PLC_Example_SFC_01
VAR
Button AT %IX0.0 : BOOL;
LED AT %QX0.0 : BOOL;
END_VAR
INITIAL_STEP Start:
START_INLINE1(N);
END_STEP
ACTION START_INLINE1:
LED:=0;
END_ACTION
STEP Step1:
STEP1_INLINE2(N);
END_STEP
● 116
Chapter 3 • The OpenPLC Editor
ACTION STEP1_INLINE2:
LED:=1;
END_ACTION
STEP Step2:
STEP2_INLINE3(N);
END_STEP
ACTION STEP2_INLINE3:
LED:=1;
END_ACTION
STEP Step3:
STEP3_INLINE4(N);
END_STEP
ACTION STEP3_INLINE4:
LED:=0;
END_ACTION
END_PROGRAM
CONFIGURATION Config0
● 117
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 118
Chapter 3 • The OpenPLC Editor
● 119
PLC Programming with the Raspberry Pi and the OpenPLC Project
Modbus was developed in 1979 by Modicon (now Schneider), the inventor of the PLC, to
connect sensors and actuators to the PLC via simple RS-232 and RS-485 networks. Later,
Modbus TCP was developed to allow connection with Ethernet. Modbus is an open standard
and today you'll find it implemented in almost every PLC. Thanks to the open protocol, it's
easy to build your own Modbus RTU and Modbus TCP modules.
Basically, the Modbus RTU works according to the Master-Slave principle. The master sends
a request to the slave and the slave returns the result. In Modbus TCP parlor, the relevant
terms are client and server. Here the client is the master, and the server is the slave.
Initially, you'll be using the Modbus in your projects to have access to the PLC program
being executed on the Raspberry Pi. Consequently, this works as a Modbus server.
In the next Chapter, you will be accessing external Modbus modules with the PLC program
in order to read in switches and measured values, as well as to switch consumers.
As already seen during the installation of the Runtime on Raspberry Pi, you have the pos-
sibility to release the Modbus server under . This setting is needed to access
the variables of your program.
● 120
Chapter 4 • OpenPLC and the Modbus
To do this, use the program "PLC-Example-KOP" and extend it with more contacts and an
LED. For this you load the project into the editor and save it under PLC-Example-LD-Mod-
bus.
Next, create a new ST file and extend the circuit as shown in the picture.
● 121
PLC Programming with the Raspberry Pi and the OpenPLC Project
The two variables "LED_Modbus" and "BTN_Modbus" are new. You need to switch the new
LED from the test program via the network. It's important that the IEC address "%QX2.0"
be used for the "BTN_Modbus" to be able to describe it. The value "%QX2.0" is used be-
cause it is outside the physical addresses of the output pins.
● 122
Chapter 4 • OpenPLC and the Modbus
After uploading the ST file to the Raspberry PI and starting it, you can proceed with the
test. For that, a ModbusTCP client is required to connect to the Raspberry PI ModbusTCP
server. There are several open source programs to do it. Download the "EasyModbusClient"
from the website (see link list) or from your download area (EasyModbusClientAndServer)
and unpack it to a directory of your choice. The client is a very helpful bit of software to
get to know the Modbus commands. For a visualization, the program AdvancedHMI — dis-
cussed at in a later chapter — is more suitable.
Then start the program, enter the IP address of your Raspberry PI, and establish the con-
nection with "connect".
This is a short overview of the most important Modbus commands for the test.
● 123
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now you can try to read in the buttons of your RaspiTestBoard. This is done with the com-
mand FC2 , using start address: 1 and number of bytes: 8.
You can see that all pins are set to "False". After pressing button 6 and button 8, the display
changes as follows after reading.
● 124
Chapter 4 • OpenPLC and the Modbus
As the next command, read-in the status of the LEDs. For this you use the command FC1
, again with the start address 1 and the number 8.
Switch the LED with button 1 on the board and read the status again.
You can see that the value of LED 1 has changed to "True".
Now let's try to send a value to your program in the Raspberry PI to simulate a button press
for the "BTN_Modbus".
● 125
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now send the value "True" to the address 17. With a double click in the field "Prepare Coils",
switch from "False" to "True" and press the arrow to the left. In the text box in the middle,
you should now see the value "True". At "Starting Address", enter 17 and then press the
button .
Afterwards, delete the value "True" in the text box using "clear all" and switch back to
"False" in the field "Prepare Coils" with a double click. Then press the arrow to the left en-
abling the value "False" to appear in the text box.
After sending the command with , the LED switches off again. Using FC1 it
is also possible to view the changed status.
As the last command, the PWM output is to be switched. This is done with the help of com-
mand FC6. For this enter the value 65535 in the field "Prepare Registers" and press the
arrow to the left again. That is the maximum value 2^16. After pressing ,
the PWM LED should shine at maximum brightness. After that, change the value to 0 and
send it again to turn off the LED.
● 126
Chapter 4 • OpenPLC and the Modbus
Now you already know the most important commands to control your program in the
Raspberry Pi from a PC. At the bottom of the page, you can see which bytes are sent and
received. A detailed description can be found in the Modbus documentation — see the web
links.
Tx: 00 AF 00 00 00 06 01 05 00 10 FF 00
Rx: 00 AF 00 00 00 06 01 05 00 10 FF 00
Tx: 00 B0 00 00 00 06 01 05 00 10 FF 00
Rx: 00 B0 00 00 00 06 01 05 00 10 FF 00
Tx: 00 B1 00 00 00 06 01 05 00 10 00 00
Rx: 00 B1 00 00 00 06 01 05 00 10 00 00
● 127
PLC Programming with the Raspberry Pi and the OpenPLC Project
Below are some important variables you can use for your programs. This gives you access
to the timing as well as the number of scan cycles.
%ML1024 The Unix epoch (or Unix time or POSIX time or Unix
timestamp) is the number of seconds that have elapsed
since January 1, 1970 (midnight UTC/GMT).
https://www.epochconverter.com/
Below is a small example of these variables. A new program will be created with the follow-
ing variables and function blocks:
After uploading to the Raspberry Pi as described in the previous examples, you can watch
the variables in the Raspberry PI Monitor.
● 128
Chapter 4 • OpenPLC and the Modbus
With this Modbus-TCP Master program from the download directory (ModbusTCP-Dot-
NET-Master.zip) you can observe the variables from the PLC program.
The next Chapter will take a look at the AdvancedHMI software, which allows a more com-
fortable way of switching the outputs and display the status of the inputs.
● 129
PLC Programming with the Raspberry Pi and the OpenPLC Project
Both AdvancedHMI and Visual Studio Community software can be downloaded for free.
First, download the software from the manufacturer's website and write a test program for
your Raspberry Pi PLC. To download it, go to the manufacturer's website:
https://www.advancedhmi.com
● 130
Chapter 4 • OpenPLC and the Modbus
After a free registration procedure, the program can be downloaded for free and unpacked
into a directory. Save the ZIP file as a template.
Next, create a folder "AdvancedHMI" and copy the content of the file "AdvancedHMI-
v399xR1.ZIP" into this directory.
● 131
PLC Programming with the Raspberry Pi and the OpenPLC Project
To open the project, a development environment is needed, so proceed to install the "Visual
Studio Community" from the Microsoft website:
https://visualstudio.microsoft.com/vs/community/
● 132
Chapter 4 • OpenPLC and the Modbus
With a right mouse click, let's open the project with Microsoft Visual Studio 2019.
With a double click on MainForm.vb, you will get the still vacant HMI view of your project
(WinForm). Now you can run the program by clicking on "Start".
● 133
PLC Programming with the Raspberry Pi and the OpenPLC Project
At this point, the project has been compiled and saved in this directory.
These project files are needed if you want to use a program on another computer. You can
also run these files on the Raspberry Pi with the Mono Framework.
● 134
Chapter 4 • OpenPLC and the Modbus
Now drag and drop the component "ModbusTCPCom" from the left toolbox onto your work-
sheet. In the properties window you can now enter the IP address of your Raspberry Pi.
● 135
PLC Programming with the Raspberry Pi and the OpenPLC Project
To test the communication, create a new PLC program with the Open PLC Editor, load it to
the Raspberry Pi, and start it.
● 136
Chapter 4 • OpenPLC and the Modbus
This is a simple on/off circuit using the "On" and "Off" buttons.
It gets interesting with the button "BTN_HMI", which is at address "%QX2.0". This address
— outside the address range of the GPIOs of the Raspberry Pi — is necessary for the HMI
interface to trigger a button press. With the timers "TON0" and "TOF0" you can build a
blinking-LED circuit for the "LEDBlink" program.
Now switch back to your HMI project and drag a switch (MomentaryButton) and three indi-
cator lights (PilotLight) from the toolbox, and label them as follows.
● 137
PLC Programming with the Raspberry Pi and the OpenPLC Project
To do this, select the object and change the text in the Properties window as shown in this
image.
BTN_HMI is at address 17, this is because you used %QX2.0 for the BTN_HMI (8+8+1).
LED goes to 1, LEDModbus to 2, and LEDBlink to 3.
The "MomentaryButton" can be changed from button to switch under the properties of
"OutputType".
With this feature set you have the correct addresses to have access to the variables of your
PLC-program. Now you can start your program and run a test.
The three LEDs always show the same state as the LEDs from the test board.
With the switch "BTN_HMI" it is possible to switch the LED 2 on the test board.
Here follows the Modbus address mapping for the Raspberry Pi with openplcproject and
AdvancedHMI.
● 138
Chapter 4 • OpenPLC and the Modbus
OpenPLC also provides a separate address space for memory variables with support for
16-, 32-, and 64-bit variables.
For the installation of the Mono Framework on the Raspberry PI the following commands
are needed:
sudo su
apt-get update
apt-get install mono-complete
apt-get install mono-vbnc
Now you can create a directory on the Raspberry PI and use "WinSCP" to copy your project
from the PC to the Raspberry PI and test it.
The example program can also be found in the download directory under "AdvancedHMI".
The EXE files for the examples are stored in the following subdirectory:
PLC-Book-Download\AdvancedHMI\AdvancedHMI\bin\Debug
● 139
PLC Programming with the Raspberry Pi and the OpenPLC Project
For this application it is necessary to read data from the running PLC program on the
Raspberry Pi. This is afforded by the Modbus TCP server installed on the Pi. The only thing
needed now is to read the data with a Modbus client and send it to the web server. For
this, a C program gets created (Codeblocks_C_HTTPPOST01) as well as a Python program
(pymodbus_openplc_post.py), which reads the output pins %Q0.0 to %Q0.7 via Modbus
query FC1. The status of the input pins %IX0.0 to %IX0.7 are read in with FC2. These data
are output by the two programs on the Raspberry Pi for control purposes.
● 140
Chapter 4 • OpenPLC and the Modbus
The C program was written with code blocks for the Raspberry Pi. If you want to adapt it
to your needs yourself, you should install the development environment. It is possible to
compile the source code on the command line, but it is easier with the IDE.
The Python program was created on the Raspberry PI with the existing development envi-
ronment called Geany. But it is also possible to run it on the PC.
To install Codeblocks, connect to the Raspberry PI using the VNC viewer and open a termi-
nal window. There, enter the following command:
After successful installation, the Code::Blocks program is found under Development in the
main menu.
Now create a subdirectory "Codeblocks" in the directory /pi. Copy the project directory
"Codeblocks_C_HTTPPOST01" into this folder with the aid of WinSCP.
After starting the IDE, you can open your project and edit it.
● 141
PLC Programming with the Raspberry Pi and the OpenPLC Project
Make the bold and italic changes for "directory" and "myserver" enabling the data to arrive
at the server.
Using the green arrow to the right, the program is compiled and started for a first test. After
that, save the project and exit Codeblocks.
Now launch the file manager and change to the Codeblocks directory. Next, find the exe-
cutable program under "Codeblocks_C_HTTPPOST01/ bin/debug". You can start it with a
double click. At the following query select "Run in terminal".
● 142
Chapter 4 • OpenPLC and the Modbus
Now you can view the test output of the program, at the same time the data is sent via
HTTP post to your web server. Currently, the Modbus functions FC1 and FC2 are used. The
program can be extended as you like.
On the web server, now install the two programs "index.html" and the PHP program
"sqlquery.php" into the web directory.
This PHP program either saves the data to a database or stores it as a file.
The HTML program also accesses this data via the PHP program or with http GET using
AJAX and displays it by the web browser.
Now you can access the web page and monitor the status of the lines.
● 143
PLC Programming with the Raspberry Pi and the OpenPLC Project
The reverse way, i.e., sending data from the web browser to the PLC program running on
the Raspberry PI, could also be implemented. For example, by clicking a button on the web
page, a Javascript function is called that sends the data to the PHP program. The C program
could fetch the data via HTTP post and send it to the PLC via the Modbus "FC" command.
A good overview is also provided by measurement devices on the website, as can be seen
in this example (MeterTest). Data sent by the PLC gets displayed in text form as well as
graphically.
● 144
Chapter 4 • OpenPLC and the Modbus
● 145
PLC Programming with the Raspberry Pi and the OpenPLC Project
In the Appendix, under web links, you can find the websites of the projects.
● 146
Chapter 5 • Modbus I/O modules
The Arduino Uno is a microcontroller board based on the ATmega328P. It has 14 digital in-
put/output pins — three of which are used here as PWM outputs, and six as analog inputs.
An external crystal provides the 16-MHz clock. The board has a USB connector and a power
supply socket, as well as socket headers for the I/O pins. It can be connected to a computer
via a USB cable and programmed using the Arduino IDE. The input and output pins work
with a supply voltage of 5 volts.
● 147
PLC Programming with the Raspberry Pi and the OpenPLC Project
The Modbus-RTU pin assignment for the PLC with the commands:
Regarding the installation of the Modbus Software on Arduino, first of all install the Arduino
IDE if it is not already installed. Next, download the Arduino UNO software and unpack it
into the directory "OpenPLC_Uno".
In the editor you can view the Modbus program for the Arduino.
● 148
Chapter 5 • Modbus I/O modules
Let's change the ID to 1, and then change two settings in the Arduino development envi-
ronment. As shown in the image, the board to be configured is the Arduino UNO.
The next step is to set the serial port (USB) to which your Arduino is connected. In this
case, it's COM6 (Arduino UNO).
● 149
PLC Programming with the Raspberry Pi and the OpenPLC Project
At this point the program can be uploaded by clicking on the arrow pointing to the right.
The upload of the software can be watched on the board by means of the two LEDs RX and
TX.
Now the Arduino board is ready for use and the first tests can be done on the PC. For this
task, start the "EasyModbusClient" and set the connection parameters as shown below,
then connect with "connect".
If the correct interface is set, the bar at the very bottom should show "Connected to Server"
and the color should change to green.
● 150
Chapter 5 • Modbus I/O modules
For clarity and a proper overview, the flowing picture of the Arduino board includes the PLC
addresses.
First of all, the outputs have to be switched. For this process, take a measuring device and
connect it to GND and PIN 7. After executing the command FC5, the voltage level should
change from 0 V to 5 V.
● 151
PLC Programming with the Raspberry Pi and the OpenPLC Project
To change the level at D8 (%QX100.1), enter 2 at "Starting Address". Using "Write Single
Coil", FC5 switches the line to 5 V as well.
You can see that the first two inputs are set to "True".
Now output an analog value (PWM) using FC6. The value range is from 0 to 65535, corre-
sponding to a 16-bit value (2^16).
Now a squarewave signal with a pulse-pause ratio of approx. 50% and a frequency of 480
Hz is generated at %QW100 (D9).
With the command FC16, it is possible to output three values at the same time.
● 152
Chapter 5 • Modbus I/O modules
Now the five digital input pins %IX100.0 (D2) to %IX100.4 (D6) are checked. Using a test
line, apply, in sequence, 5 V to one of the pins D2 to D6 and read the six values with FC2.
Finally, test the six inputs of the analog-to-digital converters %IW100 (A0) to %IW105
(A5) with FC4. For this test, connect a voltage of 1.5 V (AA battery) to GND and one of the
analog inputs each.
That completes your test for the moment. Now the Arduino UNO can be connected to the
Raspberry PI and used as a Modbus RTU module. With "lsusb" in a terminal window, it is
possible to check if the Arduino UNO is detected.
This shows "Device 005 Arduino SA Uno R3 (CDC ACM)" and you need to configure this to
the Raspberry Pi. Using the browser, switch to the web interface of the Raspberry PI and log
in. Then select the menu item "Slave Devices" and click on "Add new device".
● 153
PLC Programming with the Raspberry Pi and the OpenPLC Project
Make all settings as shown in the picture and confirm with "Save device". Now, the lines of
the Arduino UNO are available to the PLC program as variables. After "Start PLC" the RX
and TX LEDs should blink. This is the variable list for the test program:
● 154
Chapter 5 • Modbus I/O modules
Below you will find some circuit examples for the operation at 24 V supply.
Here's a circuit for a 24 V digital input with galvanic isolation. From a voltage of approx.
15 volts onwards, the transistor conducts hard and the ATmega328P detects a logic "True"
signal.
● 155
PLC Programming with the Raspberry Pi and the OpenPLC Project
And now a 24 V output for switching loads up to 1.5 A. The controller output is connected
to the gate of Q1 and switches the load at X2-1 and X2-2 from 24 V to GND.
In the next little circuit, low pass filter R4-C1 generates a direct voltage from the PWM
signal at the input, which is amplified by a factor of 2 by the OP LM7301. At the output of
the OP, 10 V is available.
From a 0 to 10 V input swing, the following circuit generates a voltage of 0 to 5 V for the
A/D converter of the microcontroller. The two diodes protect the input from the effects of
polarity reversal.
● 156
Chapter 5 • Modbus I/O modules
This next voltage divider with diodes for reverse-polarity protection allows you to measure
voltages in the 0 to 10 V range.
This circuit enables a multi-drop network to connect multiple Modbus slaves via a USB-
RS485 interface.
● 157
PLC Programming with the Raspberry Pi and the OpenPLC Project
For the operation as an RS-485 interface, the following program changes are necessary:
● 158
Chapter 5 • Modbus I/O modules
The NodeMCU module contains a powerful 32-bit RISC CPU from Tensilica which is clocked
at 80 MHz. Besides GPIOs and ADC, interfaces like UART, SPI, I2C and I2S are available.
Especially interesting is the WLAN interface according to IEEE 802.11 b/g/n WIFI with WEP
or WPA / WPA2 encryption. The modules have enough memory for interesting applications.
This module is the core for the I/O board, which is suitable for a 24 V supply voltage and
contains the following components:
This board is used as a Modbus TCP module via WLAN to read inputs and switch outputs
from the Raspberry Pi.
● 159
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 5.2.2: Circuit diagram of the ESP8266 I/O module for 24 V supply.
Figure 5.2.3: Example assembly with coupling relay from Finder for the top-hat rail.
● 160
Chapter 5 • Modbus I/O modules
● 161
PLC Programming with the Raspberry Pi and the OpenPLC Project
Let's use the ESP8266 I/O board as a Modbus TCP module, so it can be used as an extension
for the PLC programs of the Raspberry PI. First of all, you have to install the driver software
for the Arduino IDE. If not already installed, install the Arduino IDE. (www.arduino.cc).
After that, from the directory "ArduinoESP8266", download the Modbus project "ESP8266_
Modbus_BOARD1_1" and save it to your working directory.
(https://www.openplcproject.com/runtime/esp8266/)
With a double click on the Arduino program XXX in the directory "ESP8266_Modbus_
BOARD1_1" start the development environment.
In the editor, you can see the Modbus program for the Arduino. Now the "Boardmanager"
for the ESP8266 board is missing, this must be installed.
Under File Preferences enter this link in the line "Additional board administrator URLs:".:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
● 162
Chapter 5 • Modbus I/O modules
Then call the "Board manager" in the Tools menu and enter: ESP8266. Select the current
version and install it.
Next, select "Generic ESP8266 Module" by following Tools Board. After the installation,
you exit and restart the Arduino IDE.
● 163
PLC Programming with the Raspberry Pi and the OpenPLC Project
/*********NETWORK CONFIGURATION*********/
const char *ssid = "WiFi Name";
const char *password = "WiFi Password";
/***************************************/
Now set up the current Commport and you can translate and upload the program.
If the Commport driver is not installed automatically when the module is plugged in, it must
be installed manually. Mostly the USB-Serial converter devices CH340 are available on the
board. The driver can be downloaded from the manufacturer's website:
http://www.wch-ic.com/downloads/category/30.html.
● 164
Chapter 5 • Modbus I/O modules
Now you have all settings to send the program to the ESP8266 board.
First, open the Serial Monitor under Tools so that the IP address is displayed. The Modbus
program sends the IP address it received from the router at program start.
● 165
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 5.2.13: Program upload button for the ESP8266 in the DIE.
Here you can view the IP address of our ESP8266 module after uploading the software and
restarting the module.
Figure 5.2.14: Output of the IP address from the ESP8266 in the IDE.
With this done, the installation of the IDE and the upload of the Modbus software is fin-
ished. Now you can use the Modbus ESP8266 module for your Raspberry Pi PLC or for
AdvancedHMI.
In case the ESP8266 is to be used with the I/O board, the software "ESP8266_Modbus_
BOARD1_1.ino" must be employed. Unfortunately, we ran out of wires for the ESP8266, so
a multiplexer 74HC151 has to be installed to read the inputs. The modified software can be
downloaded from the project page.
● 166
Chapter 5 • Modbus I/O modules
In order to run the program on the PC, the Python development environment called "Idle"
must be installed, which allows changes to be made in the program. Get it here:
https://www.python.org/
After that, you have to install PIP to be able to install the Modbus libraries.
https://www.ephesossoftware.com/articles/programming/how-to-install-pip-for-python-
on-windows-mac-and-linux.html
For the Modbus interface you need the PyModbus library from the website:
# for Python 3
sudo pip3 install pyModbusTCP
# upgrade from an older release
sudo pip3 install pyModbusTCP --upgrade
https://pymodbustcp.readthedocs.io/en/latest/quickstart/index.html
This place also has the installation instructions and some simple examples for Python.
Figure 5.2.15: Output of the IP address from the ESP8266 in the IDE.
The program can also be run on the Raspberry Pi. A big advantage of this programming
language is that programs can run without change on different operating systems.
● 167
PLC Programming with the Raspberry Pi and the OpenPLC Project
There are enough applications that work in-house via WLAN. Therefore, here is an applica-
tion that allows you to access your ESP8266 I/O module from any computer in the world.
But before installing the software on your module, here's a brief description of its function.
The ESP8266 sends the status of its inputs — here the four 24 volt inputs and the 0 to 10
volt analog input — via your WLAN per http post request to a small PHP server program,
which is installed in a directory on your web host. The status of the output lines is also
transmitted.
● 168
Chapter 5 • Modbus I/O modules
This PHP program either saves the data to a database or stores it as a file.
The HTML program also accesses this data via the PHP program or using http GET with
AJAX and displays it on the web browser.
● 169
PLC Programming with the Raspberry Pi and the OpenPLC Project
The reverse way, i.e., sending data from the web browser to the ESP8266, is also feasible.
For this purpose, a Javascript function is called, for example, by clicking a button on the
web page, which causes the data to be sent to the PHP program.
● 170
Chapter 5 • Modbus I/O modules
These are also stored and can be queried by the home ESP8266 via HTTP post. In this direc-
tion you can switch the four outputs of the ESP8266 I/O module as well as send a voltage
value between 0 to 10 volts to the module.
Here is an example of a web page to control the module. Of course, the existing Arduino
software can be extended with own functions.
If not already done, install the Arduino IDE and the appropriate drivers for the I/O board as
already described in the previous Chapter.
Now you can load the ESP8266 software from the directory: "ESP8266_HTTP_POST" into
the Arduino IDE.
// Wlan Parameter
const char* ssid = "FRITZ!Box Fon WLAN 7170";
const char* password = "1234567890";
After that, change the access data for the web server.
● 171
PLC Programming with the Raspberry Pi and the OpenPLC Project
Now, the program gets transferred to the ESP8266 using the right-arrow.
After that, copy the files "index.html" and "sqlquery.php" with SFTP into your server direc-
tory. Now the ESP8266 module and the website can exchange data with each other.
For a good overview, gauges appear on the website as shown in this example.
● 172
Chapter 5 • Modbus I/O modules
● 173
PLC Programming with the Raspberry Pi and the OpenPLC Project
The example "gaugetest.html" shows how to integrate such an instrument. It can also be
found in the project directory under visualization.
● 174
Chapter 6.1 • Bibliography, circuit diagrams and layouts
[English]
PLC Controls with Structured Text (ST), V3: IEC 61131-3 and
best practice ST programming
Tom Mejer Antonsen (2020)
PLC Controls with Ladder Diagram (LD): IEC 61131-3 and introduction
to Ladder programming
Tom Mejer Antonsen (2021)
[German]
● 175
PLC Programming with the Raspberry Pi and the OpenPLC Project
Openplcproject
https://www.openplcproject.com/
https://openplcproject.gitlab.io/openplc_v3/md_README.html
Matiec Compiler
https://directory.fsf.org/wiki/Matiec
https://github.com/sm1820/matiec
PLCOPEN
https://www.plcopen.org/downloads
Beremiz Project
https://beremiz.org/
https://bitbucket.org/automforge/beremiz_git/wiki/Home
Raspberry PI
https://www.raspberrypi.org/
VNC Viewer
https://www.realvnc.com/de/connect/download/viewer/windows/
WinSCP
https://winscp.net/eng/download.php
History of Modbus
http://www.summaryplanet.com/summary/History-of-Modbus-protocol.html
● 176
Chapter 6 • Bibliography, circuit diagrams and layouts
AdvancedHMI
https://www.advancedhmi.com/index.php?main_page=page&id=14&chapter=0
MONO Project
https://www.mono-project.com/
Arduino
https://www.arduino.cc/
Chart Library
https://www.chartjs.org/samples/latest/
● 177
PLC Programming with the Raspberry Pi and the OpenPLC Project
Outputs
OFF
transmit
Tx: 00 08 00 00 00 06 01 05 00 00 00 00
receive
Rx: 00 08 00 00 00 06 01 05 00 00 00 00
OFF
Tx: 00 0A 00 00 00 06 01 05 00 01 00 00
receive
Rx: 00 0A 00 00 00 06 01 05 00 01 00 00
● 178
Chapter 6 • Bibliography, circuit diagrams and layouts
● 179
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 180
Chapter 6 • Bibliography, circuit diagrams and layouts
● 181
PLC Programming with the Raspberry Pi and the OpenPLC Project
Component side
Solder side
● 182
Chapter 6 • Bibliography, circuit diagrams and layouts
6.4.2 Circuit diagram for the test board with 24V input outputs:
● 183
PLC Programming with the Raspberry Pi and the OpenPLC Project
Component side
Solder side
● 184
Chapter 6 • Bibliography, circuit diagrams and layouts
6.4.3 Circuit diagram for the ESP8266 board with 24V input outputs:
● 185
PLC Programming with the Raspberry Pi and the OpenPLC Project
Component side
Solder side
● 186
Index
Index archive 32
ArduinoESP8266 162
Symbols Arduino HTTP 169
24 V output 156 Arduino IDE 147, 148, 162
%IW0, %IX0, %MD0, %ML0, %MW0, Arduino UNO 10, 147, 148
%QW0, %QX0.0, %QX2.0 139 Arduino UNO IDE settings 149
Arduino UNO IDE upload 150
A array 96, 103
ABS 87 AS 110
access code 14 ASIN 87
access data 164, 171 assembly language 53, 76
access structure variables 105 ATAN 87
ACOS 87 ATmega328P 147, 155
action 51, 110, 111, 112 audio 11
action block 114
actuators 120 B
ADC 159 barcode 93
A/D converter 124, 156 Barcode printing 94
AddCatalog 33 base type 88
Adding a contact 61, 64 Basic data types 82
Addition 87 BCD_TO_BIN () 88
Add new device 153 Beremiz 7
address 17, 126 BIN_TO_BCD () 88
address range 137 Bit AND 87
address space 139 Bit Exclusiv OR 87
AdvancedHMI 26, 123, 129, 130, 132, 166 Bit OR 87
AJAX 143, 169 bit pattern 96
Aktion 169 Bitwise operations 74
alarm contacts 168 BlinkLed 93, 94
alternative branching 119 BlinkTimer 91, 92
analog input 153, 159, 167 Boardmanager 162
analog-to-digital 153 branches 55
analog value 152 breadboard 39
AND 67, 80, 83, 87 BTN_HMI 138
Android 15 BTN_Modbus 122, 125
ANY_TO_ 88 button 92, 124
ANY_TO_BOOL 88 button press 110, 125, 137
ANY_TO_DINT 88
ANY_TO_INT () 88 C
ANY_TO_REAL () 88 CASE 86
ANY_TO_SINT () 88 CDC ACM 153
ANY_TO_STRING () 88 CH340 164
ANY_TO_TIME () 88 chart 55, 109, 146, 174
ANY_TO_UDINT () 88 C_HTTP 141
ANY_TO_UINT () 88 Circuit diagram for the ESP8266 board 185
ANY_TO_USINT () 88 Class 58, 69
● 187
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 188
Index
● 189
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 190
Index
● 191
PLC Programming with the Raspberry Pi and the OpenPLC Project
● 192
Index
● 193
PLC Programming with the Raspberry Pi and the OpenPLC Project
VNC Server 15
VNC viewer 15, 22, 141
VNC Viewer credentials 17
VNC Viewer installation 15
VNC Viewer start menu 16
voltage divider 42, 157, 159
W
web browser 24, 140, 143, 144, 169, 170
web host 168
web interface 24, 26, 38, 41, 79, 153, 168
web page 170, 171
web server 24, 38, 140, 143, 171
Web server application 168
website 167, 172
WEP 159
WHILE 84, 90
WIFI 159
WinSCP 18, 20, 21, 22, 139, 141
WiringPI library 30
WLAN 14, 120, 158, 159, 168
working directory 162
worksheet 60, 69, 77, 111
world 140
Write 13
Write Multiple Coils 179
Write Single Coil 178
Write Single Register 180
X
XOR 80, 87, 92
Z
ZIP 131
● 194
books
books books
>
PLC Programming
with the Raspberry Pi
PLC Programming
PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt
and the OpenPLC project with the Raspberry Pi
ModbusRTU and ModbusTCP examples
with the Arduino Uno and ESP8266
and the OpenPLC project
Introduction to PLC programming with OpenPLC, the first fully
Josef Bernhardt became
ModbusRTU and ModbusTCP examples
open source Programmable Logic Controller on the Raspberry
Pi, and Modbus examples with Arduino Uno and ESP8286
interested in electronics
at a very young age, when
with the Arduino Uno and ESP8266
he built his first detector
PLC programming is very common in industry and home automation. receiver, followed over
the years by many other
This book describes how the Raspberry PI 4 can be used as a Program-
circuits.
mable Logic Controller. Before taking you into the programming, the
author starts with the software installation on the Raspberry PI and He gained his first
the PLC editor on the PC, followed by a description of the hardware. programming experience
in the 1980s with the
Commodore VC20.
You'll then find interesting examp les in the different programming
He became familiar with
languages complying with the IEC 61131-3 standard. assembler programming
This manual also explains in detail how to use the PLC editor and on the 8088 processor.
how to load and execute the programs on the Raspberry PI.
All IEC languages are explained with examples, starting with LD Josef can look back on more
than 30 years of electronics
(Ladder Diagram) over ST (Structured Control Language) to SFC
and software development
(Special Function Chart). All examples can be downloaded from the at UNI Regensburg.
author's website.
With his own SMD
Networking gets thorough attention too.The Arduino UNO and the production, he also
implements electronic
ESP8266 are programmed as ModbusRTU or ModbusTCP modules
customer projects, always
to get access to external peripherals, reading sensors and switching driven by the pleasure
electrical loads. I/O circuits complying with the 24V industry standard of electronics and
may also be of interest for the reader. programming.
Josef Bernhardt