100% found this document useful (1 vote)
371 views

PLC Programming with the Raspberry Pi and the OpenPLC Project

The document is a book titled 'PLC Programming with the Raspberry Pi and the OpenPLC Project' by Josef Bernhardt, which serves as a practical guide to using the Raspberry Pi as a Programmable Logic Controller (PLC). It covers software installation, programming in various IEC 61131-3 languages, and includes examples with ModbusRTU and ModbusTCP using Arduino and ESP8266. The book aims to enable readers to create their own controllers and is suitable for both beginners and experienced users in electronics and programming.

Uploaded by

yeutruonghvtc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
371 views

PLC Programming with the Raspberry Pi and the OpenPLC Project

The document is a book titled 'PLC Programming with the Raspberry Pi and the OpenPLC Project' by Josef Bernhardt, which serves as a practical guide to using the Raspberry Pi as a Programmable Logic Controller (PLC). It covers software installation, programming in various IEC 61131-3 languages, and includes examples with ModbusRTU and ModbusTCP using Arduino and ESP8266. The book aims to enable readers to create their own controllers and is suitable for both beginners and experienced users in electronics and programming.

Uploaded by

yeutruonghvtc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 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.

The book ends with an overview of commands for ST and LD.


After reading the book, the reader will be able to create his own
controllers with the Raspberry Pi. Elektor International Media
www.elektor.com

Josef Bernhardt

Cover PLC ENGLISH Josef Bernhardt.indd Alle pagina's 29-10-2021 10:15


PLC Programming
with the Raspberry Pi
and the OpenPLC Project
ModbusRTU and ModbusTCP examples using the Arduino Uno and the ESP8266

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.

● British Library Cataloguing in Publication Data


A catalogue record for this book is available from the British Library

● ISBN 978-3-89576-469-1 Print


ISBN 978-3-89576-470-7 eBook

● © Copyright 2021: Elektor International Media B.V.


Editor: Denis Meyer
Prepress Production: D-Vision, Julian van den Berg

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

Chapter 1 • Installing the Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.1 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2 Installing the operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.3 Installing the VNC Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.4 Installing the File Transfer Software WinSCP . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.5 Installing the openplcproject runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Chapter 2 • OpenPLC Editor Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.1 Download and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.2 Raspberry Pi pin descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

2.3 Additional Hardware I/O Test Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

2.4 Additional 24 V PLC Board hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Chapter 3 • The OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.1 Description of the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.2 Ladder Logic Example (LD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

3.3 Function Block example (FBD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

3.4 Instruction List example (IL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

3.5 Structured Text examples (ST, SCL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

3.5.1 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

3.5.2 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

3.5.3 Conversion operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

3.5.4 Standard function blocks according to IEC 61131-3 . . . . . . . . . . . . . . . . . . 88

3.5.5 First ST program example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

3.5.6 ST example for controlling a conveyor belt . . . . . . . . . . . . . . . . . . . . . . . . 93

3.5.7 Defining arrays with the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . 96

3.5.8 Defining structures with the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . 100

3.5.9 Combining structures with arrays using the OpenPLC Editor . . . . . . . . . . . 103

3.5.10 Definition of ENUMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

3.6 Sequential Function Chart example (SFC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

Chapter 4 • OpenPLC and the Modbus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

4.1 Testing PLC programs with Modbus TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

●5
PLC Programming with the Raspberry Pi and the OpenPLC Project

4.2 Visualization of PLC programs with AdvancedHMI . . . . . . . . . . . . . . . . . . . . . . . 130

4.3 Visualization of PLC programs via the Internet . . . . . . . . . . . . . . . . . . . . . . . . . 140

Chapter 5 • Modbus I/O modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

5.1 Modbus RTU module with the Arduino UNO . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

5.2 Modbus TCP module with the ESP8266 and WLAN . . . . . . . . . . . . . . . . . . . . . . 158

5.3 Web server application with the ESP8266 I/O module . . . . . . . . . . . . . . . . . . . . 168

Chapter 6.1 • Bibliography, circuit diagrams and layouts . . . . . . . . . . . . . . . . . . 175

6.2 Web links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

6.3 Modbus commands for the ESP8266 I/O module . . . . . . . . . . . . . . . . . . . . . . . 178

6.4 Circuit diagrams and layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

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.

In a later chapter, the visualization with AdvancedHMI is discussed to display processes on


the screen.

Circuits with the Arduino and ESP8266, which are necessary for Modbus, are also explained.

I wish you lots of success in reading and using the book.

Bad Abbach, May 2021


Josef Bernhardt

●7
PLC Programming with the Raspberry Pi and the OpenPLC Project

Introduction

Programmable logic controllers (PLCs) have revolutionized industrial control technology.


PLCs have been used primarily in industrial control systems and home automation since
their invention by Richard E. Morley about 50 years ago.

Here is the definition of "PLC" according to EN61131-3:

"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."

The Raspberry Pi is perfectly suitable for an application as a PLC because of its


architecture with the GPIO connector, as well as its low price.

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.

Another advantage of PLC programming is the inter-compatibility of PLC systems. Programs


for a PLC from manufacturer "A" can generally be used for PLCs from manufacturer "B"
without much effort.

Figure 0.1: Raspberry Pi PLC Unipi 1.1.

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.

Figure 0.2: Overview OpenPLC.

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.

The following PLC languages are supported:

Programming language Abbreviation

Ladder Logic LD

Function Block FBD

Instruction List IL

Structured Text ST

Sequential Function Chart SFC

●9
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 0.3: IEC programming languages.

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

Chapter 1 • Installing the Raspberry Pi 4

1.1 Hardware description


The Raspberry Pi is now a well-known minicomputer, which, thanks of its low price, is wide-
ly used by hobbyists and industrial companies alike.

Figure 1.1: 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.

Figure 1.2: Raspberry Pi Zero W.

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

1.2 Installing the operating system


Download the Raspberry Pi Imager here. This is a simple and quick way to install the oper-
ating system on a Micro SD card. There is also a video about this on YouTube. Link:

https://www.youtube.com/watch?v=J024soVgEeM

After downloading, change to this directory and start the imager.

Link: https://downloads.raspberrypi.org/imager/imager_1.5.exe

Figure 1.3: Raspberry Pi Imager launched.

After clicking on "Install", the installation of the Imager program will commence. The Im-
ager is used to install our Micro SD card.

Figure 1.4: Raspberry Pi Imager Setup.

● 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:

Figure 1.5: Raspberry Pi Imager OS selection.

Click the "Write" button to launch the installation.

Figure 1.6: Raspberry Pi Imager startup.

Confirm the security prompt with "YES".

● 13
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 1.7: Writing to the SD card using the Raspberry Pi Imager.

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

1.3 Installing the VNC Viewer


VNC is a graphical desktop sharing system allowing you to remotely control the desktop
interface of a computer, in this case the Raspberry Pi (VNC Server) from another computer
or mobile device using VNC Viewer (Client). VNC Viewer transmits keyboard, mouse, or
touch events to VNC Server and in return receives information for screen updates. This can
be downloaded and installed on a PC for now.

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.

After downloading, start the installation.

Figure 1.8: VNC Viewer installation.

● 15
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 1.9: VNC Viewer installation.

Continue the installation by clicking on "Next". Click on "Install". This will launch the instal-
lation of the Viewer.

Start VNC Viewer and connect to the Raspberry Pi.

Figure 1.10: VNC Viewer start menu.

Under "File", and then "New Connection", enter the access data from your Raspberry Pi.

● 16
Chapter 1 • Installing the Raspberry Pi 4

Figure 1.11 VNC Viewer configuration.

Enter the IP address and a suitable name. Afterwards, confirm the "Properties", "General"
window with "OK".

Figure 1.12: VNC Viewer credentials

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

Figure 1.13 Raspberry Pi OS Desktop.

1.4 Installing the File Transfer Software WinSCP


WinSCP is an open-source SFTP and FTP client for Windows, needed to transfer files be-
tween a PC and Raspberry Pi.

To get access, enable SSH on the Raspberry Pi under Settings  Raspberry Pi Configuration
Interfaces.

Figure 1.14: SSH configuration.

Firstly, download and save the installer.

● 18
Chapter 1 • Installing the Raspberry Pi 4

Figure 1.15: WinSCP Download.

Now change to the download directory and start the installation.

Figure 1.16: WinSCP installation

Accept the license agreement by clicking "Accept".

● 19
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 1.17: WinSCP installation.

Confirm with "Next".

Figure 1.18: WinSCP installation.

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.

Figure 1.19: WinSCP credentials configuration.

Log in and save the connection destination.

Figure 1.20: WinSCP configuration of directories.

You can now access the file directories of the Raspberry Pi.

● 21
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 1.21: WinSCP in action

1.5 Installing the openplcproject runtime


With your PLC computer set up so far, the next important step is to install the Open PLC
Runtime from the Open PLC Project website.

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:

sudo apt-get install git

and confirm with "Enter".

● 22
Chapter 1 • Installing the Raspberry Pi 4

Figure 1.22: Install GIT.

For the installation of the OpenPLC Runtime (PLC operating system) you need the following
commands:

git clone https://github.com/thiagoralves/OpenPLC_v3.git


cd OpenPLC_v3
./install.sh rpi

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

Figure 1.23: Installing OpenPLC Runtime.

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.

Figure 1.24: OpenPLC Runtime web interface.

● 24
Chapter 1 • Installing the Raspberry Pi 4

The default username and password are both: "openplc". These credentials can be changed
after login.

Figure 1.25: OpenPLC Runtime Dashboard.

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.

Figure 1.26: OpenPLC runtime hardware selection.

● 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".

Figure 1.27: OpenPLC Runtime settings.

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

Figure 1.28: OpenPLC PLC program upload.

For this, click on "Browse" for the file dialog, select the ST file from your PLC project, and
confirm with "Open".

Figure 1.29: Select your OpenPLC ST file.

● 27
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 1.30: The OpenPLC PLC ST-Program upload process.

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.

Figure 1.31: Compiling ST program into C.

● 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.

Figure 1.32: Dashboard Runtime Los.

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".

Figure 1.33: Connecting the LEDs

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

Figure 1.34: Install WiringPI library.

After that, the installation can be tested. The version 2.52 should be displayed.

gpio –v

It should display

gpio version: 2.52


Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

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

Figure 1.35: Logout from the OpenPLC PLC program.

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

Chapter 2 • OpenPLC Editor Installation

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.

2.1 Download and Installation


Go to this web place: https://www.openplcproject.com/plcopen-editor/ to download the
editor for Windows. A version for Linux is also available on the website.

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.

Figure 2.1: OpenPLC Editor directory.

Figure 2.2: OpenPLC Editor start screen.

● 32
Chapter 2 • OpenPLC Editor Installation

Now the start screen appears with an empty project.

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:

from locale = wx.Locale(wx.LANGUAGE_DEFAULT)

into locale = wx.Locale(wx.LANGUAGE_ENGLISH)

A list of possible variables can be found here:

https://wxpython.org/Phoenix/docs/html/wx.Language.enumeration.html

This information can be found on the following website:

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.

You can find the "HelloWorld" project on the website at:

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

Figure 2.3: OpenPLC Editor with 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

Figure 2.4: OpenPLC Editor with LD project.

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 .

Figure 2.5: Create an OpenPLC Editor ST file.

● 35
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 2.6: OpenPLC Editor ST project save function.

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.

Start build in C:\Users\info\PLC-Examples\Hello World\build


Generating SoftPLC IEC-61131 ST/IL/SFC code...
Compiling IEC Program into C code...
Extracting Located Variables...
C code generated successfully.
PLC :
[CC] plc_main.c -> plc_main.o
[CC] plc_debugger.c -> plc_debugger.o
py_ext :
[CC] py_ext.c -> py_ext.o
PLC :
[CC] Config0.c -> Config0.o
[CC] Res0.c -> Res0.o
Linking :
[CC] plc_main.o plc_debugger.o py_ext.o Config0.o Res0.o -> Hello World.dll
Successfully built.
OpenPLC program generated successfully

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

TOF0(IN := my_button, PT := T#5000ms);


lamp := TOF0.Q;
END_PROGRAM

CONFIGURATION Config0

RESOURCE Res0 ON PLC


TASK task0(INTERVAL := T#20ms,PRIORITY := 0);
PROGRAM instance0 WITH task0 : Hello_World;
END_RESOURCE
END_CONFIGURATION

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.

2.2 Raspberry Pi pin descriptions


Raspberry Pi GPIO header with the corresponding PLC pins.

● 37
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 2.7: Pinout of the RPi's GPIO connector.

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".

Figure 2.8: OpenPLC web interface of the Raspberry Pi

● 38
Chapter 2 • OpenPLC Editor Installation

Under "Programs", click the "Browse" button, then select the ST file you created and saved
with the Editor.

Figure 2.9 Uploading an ST file.

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:

Compiling main program...


Compilation finished successfully!

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

2.3 Additional Hardware I/O Test Board

Figure 2.10: Circuit diagram of pushbuttons and LEDs.

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.

Figure 2.11: Raspberry Pi Test Board.

● 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:

Figure 2.12: Monitoring program.

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.

2.4 Additional 24 V PLC Board hardware


For the practical use of the Raspberry Pi as a PLC there is also a small board designed for
compatibility with industrial controllers. The small board has four 24 V inputs with galvan-
ic isolation provided by optocouplers. The four outputs with the HITFET type BSP76 can
switch around. 1.5 A. An additional PWM output is also available to control a DC motor, for
example.

● 41
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 2.13 Circuit diagram of the 24 V I/O board.

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.

Figure 2.14: Internal circuit of the HITFET type BSP76.

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

Figure 2.15: Layout of the 24 V I/O board.

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

Chapter 3 • The OpenPLC Editor

3.1 Description of the OpenPLC Editor


This Chapter discusses the OpenPLC Editor in more detail. You use it to write your own
programs for the Raspberry PI. A description of the title bar menus and the possible pro-
gramming languages follows.

Figure 3.1: OpenPLC Editor start screen.

After launching the program, the main menu appears, where a new project can be created.

That's done with the big button.

Here is a structure tree with an example which uses function blocks from different IEC
languages.

● 44
Chapter 3 • The OpenPLC Editor

Figure 3.2: Project structure.

Figure 3.3: File menu of 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

Figure 3.4: Edit menu of the OpenPLC Editor.

The Edit menu is for copying and pasting objects as well as for searching objects.

Figure 3.5: Project properties.

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

Figure 3.6: Display menu of the OpenPLC Editor.

The "View" menu to switch the view to full screen and back again.

Figure 3.7: Help menu of the OpenPLC Editor.

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.

Figure 3.8: Library menu of the OpenPLC Editor.

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

Figure 3.9: Function blocks selection.

This window for the function blocks selection menu appears when you right-click on the
current worksheet.

Figure 3.10: Debug menu of the OpenPLC Editor.

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

Figure 3.11: Task menu of 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.

The menu bar from an empty project:

Figure 3.12: OpenPLC Editor Menu bar.

● 49
PLC Programming with the Raspberry Pi and the OpenPLC Project

Symbol Name Key Function

New Project Create a new project

Open Project Open an existing project

Save Project Save a current project

Save As Save the current project under another name

Print Print the current program

Undo Undo the last change in the editor

Redo Restore change in the editor

Cut Cutting the selected objects in the editor

Copy Copying an object

Pasting objects from the clipboard into the


Paste
editor

Search Opening the data search dialog in the project

Simulate Simulates the current program

Create Program Translates the program into an ST file

The menu bar in a LD project (ladder diagram):

Figure 3.13: Menu bar with LD program.

Here, the right side shows the elements such as busbar, coil, and contact.

The menu bar in an ST project (structured text):

Figure 3.14: Menu bar with ST program.

● 50
Chapter 3 • The OpenPLC Editor

The menu bar in an FBD project (function block language):

Figure 3.15: Menu bar with an FBD program.

The menu bar in an SFC project (sequential function chart):

Figure 3.16: Menu bar with an SFC program.

Here you see on the right side the elements like Step, Transition, Action, Branch, Jump.

The menu bar in an IL project (instruction list):

Figure 3.17: Menu bar with an IL program.

Overview of PLC programming languages according to IEC 61131-3:

Symbol Short Full Name

LD Ladder Diagram

FBD Function Block

IL Instruction List

ST Structured Text

SFC Sequential Function Chart

● 51
PLC Programming with the Raspberry Pi and the OpenPLC Project

Ladder diagram (LD)

Figure 3.18: Ladder Diagram program example.

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.

Function block diagram (FBD)

Figure 3.19: Function block diagram program example.

● 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.

Instruction list (IL)

Figure 3.20: IL program example.

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

Structured control language (ST, SCL)

Figure 3.21: ST Sample program.

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

Sequential function chart (SFC)

Figure 3.22: SFC program example.

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.

3.2 Ladder Logic Example (LD)


Programming with Ladder Logic allows the programmer to work with standardized graphi-
cal symbols. The symbols are arranged in networks similar to current paths of relay sche-
matics. The networks are bounded on the left and right by the power supply rails.

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

Figure 3.2.1: Circuit diagram for our program.

Figure 3.2.2: Representation as a PLC ladder diagram (LD).

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

Figure 3.2.3: Create a new project.

Using "File à New", you create a new project, let's call it "PLC-Example-LD".

Figure 3.2.4: Select project directory

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

Figure 3.2.5: Define POU block

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 project name now appears in the structure tree.

Figure 3.2.6: Blank worksheet.

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

Figure 3.2.7: Base data types.

Figure 3.2.8: Declaring variables.

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.

Figure 3.2.9: Adding a function block.

● 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.

Figure 3.2.10: Adding a symbol function block.

The following icons are available in the ladder diagram:

Power Rail left and right

New coil

New contact

New variable

New function block

New connection

Figure 3.2.11: Adding the left power rail.

Here, confirm with OK. The symbol now appears on the worksheet.

● 60
Chapter 3 • The OpenPLC Editor

Figure 3.2.12: Power rail, left section.

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.

Figure 3.2.13: Adding a contact.

● 61
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 3.2.14: Adding a contact.

Now you can put the switch on your worksheet next to the left-hand power line.

Figure 3.2.15: Add a negated contact.

For the opener "Off", select the modifier "Negated" and from the combo box "Variable", the
value "Off".

Next, you can put a coil on the worksheet.

● 62
Chapter 3 • The OpenPLC Editor

Figure 3.2.16: Adding a coil.

And confirm it by clicking "OK".

Figure 3.2.17: Adding a power rail on the right side.

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

Figure 3.2.18: Adding the right-hand power rail.

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.

Figure 3.2.19: Adding a contact.

● 64
Chapter 3 • The OpenPLC Editor

Figure 3.2.20: Adding the "LED" contact .


.

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.

Figure 3.2.21: Connecting components.

● 65
PLC Programming with the Raspberry Pi and the OpenPLC Project

In between you should save the project with this symbol.

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.

Figure 3.2.22: Saving the project.

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".

Figure 3.2.23: ST file save,

● 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.

Figure 3.2.24: ST file listing.

3.3 Function Block example (FBD)


Programming with FBD allows the programmer to work with standardized graphical sym-
bols similar to LD.

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

Figure 3.3.1: New project.

With "File  New" you create a new project, let's call it PLC-Example-FBD.

Figure 3.3.2: Creating new project.

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

Figure 3.3.3: Inserting the project name.

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.

Figure 3.3.4 Entering the variable.

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

Figure 3.3.5: Block choice.

After that, put the variables for "On", "Off" and "Led" on your worksheet and position them
in the right place.

Figure 3.3.6: Editing the input.

● 70
Chapter 3 • The OpenPLC Editor

Figure 3.3.7: Editing the output.

Figure 3.3.8: Inserting modules.

After that, you can create the connections between the blocks.

● 71
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 3.3.9 Connecting modules.

The following icons are available in the ladder diagram:

Comment

New variable

New function block

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

Figure 3.3.10: Saving the ST file.

Here are the task settings for the program. You can access them by double-clicking on Res0
in the structure tree on the left.

Figure 3.3.11 Editing the task settings.

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

Figure 3.3.12: Project directory.

Figure 3.3.13: ST program.

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

Figure 3.3.14: Advanced program.

Here is your result as an ST file with the extended functions.

Figure 3.3.15: ST Listing

● 75
PLC Programming with the Raspberry Pi and the OpenPLC Project

For more info, the website

https://de.wikibooks.org/wiki/Digitale_Schaltungstechnik/_Flipflop/_Einleitung

is recommended, where the basic digital circuits are explained in detail.

3.4 Instruction List example (IL)


The programming language Instruction List (IL) is one of the oldest languages for PLC
programming, dating back to the '70s. It is text-based and largely resembles the assem-
bly language of microcontrollers. The commands are processed in the order in which they
appear in the program. As with the other languages, programs in IL may also contain func-
tions and function blocks.

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.

Figure 3.4.1: Creating a new project.

Here you declare your variables again.

● 76
Chapter 3 • The OpenPLC Editor

Figure 3.4.2: Declaring a variable.

Now you can enter your program on the worksheet.

Figure 3.4.3: IL program example

● 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.

Figure 3.4.4: Declaring a variable.

Here is the example program:

Figure 3.4.5: Entering the program.

(*
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

ldn Pulse3.q (* test Puls3 *)


st Pulse1.in
cal Pulse1
ldn Pulse1.q (* test Puls1 *)
st Pulse2.in
cal Pulse2
ldn Pulse2.q (* test Puls2 *)
st Pulse3.in
cal Pulse3
ld Pulse1.q
st LED1
ld Pulse2.q
st LED2
ld Pulse3.q
st LED3
st LED3

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".

Figure 3.4.6: Monitoring program in action.

● 79
PLC Programming with the Raspberry Pi and the OpenPLC Project

Here are the IL commands from the example:

Command Description

LD Load value into ACCU

LDN Load negated value into accumulator

R Reset value

S Set value

AND And logic operation

ANDN And logic operation negated

JMP Jump to label

JSR Jump to subroutine

RET Return from subroutine

CAL Call function block

Here is an extended IL command overview:

LD Loads the value of the operand into the accumulator


LDN Load the negated value of the operand into the accumulator
ST Assign the value from the accumulator to the operand
STN Assign the inverted value of the accumulator to the operand
S If the accumulator value is TRUE, the operand is set.
R If the accumulator value is FALSE, the operand is reset.
AND Bitwise AND of the accumulator and the operand, the result will be in the accumulator
Bitwise AND of the accumulator and the negated operand, the result will be in the
ANDN
accumulator
OR Bitwise OR of accumulator and operand, the result will be in the accumulator
Bitwise OR of accumulator and the negated operand, the result will be in the
ORN
accumulator
Bitwise exclusive OR of the accumulator and the operand, the result will be in the
XOR
accumulator
Bitwise exclusive OR of the accumulator and the negated operand, the result will be in
XORN
the accumulator
NOT Accumulator bit inversion, the result will be in the accumulator
ADD Adding accumulator and operand, the result is loaded into the accumulator
SUB Subtract the operand from the accumulator, the result is loaded into the accumulator
Multiplication of the accumulator with the operand, the result is loaded into the
MUL
accumulator
DIV Division of the accumulator by the operand, the result is loaded into the accumulator

● 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.

A complete list of IL commands and examples can be found in the document


IEC 61131-3, 2nd Ed. PROGRAMMABLE CONTROLLERS - PROGRAMMING LANGUAGES

On pages 134–140.

As well as on the Matiec Compiler website, here:

http://mat.sourceforge.net/manual/logic/il.html

3.5 Structured Text examples (ST, SCL)


The programming language called Structured Text (ST, SCL) is similar to the programming
language Pascal by the Swiss computer scientist Niklaus Wirth. It's a modern text-based
language, compliant with IEC61131-3 and able to create clearly structured programs. The
language is particularly well suited for applications in which mathematical calculations and
data management occur. Here is an overview of the ST programming language:

POU (Program Organization Unit)


Programs, functions (subprograms) and function blocks (FB) are defined as program pro-
cessing units (POUs). Each POU consists of a declaration section and a program section.
This is written in one of the IEC programming languages.

● 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 may not begin with a numerical (0-9).


• Variable names may not contain spaces.
• The underscore ( _ ) is a valid character, but the variable name must not con-
tain consecutive underscores.
• Variable names must not be identical with a keyword (example: FOR, AND, DO,
CASE etc.).

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.

IEC 61131-3 Basic data types

Data type Designation Value range Example

Boolean BOOL FALSE (0), TRUE (1) FALSE

Short Integer SINT –128 … 127 2

Integer INT –32768 … 32767 –432

Double Integer DINT –2147483648 … 2147483647 –38099887

Unsigned Short Integer USINT 0 … 255 200

Unsigned Integer UINT 0 … 65535 43244

Double Integer UDINT 0 … 4294967295 12534256

Byte (USINT) BYTE 0 … 255 53

Word (UINT) WORD 0 … 65535 16#2102

Double Word (UDINT) DWORD 0 … 4294967295 2294967295

Real REAL –1.2 × 10-38 … 3.4 × 1038 1.34996

Time TIME 0 ms … 1193h2m47s295ms T#8h12m8s125ms

String STRING 1 … 255 characters `Emergency Stop`

Statements, expressions and their evaluation


The assignment operator is used to assign a value to a variable. For example:

<Variable>: = < Value >;

● 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.

Example: (* This is a comment *)

Sometimes comments are also used to temporarily disable a code block.

Tabs and spaces


Tabs and spaces can be used to enable better readability of the written code by indentation.
They are ignored by the compiler and have no effect on the program.

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.

Example: Q := (IN1 + (IN2 / IN 3)) * IN4;

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

3.5.2 Control structures


FOR loop
The instructions between DO and END_FOR are repeated and executed with increment
or decrement steps from the start value to the end value of the defined variable. The BY
<step> instruction can be omitted. The default value is 1, even if no step expression is
specified.

● 83
PLC Programming with the Raspberry Pi and the OpenPLC Project

Syntax FOR <Variable> := <Start> TO <End> BY <Step> DO


<Commands>
END_FOR;
// Variable = Variable of type DINT is used as index
// Start = DINT expression: The start value for the index variable.
// End = DINT expression: maximum valid value for the index.
// Step = DINT expression: This is the automatic increase of the index after each
repetition. (Default is 1.)

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.

Syntax WHILE < Logic condition > DO


< Commands >
END_WHILE;

Example WHILE ( Wert > 0 ) DO


Value := Value * 2;
Wertr := Wert - 1;
END_WHILE;

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.

Syntax IF < Condition1> THEN


<IF_Commands>
ELSIF < Condition 2> THEN
<ELSIF_Commands 1>
ELSIF < Condition n> THEN
<ELSIF_ Condition n-1>
ELSE
<ELSE_Commands>
END_IF;

Example IF Temperature < 65 THEN


Relay1On := TRUE;
Relay2On := FALSE;
ELSIF Temperature >= 65 AND Temperature <= 75 THEN
Relay1On := FALSE;
Relay2On := FALSE;
ELSE
Relay1On := FALSE;
Relay2On := TRUE;
END_IF;

● 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.

Syntax CASE <DINT Variable or expression > OF


< Value 1>:
< Command 1>
< Value 2>:
< Command 2>
< Value 3, Value 4, Value 5>:
< Command 3>
< Value 6 .. Value 10>:
< Command 4>
...
< Value n>:
< Command n>
ELSE
<ELSE Command n>
END_CASE;

Example CASE Value OF


1:
iMaterial1Kg := 5;
iMaterial2Kg := 10;
2,3:
iMaterial1Kg := 10;
iMaterial2Kg := 0;
4..6:
iMaterial1Kg := 0;
iMaterial2Kg := 1;
7,9..11:
iMaterial1Kg := 6;
iMaterial2Kg := 10;
8:
iMaterial1Kg := 10;
iMaterial2Kg := 10;
ELSE
iMaterial1Kg := 0;
iMaterial2Kg := 0;
END_CASE;

● 86
Chapter 3 • The OpenPLC Editor

Common operators (ST format)

Symbol Example Description

AND X := A AND B; Bit AND

OR X := A OR B; Bit OR

XOR X := A XOR B; Bit Exclusiv OR

NOT X := NOT(A); NOT inverted

+ X := A + B; Addition

- X := A - B; Substraction

* X := A * B; Multiplication

/ X := A / B; Division

MOD X := 12 MOD 8; Modulo (Remainder)

> IF (A > B) THEN.. Greater than

>= IF (A >= B) THEN.. Greater than equal to

= IF (A = B) THEN.. Equal

<> IF (A <> B) THEN.. Not equal

<= IF (A < B) THEN.. Less equal

< IF (A <= B) THEN.. Small

SHL X := SHL(A, B); Shifts the content of A by B bits to the left

SHR X := SHR(A, B); Shifts the content of A by B bits to the right

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

TRUNC X:= TRUNC(A); The integer part of a variable of type Real

ABS X := ABS(A); Absolute value of A

SQRT X := SQRT(A); Square root of A

LN X := LN(A); Natural logarithm of A with base e

LOG X := LOG(A); Base 10 logarithm of A

EXP X := EXP(A); Exponential function

EXPT X := EXPT(A,B); Exponentiation of a variable;


POW (A, B) or A ** B instructions are equal.

SIN X := SIN(A); sine of A

COS X := COS(A); cosine of A

TAN X := TAN(A); tangent of A

ASIN X := ASIN(A); arcsine of A

ACOS X := ACOS(A); arccosine of A

ATAN X := ATAN(A); arctangent of A

● 87
PLC Programming with the Raspberry Pi and the OpenPLC Project

3.5.3 Conversion operators


IEC 61131 does not allow direct conversion from a "larger" variable type to a "smaller" type
(e.g., INT to BYTE or DINT to WORD). To achieve this, special types of conversions are re-
quired. Conversion operators are used to convert from any base type to another base type.
The example syntax is as follows.

TargetVariable: = ANY_TO_ < New data type > (Variable or constant value);

Conversion operator Example and description

ANY_TO_BOOL () Result := ANY_TO_BOOL( 2 ); // Result value is TRUE

ANY_TO_DINT () Result:= ANY_TO_DINT( 1234*100 ); // The Result value is


123400.

ANY_TO_INT () Result:= ANY_TO_INT( -2400.35 ); // The result value is –2400.

ANY_TO_REAL () Result:= ANY_TO_REAL( 54 ); // Result value is 54.0.

ANY_TO_SINT () Result:= ANY_TO_SINT( -24.35 ); // The result value becomes


–24.

ANY_TO_TIME () Result:= ANY_TO_TIME( 3000 ); // Result value is T#3s.

ANY_TO_UDINT () Result:= ANY_TO_UDINT( T#10ms ); // The result value is 10. It


is the same as DWORD.

ANY_TO_UINT () Result:= ANY_TO_UINT( 2400.52 ); // The result value is 2400.


Same as WORD.

ANY_TO_USINT () Result:= ANY_TO_USINT( 16#1234); // The result value is 16 #


34 or 52 decimal places. Same as BYTE.

ANY_TO_STRING () Result:= ANY_TO_STRING( 6436.234 ); // The result value is


'6436.234'...

BCD_TO_BIN () Result:= BCD_TO_BIN( 16#1234 ); // The result value is 1234.

BIN_TO_BCD () Result:= BIN_TO_BCD( 1234 ); // The result value is 16 # 1234


or 4660 decimal.

3.5.4 Standard function blocks according to IEC 61131-3

Description Symbol Argument list


Set/Reset Flip-Flop SR SR(SET1:= ,RESET:= ,Q1=> );
Reset/Set Flip-Flop RS RS(SET:= ,RESET1:= ,Q1=> );
Trigger for rising edges R_TRIG R_TRIG(CLK:= , Q=> );
Trigger for falling edge F_TRIG F_TRIG(CLK:= , Q=> );
Up counter CTU CTU(CU:= , RESET:= , PV:= , Q=> , CV=> );
Down counter CTD CTD(CD:= , LOAD:= , PV:= , Q=> , CV=> );
Up-down counter CTUD CTUD(CU:= , CD:= , RESET:= , LOAD:= , PV:= , QU=>
, QD=> , CV=> );

● 88
Chapter 3 • The OpenPLC Editor

Pulse Timer TP TP(IN:= , PT:= , Q=> , ET=> );


On Timer TON TON(IN:= , PT:= , Q=> , ET=> );
Off Timer TOF TOF(IN:= , PT:= , Q=> , ET=> );

Timer On (TON) ST Example

Definition //Timers are built-in function blocks with two input parameters and two
// output values.
// Timer1 : TON; create timer instance in Var

Code example Timer1( Start, T#5s);


Signal:=Timer1.Q;
Zaehlerwert:= Timer1.ET;

Rising Edge Trigger (R_TRIG) ST Example

Definition // R_TRIG enables the detection of a rising edge


// Example
Button: R_TRIG ;
Input1: Bool; // Input terminal
Counter :Dint; // Counter

Code example Button (Input1);


IF Button.Q THEN
Counter := Counter + 1; // Counts on rising edge
END_IF;

Falling Edge Trigger (F_TRIG) ST Example

Definition // F_Trig enables the detection of a falling edge


// Example
Button: F_TRIG ;
Input1: Bool; // Input terminal
Counter :Dint;

Code example IF Button.Q THEN


Counter := Counter + 1; // Counts on falling edge
END_IF;

● 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.

Code example IF NOT bEnable THEN


   RETURN;
END_IF;
// If the bEnable tag is FALSE, the rest of the program will not be
executed....

3.5.5 First ST program example


Now you're set to write an ST program to link inputs "%IX0.X" and to switch outputs
"%QX0.X" of the Raspberry PI. The switching is done using buttons "%IX0.X" for inputs,
and LEDs "%QX0.X" for outputs.

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

Figure 3.5.5.1: Create new project

Confirm these entries with "OK".

Figure 3.5.5.2: A new ST project.

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

Figure 3.5.5.3: Creating variables for the 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.

Figure 3.5.5.4: Entering program text.

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

3.5.6 ST example for controlling a conveyor belt


In this example we want to control a conveyor belt with a motor and two inductive limit
switches, i.e., sensor-left and sensor-right. If the workpiece is detected by the left sensor
and you press the "Start" button, the motor should kick in and move the conveyor belt with
the workpiece to the right. When the object reaches the right-hand position, the sensor on
the corresponding position detects the event and the motor switches off.

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.

Figure 3.5.6.1: Conveyor belt made from 3D-printed parts.

● 93
PLC Programming with the Raspberry Pi and the OpenPLC Project

Again, you start with the definition of the variables:

Variable Connection Direction Description

Start %IX0.0 Input "Start" button

SensorLeft %IX0.1 Input Left proximity switch

SensorRight %IX0.2 Input Right proximity switch

MotorOn %QX0.0 Output Motor On

MotorDir %QX0.1 Output Motor direction; 1= Right, 0= Left

Barcode %QX0.2 Output Barcode printing

BlinkLed %QX0.3 Output Control lamp for the machine

LedOK %QX0.4 Output Control lamp for OK

The motor is controlled with the two lines called "MotorOn" and "MotorDir". You first create
a new project called "PLC-Example-ConveyorBelt".

Figure 3.5.6.2: ST example for the Conveyor Belt.

● 94
Chapter 3 • The OpenPLC Editor

Figure 3.5.6.3: Creating the POU.

Figure 3.5.6.4: Variables list.

Proceed by specifying your variables and function blocks.

● 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.

3.5.7 Defining arrays with the OpenPLC Editor


Sometimes you need array data types for our programs, for example, to store measured
values or bit patterns. Here is a concise instruction on how to define the array types using
the OpenPLC editor.

● 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 .

Figure 3.5.7.1: ST example array.

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".

Figure 3.5.7.2: ST example array type.

● 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".

Figure 3.5.7.3: ST example of entering an array type.

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:

Symbol Function Description

Edit Edit the field in the table

Add Create new field in the table

Delete Delete a field in the table

Move Up Moving a field up

Move Down Moving a field down

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

Figure 3.5.7.4: ST example for array type variable picking.

This is the sample program to access the array variables.

Figure 3.5.7.5: ST example for array type.

● 99
PLC Programming with the Raspberry Pi and the OpenPLC Project

This is the translated ST program:

Figure 3.5.7.6: ST array type listing.

3.5.8 Defining structures with the OpenPLC Editor


Structured data types are required to get a better overview of the variables. Here is a short
tutorial on how to define structures with the OpenPLC editor.

Once again you start off with a new project. The definition of structures is started by click-

ing on the plus symbol . Choose data type.

● 100
Chapter 3 • The OpenPLC Editor

Figure 3.5.8.1: ST example of data type structure.

Select Data type and enter the name "MeasuredValues" in the structure tree.

Figure 3.5.8.2: ST example for Create structure.

Now select "Structure" from the Reference combo box.

● 101
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 3.5.8.3: A variable for structure.

With the plus symbol you enter in sequence, the elements you wish to get in your
structure.

Figure 3.5.8.4: Variable for 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.

Figure 3.5.8.5: Applying 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.

Figure 3.5.8.6: ST example structure.

Now follows the sample program to access the structure variables. The access to the ele-
ments is done via the point.

Figure 3.5.8.7: ST listing structure.

3.5.9 Combining structures with arrays using the OpenPLC Editor


Structured data types in combination with arrays are needed to get a better overview of the
variables. Here is a small tutorial how we can combine structures with arrays.

First, define the arrays by clicking on the plus symbol . Select data type.

● 103
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 3.5.9.1: ST example structure and arrays

After that, set the number of elements for your array.

Figure 3.5.9.2: Defining the integer array

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

Figure 3.5.9.3: Defining structure elements.

Now create the structure with the array data types defined earlier.

Figure 3.5.9.4: Applying a structure variable.

In the program part, you define the variable Room1 with the user-defined structure "Struk-
tRoom" and the Int variable "Loop".

Here is the sample program to access the structure variables.

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

Figure 3.5.9.5: Complete ST listing with structure and arrays.

3.5.10 Definition of ENUMs


ENUMs are the enumeration variables in an ST program. Here is an example of how they
are created in the OpenPLC Editor.

Figure 3.5.10.1: ST example Enums.

● 106
Chapter 3 • The OpenPLC Editor

Let's create a new project.

Figure 3.5.10.2: ST example data type ENUM.

With the + symbol, select data type and confirm. For Reference, enter Enumeration.

Figure 3.5.10.3: Entering the ENUM variables.

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

Figure 3.5.10.4: ST example ENUM.

Next, enter the small test program and load it to the Raspberry PI to test it.

(* Simple Example with ENUM *)


(* CASE with ENUM *)

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;

This is the complete program:

Figure 3.5.10.5: ST example using Enum.

● 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.

Figure 3.5.10.6: ST example of ENUM settings.

3.6 Sequential Function Chart example (SFC)


Programming with SFC makes it possible to work with standardized graphic symbols. The
symbols are arranged as in a program flow chart. This programming language is often used
in sequence control systems, including conveyor belts, gate controls, production lines, and
similar applications.

Figure 3.6.1: Example of an SFC program.

● 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).

Figure 3.6.2: Menu bar for SFC programs.

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

First step (start condition)

New step possibly with branching

New transition condition (Transition)

New action block (execute commands)

New branching, parallel and alternative branching are possible

New jump

Create a new variable

New function block

New connection

New power line

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.

Figure 3.6.3: Creating the SFC program.

As before, define your variables Button and LED with the appropriate inputs/outputs.

Figure 3.6.4: Declaring variables.

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

Figure 3.6.5: Adding a start block.

Now you need an action block to turn off the LED.

Figure 3.6.6: Inserting the required action.

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

N The action is executed as long as this step is active.

R Sets the BOOL variable to FALSE

S Sets the BOOL variable to TRUE

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.

SL The action is active for a certain interval.

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;".

The next operation is a transition (transition condition).

Figure 3.6.7: Inserting the transition.

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

Figure 3.6.8: Editing the program step.

After this, the next action follows to the right of it. It allows you to turn on the LED.

Figure 3.6.9: Editing the action block.

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

Figure 3.6.10: SFC program blocks.

Finally, you need the "Jump" icon permitting the program to start again at "Start".

Figure 3.6.11: Entering a jump with its jump target.

Now you can connect your blocks and comment the program.

● 115
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 3.6.12: Making connections.

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

TRANSITION FROM Start TO Step1


:= Button = 1;
END_TRANSITION

STEP Step1:
STEP1_INLINE2(N);
END_STEP

● 116
Chapter 3 • The OpenPLC Editor

ACTION STEP1_INLINE2:
LED:=1;
END_ACTION

TRANSITION FROM Step1 TO Step2


:= Button = 0;
END_TRANSITION

STEP Step2:
STEP2_INLINE3(N);
END_STEP

ACTION STEP2_INLINE3:
LED:=1;
END_ACTION

TRANSITION FROM Step2 TO Step3


:= Button = 1;
END_TRANSITION

STEP Step3:
STEP3_INLINE4(N);
END_STEP

ACTION STEP3_INLINE4:
LED:=0;
END_ACTION

TRANSITION FROM Step3 TO Start


:= Button = 0;
END_TRANSITION

END_PROGRAM

CONFIGURATION Config0

RESOURCE Res0 ON PLC


TASK task0(INTERVAL := T#20ms,PRIORITY := 0);
PROGRAM instance0 WITH task0 : PLC_Example_SFC_01;
END_RESOURCE
END_CONFIGURATION

Figure 3.6.13: SFC program listing.

● 117
PLC Programming with the Raspberry Pi and the OpenPLC Project

Adding divergence and convergence.


When adding a branch, the "New Branch" dialog box is displayed. It allows you to choose
between divergence and convergence.

Figure 3.6.14: Adding a branch.

Here comes an example of an SFC diagram with parallel-branching (divergence).

Figure 3.6.15: Parallel branching.

● 118
Chapter 3 • The OpenPLC Editor

And now, an example of an SFC diagram with alternative branching (convergence):

Figure 3.6.16: Alternate branching.

● 119
PLC Programming with the Raspberry Pi and the OpenPLC Project

Chapter 4 • OpenPLC and the Modbus

4.1 Testing PLC programs with Modbus TCP


Both in industrial control systems and in home automation, it's increasingly accepted prac-
tice to control and monitor the process via so called "monitors". This is also supported
by the openplcproject. On the Raspberry Pi, a Modbus TCP server is working in the back-
ground, establishing the connection to your PLC program. This enables you to monitor vari-
ables declared in the program via Ethernet or WLAN, and to simulate switches. The Modbus
TCP protocol is used for this purpose.

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

Figure 4.1.1: PLC settings.

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.

Figure 4.1.2: Creating the PLC project.

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

Figure 4.1.3: Entering the PLC program.

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.

Figure 4.1.4: Save PLC program.

● 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".

Figure 4.1.5: Modbus client test program.

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

Modbus commands for Description Start Number


Reading address of bytes

Command to read the lines (coils) you 1 2


have switched with FC1 or FC5.

Command to read the buttons %IX0.0 1 14


to %IX1.5

Read back D/A converter data (PWM) 1 1


that got written.

Read in the values from an A/D con- 1 1


verter. (Not implemented, returns 0)

Modbus commands for Writing

Command for switching an output. The 1…11 1


start address must be specified here.

Command for writing values to the D/A 1 1


converter. Here the PWM line.

Command for switching several out- 1 11


puts simultaneously here %QX0.0 to
%QX1.2 to TRUE.

Command for writing to multiple 1 1


registers.

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.

Figure 4.1.6: Reading inputs

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

Figure 4.1.7: Reading inputs.

The value of the two buttons now changes to "True".

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.

Figure 4.1.8: Reading outputs.

Switch the LED with button 1 on the board and read the status again.

Figure 4.1.9: Reading outputs.

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

Figure 4.1.10: Modbus PLC button and LED.

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 .

Figure 4.1.11: Setting the output.

The LED should now switch to "%QX0.1".

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.

Figure 4.1.12: Resetting the output.

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

Figure 4.1.13: Writing the PWM register.

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

Here's an overview of the Modbus addresses used in the Raspberry Pi:

● 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/

%ML1025 Number of executed scan cycles since the start of the


PLC program.

%ML1026 Slave devices communication error counter.

Below is a small example of these variables. A new program will be created with the follow-
ing variables and function blocks:

Figure 4.1.14: PLC test program.

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

Figure 4.1.15: PLC program monitoring.

Figure 4.1.16: Modbus TCP Tester.

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

4.2 Visualization of PLC programs with AdvancedHMI


In industrial control systems as well as in home automation, it's accepted practice to con-
trol and observe the current process using so-called monitors. In this Chapter you will
get acquainted with the AdvancedHMI software. You access the variables in the PLC and
try to visualize them. It is also possible to access the process and simulate switches with
it. AdvancedHMI is a .NET program written in Visual Basic using Microsoft's Visual Studio
Community software. If you install the Mono Framework on your Raspberry Pi, you can also
run the software on it and get access to the running PLC program.

Both AdvancedHMI and Visual Studio Community software can be downloaded for free.

Figure 4.2.1: AdvancedHMI example.

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

and add the "AdvancedHMI Base Package" to your shopping cart.

● 130
Chapter 4 • OpenPLC and the Modbus

Figure 4.2.2: AdvancedHMI.

After that, click on Checkout .

After a free registration procedure, the program can be downloaded for free and unpacked
into a directory. Save the ZIP file as a template.

Figure 4.2.3: AdvancedHMI unzip action.

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

Figure 4.2.4: AdvancedHMI project directory.

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/

Figure 4.2.5: Visual Studio Installer.

After the installation process is complete, start the first project.

● 132
Chapter 4 • OpenPLC and the Modbus

Figure 4.2.6: Open AdvancedHMI project.

With a right mouse click, let's open the project with Microsoft Visual Studio 2019.

Figure 4.2.7: Visual Studio user interface.

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

Figure 4.2.8: Visual Studio menu bar.

At this point, the project has been compiled and saved in this directory.

Figure 4.2.9: AdvancedHMI debug 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

Figure 4.2.10: Visual Studio Toolbox.

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

Figure 4.2.11: Modbus settings (IP and port).

To test the communication, create a new PLC program with the Open PLC Editor, load it to
the Raspberry Pi, and start it.

Figure 4.2.12: PLC example variable list.

● 136
Chapter 4 • OpenPLC and the Modbus

Figure 4.2.13: LD sample program.

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.

Figure 4.2.14: HMI program.

● 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.

Set the fields "PLCAdressClick" as follows.

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".

Figure 4.2.15: HMI address setting.

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

Function Usage PLC Modbus Register Value Access Advanced Example


Code Address Register Size Range HMI
FC01 Digital %QX0.0 - 0..799 1 Bit 0 or 1 Read / Write "00001" LED
Read Coil Outputs %QX99.7 (1-800) – "00800"
FC02 Digital %IX0.0 0..799 1 Bit 0 or 1 Read Only "100001" BTN
Discrete Inputs - %IX99.7 (1-800) – "100800"
Input
FC04 Analog %IW0 0..1023 16 Bit 0..65535 Read Only "30001" ADC
Input Inputs - %IW99 (1-1024) – "31024"
Register
FC03 Analog %QW0 0..1023 16 Bit 0..65535 Read / Write "40001" DAC
Holding Outputs - %QW99 (1-1024) – "41024"
Register

OpenPLC also provides a separate address space for memory variables with support for
16-, 32-, and 64-bit variables.

Register Usage PLC Modbus Register Value Access Advanced


Type Address Address Size Range HMI
Range
Holding General %MW0 1024..2047 16 Bit 0..65535 Read / "41025"
Register 16Bit - %MW1023 (1025-2048) Write – "42048"
Register
Holding General %MD0 2048..4095 32 Bit 0.. Read / "42049"
Register 32Bit - %MD1023 (2049-4096) 4.294.967.295 Write – "44096"
Register

Holding General %ML0 4096..8191 64 Bit 0..huge Read / "44097"


Register 64Bit - %ML1023 (4097-8192) Write – "48192"
Register

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

4.3 Visualization of PLC programs via the Internet


Both in industrial control systems and in home automation, the ability to control and watch
the current process via monitors is widely accepted. In this Chapter you enter into an appli-
cation allowing you to access your Raspberry Pi PLC from any computer in the world using
a web browser.

Figure 4.3.1: Internet-connected process visualization.

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.

Figure 4.3.2: Output of the Modbus POST program.

● 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:

$ sudo apt-get install codeblocks

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.

Figure 4.3.3: Code blocks projects.

After starting the IDE, you can open your project and edit it.

● 141
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 4.3.4: Codeblocks development environment.

Figure 4.3.5: Executing a program.

Make the bold and italic changes for "directory" and "myserver" enabling the data to arrive
at the server.

// String for Result from Modbus Call FC01


char strresult[16] = {'0',';','0',';','0',';','0',';','0',';','0',';','0',';','0'
,0};

// Post Message to HTTP Server


char *msg_post = "POST /directory/sqlquery.php HTTP/1.0\r\n";
char *msg_host = "Host: www.myserver.com\r\n";
char *msg_conn = "Connection: close\r\n";
char *msg_cont = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8;\
r\n";
char *msg_conl = "Content-Length: %d\r\n\r\n";

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.

Figure 4.3.6: Excerpt of a PHP program.

The HTML program also accesses this data via the PHP program or with http GET using
AJAX and displays it by the web browser.

Figure 4.3.7: AJAX query of the web page.

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.

Here's what the lines look like in the web browser:

Figure 4.3.8: Internet visualization.

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.

Figure 4.3.9: Simple web example.

● 144
Chapter 4 • OpenPLC and the Modbus

Possibilities for visualization with Javascript objects:

Figure 4.3.10: Visualization with round measuring devices.

Figure 4.3.11: Visualization with linear measuring devices.

● 145
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 4.3.12: Visualization using a chart.

In the Appendix, under web links, you can find the websites of the projects.

● 146
Chapter 5 • Modbus I/O modules

Chapter 5 • Modbus I/O modules

5.1 Modbus RTU module with the Arduino UNO

Figure 5.1.1: Arduino UNO board.

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:

Designation Direction Arduino Pins PLC addresses Modbus command

Digital In Input 2, 3, 4, 5, 6 %IX100.0 - %IX100.4 FC2 Read

Analog IN Input A0, A1, A2, %IW100 – %IW105 FC4 Read


A3, A4, A5

Digital OUT Output 7, 8, 12, 13 %QX100.0 - %QX100.3 FC5 Write


FC1 Read

Analog OUT Output 9, 10, 11 %QW100 - %QW102 FC6 Write


(PWM) 485 Hz FC3 Read

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".

Figure 5.1.2: Arduino UNO ModbusRTU project.

With a double click on , start the development environment.

In the editor you can view the Modbus program for the Arduino.

Figure 5.1.3: Arduino UNO source code.

● 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.

Figure 5.1.4: Arduino UNO IDE settings.

The next step is to set the serial port (USB) to which your Arduino is connected. In this
case, it's COM6 (Arduino UNO).

Figure 5.1.5: Arduino UNO IDE Commport.

● 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.

Figure 5.1.6: Arduino UNO IDE upload.

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".

Figure 5.1.7: Arduino UNO ModbusRTU test.

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.

Figure 5.1.8: Arduino UNO pinout.

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.

Figure 5.1.9: Test output

● 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.

Figure 5.1.10: Test output

"Read Coils - FC1" allows you to check this.

Figure 5.1.11: Reading in PINs.

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).

Figure 5.1.12: Writing to Modbus registers.

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.

Figure 5.1.13: Reading the Modbus inputs.

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.

Figure 5.1.14: Reading from a Modbus ADC.

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.

Figure 5.1.15: USB ID on the Raspberry Pi (CDC ACM).

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

Figure 5.1.16: Raspberry Pi Modbus Slave settings.

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:

Figure 5.1.17: PLC Modbus RTU test program (variable).

● 154
Chapter 5 • Modbus I/O modules

The test program "PLC-Example-ArduinoUNO-Modbus".

Figure 5.1.18: PLC Modbus RTU ST test program.

Figure 5.1.19: Monitoring the PLC Modbus RTU test program.

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

Figure 5.1.20: Optocoupler input converting 24 V to 5 V swing.

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.

Figure 5.1.21: 24 V output swing for up to 1.5 A.

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.

Figure 5.1.22: DC output for 0 to 10 V range.

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

Figure 5.1.23: Input circuit for the A/D converter.

This next voltage divider with diodes for reverse-polarity protection allows you to measure
voltages in the 0 to 10 V range.

Figure 5.1.24: RS485 interface for Modbus RTU.

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

Figure 5.1.25: Modbus RTU RS485 multidrop network (source: Maxim).

For the operation as an RS-485 interface, the following program changes are necessary:

//ModBus Port information


#define BAUD 115200
#define ID 1
#define TXPIN 7

//Define the number of registers, inputs and coils to be created


#define NUM_DISCRETE_INPUT 5
#define NUM_INPUT_REGISTERS 6
#define NUM_COILS 3
#define NUM_HOLDING_REGISTERS

//Create the I/O pin masks


uint8_t pinMask_DIN[] = {2, 3, 4, 5, 6};
uint8_t pinMask_AIN[] = {A0, A1, A2, A3, A4, A5};
uint8_t pinMask_DOUT[] = { 8, 12, 13};
uint8_t pinMask_AOUT[] = {9, 10, 11};

5.2 Modbus TCP module with the ESP8266 and WLAN


This Chapter describes an ESP8266 I/O board that's compatible with 24 V I/O. The module
is suitable for Modbus TCP over WLAN and also for HTML server applications. The Internet
has many different examples for the ESP8266. The board is controlled by an ESP8266 No-
deMCU module.

● 158
Chapter 5 • Modbus I/O modules

Figure 5.2.1: ESP8266 NodeMCU Board.

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:

• four 24V inputs, with EL816 optocouplers


• (The optocoupler / switches from approx. 14 V / safety galvanic isolation)
• four 24 V outputs with Infineon's HITFET BSP76
• one analog input with 0 to 10 V range (voltage divider)
• one analog output with 0 to 10V range (OP LM7301)
• an input for the type DHT11/22 temperature and humidity sensor is also availa-
ble. (also, DS18B20)

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

Figure 5.2.4: Layout of the ESP8266 I/O module for 24 V supply.

Figure 5.2.5: Picture of the ESP8266 I/O module for 24 V supply.

● 161
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 5.2.6: Internal circuit of the HITFET type BSP76.

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

And confirm with "OK".

● 162
Chapter 5 • Modbus I/O modules

Figure 5.2.7: Installing the board manager.

Then call the "Board manager" in the Tools menu and enter: ESP8266. Select the current
version and install it.

Figure 5.2.8: Installing ESP8266 support.

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

Figure 5.2.9: Setting the ESP8266 board in the IDE.

After the restart, enter in the program "ESP8266_Modbus_BOARD1_1.ino" into lines 30


and 31 to pass your access data to the domestic or in-office router.

/*********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.

After the installation, restart the computer if necessary.

Proceed by setting the Commport for the program upload:

● 164
Chapter 5 • Modbus I/O modules

Figure 5.2.10: Commport setting for the ESP8266 in the DIE.

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.

Figure 5.2.11: Commport setting for the ESP8266 in the IDE.

● 165
PLC Programming with the Raspberry Pi and the OpenPLC Project

Next, set the baud rate to 115200.

Figure 5.2.12: Commport setting for baud rate.

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

Python test program for the ESP8266 Modbus TCP module:


For a first test from the PC, a Python test program is also available. It is located in the
download directory under "PythonModbus" and has the file name "python_modbus_cli-
ent_tkinter_esp8266.py". With this it is possible to switch the lines, read the analog input
and output voltages to the D/A converter.

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

5.3 Web server application with the ESP8266 I/O module


The ESP8266 is not only suitable as a Modbus module but also as a stand-alone module
with a web interface. Users often wish to switch devices in-house, in-office, within a , or to
query alarm contacts while on the road. This is also possible with this module.

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.

Figure 5.3.1: ESP8266 NodeMCU Board within the Internet.

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

Figure 5.3.2: Arduino HTTP Post Sender.

This PHP program either saves the data to a database or stores it as a file.

Figure 5.3.3: PHP server program.

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

Figure 5.3.4: Excerpt XML http request of the website.

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.

Figure 5.3.5: JavaScript section of the web page.

● 170
Chapter 5 • Modbus I/O modules

Figure 5.3.6: JavaScript function for switching the LEDs.

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.

Figure 5.3.7: Web interface for the ESP8266 module.

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.

You again modify the data for your own WLAN.

// 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

//Your Domain name with URL path or IP address with path


const char* serverName = "http://www.meinserver.de/esp8266web/sqlabfrage.php";

Now, the program gets transferred to the ESP8266 using the right-arrow.

Figure 5.3.8: ESP8266 NodeMCU Board Pins.

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

Figure 5.3.9: JavaScript library round gauges.

Figure 5.3.10: JavaScript-library linear gauges.

● 173
PLC Programming with the Raspberry Pi and the OpenPLC Project

Figure 5.3.11: Visualization with a chart.

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

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)

Programmable Logic Controllers:


A Practical Approach to IEC 61131-3 Using Codesys
Hanssen (2015)

[German]

Raspberry Pi - Das umfassende Handbuch


Michael Kofler, Charly Kühnast, Christoph Scherbeck

Grundkurs der Steuerungstechnik: Mit einer Einführung in


Speicherprogrammierbare Steuerungen und DIN EN 61131-3
Peter Beater

SPS-Programmierung mit IEC 61131-3


Konzepte und Programmiersprachen, Anforderungen an Programmiersysteme,
Entscheidungshilfen
John, Karl-Heinz, Tiegelkamp, Michael

SPS-Programmierung in Anweisungsliste nach IEC 61131-3


Eine systematische und handlungsorientierte Einführung in die strukturierte
Programmierung
Adam, Hans-Joachim, Adam, Mathias

SPS-Softwareentwicklung mit IEC 61131


Jens von Aspern (2000)

Rheinwerk Python Buch

● 175
PLC Programming with the Raspberry Pi and the OpenPLC Project

6.2 Web links

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

Josef Bernhardt (Author)


http://www.bernhardt.de/openplcproject/

Modbus Web site


https://modbus.org/

Modbus RTU Specification User Guide


https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf

Modbus TCP/IP Specification User Guide


https://modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf

History of Modbus
http://www.summaryplanet.com/summary/History-of-Modbus-protocol.html

Free library for Modbus


https://libmodbus.org/documentation/

● 176
Chapter 6 • Bibliography, circuit diagrams and layouts

EasyModbus Client and Server


https://sourceforge.net/projects/easymodbustcp/
https://github.com/rossmann-engineering/EasyModbusTCP.NET

Modbus TCP test program client for Sharp Develop


https://github.com/stephan1827/modbusTCP-DotNET

AdvancedHMI
https://www.advancedhmi.com/index.php?main_page=page&id=14&chapter=0

AdvancedHMI Quick Start Guide Download


https://www.advancedhmi.com/downloads/QuickStart.pdf

Visual Studio Community Software Download


https://visualstudio.microsoft.com/vs/community/

MONO Project
https://www.mono-project.com/

Arduino
https://www.arduino.cc/

Arduino IDE Download


https://downloads.arduino.cc/arduino-1.8.13-windows.exe

Arduino UNO Modbus Software Download


https://www.openplcproject.com/runtime/arduino/

ESP8266 E/A Modul Modbus TCP Software Download


https://www.openplcproject.com/runtime/esp8266/

USB Driver CH340


Driver&Tools - Nanjing Qinheng Microelectronics Co., Ltd. (wch-ic.com)
http://www.wch-ic.com/downloads/category/30.html

Canvas Gauge API in Javascript for web pages


https://canvas-gauges.com/

Rheinwerk Python Book [German]


https://openbook.rheinwerk-verlag.de/python/

Chart Library
https://www.chartjs.org/samples/latest/

● 177
PLC Programming with the Raspberry Pi and the OpenPLC Project

6.3 Modbus commands for the ESP8266 I/O module


The module has four digital inputs and outputs with 24V and one analog input and output
with 0 to 10V each.

Outputs

Output 1 Write Single Coil (FC5)


ON
transmit
Tx: 00 05 00 00 00 06 01 05 00 00 FF 00
receive
Rx: 00 0B 00 00 00 06 01 05 00 00 FF 00

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

Output 2 Write Single Coil (FC5)


ON
transmit
Tx: 00 09 00 00 00 06 01 05 00 01 FF 00
receive
Rx: 00 09 00 00 00 06 01 05 00 01 FF 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

Switch multiple outputs (4) simultaneously

Output Write Multiple Coils (FC15)


1,2,3,4 ON
transmit
Tx: 00 0E 00 00 00 08 01 0F 00 00 00 04 01 0F (the last byte holds the bit pattern
00001111)
receive
Rx: 00 0E 00 00 00 06 01 0F 00 00 00 04
1,2,3,4 OFF
Tx: 00 0F 00 00 00 08 01 0F 00 00 00 04 01 00
receive
Rx: 00 0F 00 00 00 06 01 0F 00 00 00 04

● 178
Chapter 6 • Bibliography, circuit diagrams and layouts

Switch multiple outputs (4) simultaneously

Output Write Multiple Coils (FC15)


1,3 ON
transmit
Tx: 00 0E 00 00 00 08 01 0F 00 00 00 04 01 05 (the last byte holds the bit pattern
00000101)
receive
Rx: 00 0E 00 00 00 06 01 0F 00 00 00 04
1,2,3,4 OFF
Tx: 00 0F 00 00 00 08 01 0F 00 00 00 04 01 00
receive
Rx: 00 0F 00 00 00 06 01 0F 00 00 00 04

Read the status of the outputs

Read Coils (FC1)


ON
transmit
Tx: 00 13 00 00 00 06 01 01 00 00 00 04 ( the last byte holds the number of bytes,
here 4)
receive
Rx: 00 13 00 00 00 04 01 01 01 05 ( the last byte holds the bit pattern
00000101)

Read the optocoupler inputs

Read Discrete Inputs (FC2)


transmit
Tx: 00 1C 00 00 00 06 01 02 00 00 00 04 ( the last byte holds the number of bytes,
here 4)
receive
Rx: 00 1C 00 00 00 04 01 02 01 02 ( the last byte holds the bit pattern
00000010)

Read A/D converter

Read Input Registers (FC4) 0 V at the input


transmit
Tx: 00 1F 00 00 00 06 01 04 00 00 00 01
receive
Rx: 00 1F 00 00 00 05 01 04 02 00 04 (the last byte holds the value 4)

● 179
PLC Programming with the Raspberry Pi and the OpenPLC Project

Read Input Registers (FC4) 5 V at the input


transmit
Tx: 00 24 00 00 00 06 01 04 00 00 00 01
receive
Rx: 00 24 00 00 00 05 01 04 02 02 0D (the last byte holds the value 525)

Read Input Registers (FC4) 9 V at the input


transmit
Tx: 00 24 00 00 00 06 01 04 00 00 00 01
receive
Rx: 00 25 00 00 00 05 01 04 02 03 AC (the last byte holds the value 940)

Output values to the D/A converter

Write Single Register (FC6) Example:


Value 300 at the output corresponds to 2.895V
transmit
Tx: 00 29 00 00 00 06 01 06 00 00 01 2C (the last byte holds the value 300)
receive
Rx: 00 29 00 00 00 06 01 06 00 00 01 2C

Read values from D/A converter register

Read Holding Register (FC3) Example:


Value 300 at the output corresponds to 2.895V
transmit
Tx: 00 2C 00 00 00 06 01 03 00 00 00 01
receive
Rx: 00 2C 00 00 00 05 01 03 02 01 2C (the last byte holds the value 300)

● 180
Chapter 6 • Bibliography, circuit diagrams and layouts

6.4 Circuit diagrams and layouts


6.4.1 Circuit diagram for the test board with buttons and LEDs:

● 181
PLC Programming with the Raspberry Pi and the OpenPLC Project

Layout for the test board:

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

Layout for the 24V I/O test board:

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

Layout for the ESP8266 board:

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

client 15, 120 desktop sharing system 15


clipboard 23, 50 DHT11/22 159
Codeblocks, C_HTTPPOST01 140, 141 DINT 82
COM6 149 direct voltage 156
Commander 20 DIV 80
comment 83, 110 divergence 118
Common operator 87 Division 80, 87
Commport 149, 164 DNP3 26
communication error counter 128 DO WHILE 54
community 47 download area 26
compatibility 8 driver, drivers 162, 164, 171
compiled, compiler 134 DS18B20 159
configuration 17, 21
connect 140, 141 E
Connected to Server 150 EasyModbusClient 123, 150
connections 65, 111 EasyModbusClientAndServer 123
contact 110 Editor ST file 35
convergence 119 Edouard Tisserant 7
conversion operator 88 EL816 optocouplers 159
conveyor belt 93, 109 electronic switch 42
copy 50, 141 elements 102
COS 87 ELSE 86
counter 88 ELSEIF 85
country and time zone selection 14 empty project 33
coupling relay 160 enable SSH on the Raspberry Pi 18
C program 140 END_CASE 86
Create a new variable 110 END_FOR 83
Create Program 50 END_IF 85
CTD, CTU, CTUD 88 END_REPEAT 90
current limiting 42 END_WHILE 84, 90
Cutting objects 50 Entering the ENUM variables 107
ENUMs 106
D equal 87
D/A 124, 167, 180 ESP8266 158, 162, 165, 186
Dashboard 39 ESP8266_HTTP_POST 171
data management 81 ESP8266 I/O board 158
data type 58, 59, 69, 96, 100, 103, 107 ESP8266 I/O module 168, 171
DC output 156 ESP8266_Modbus_BOARD1_1 162
Debug menu 48 ESP8266 NodeMCU Board Pins 172
declare, declaring 91 Ethernet 11, 120
Declaring variables 59, 111 exclusive OR 80
default value 83 existing project 50
define 111 EXIT 90
definition 94 EXP 87
Description 28 expression 82, 83
desktop 17, 18 EXPT 87

● 188
Index

F humidity sensor 159


falling edge 88
Falling Edge Trigger 89 I
FC1 124, 126, 140, 148, 152 I2C 159
FC2 124, 140, 148, 153, 179 I2S 159
FC3 148 Idle 167
FC4 148, 153, 179, 180 IEC 61131-3 9, 88
FC5 124, 148, 151, 152, 178 IEC address 59, 122
FC6 126, 148, 152, 180 IEC programming languages 81
FC15 179 IEC symbols 59
FC16 152 IEEE 802.11 159
field buses 26 IF 85, 87
file dialog 68 IL command overview 80
File Menu 45 IL program example 53, 77
File menu of the OpenPLC Editor 45 Imager 13
File Transfer Software WinSCP 18 Imager program 12
FOR 82, 83 index.html 143, 172
F_TRIG 88, 89 inductive and capacitive loads 42
full screen 47 industrial controllers 41
function block 9, 51, 52, 69, 72, 80, 81, 83, industrial control systems 8, 120, 130, 140
88 industrial environments 8
Function Block example 67 Infineon 42, 159
FUP 10 infinite-loop condition 85
initialization value 102
G initial start 14
galvanic isolation 41, 155 initial test 26
gate 67 Inline 112
gate controls 109 Input 113
gaugetest.html 174 Input circuit 157
Generic ESP8266 Module 163 inputs/outputs 111
Git 22 installation 12, 120, 132, 139, 141, 167
Go to Dashboard 29 instruction list 9, 51, 53
GPIO 8, 11, 42, 137, 159 Instruction List example 76
Greater than, Greater than equal to 87 Internet 140
I/O board 158, 159, 162, 166
H I/O pins 147
HDMI 11 IP address 14, 165
Hello_World 33, 34, 36 IX0.0 41
HelloWorld.ST 36
Help menu 47 J
HITFET BSP76 41, 159 JavaScript function 144, 171
home automation 8, 120, 130 JavaScript-library 173
house 168 Javascript objects 145
HTML 143 JavaScript section 170
HTML server 158 jump with target 115
http post 168, 171

● 189
PLC Programming with the Raspberry Pi and the OpenPLC Project

K Modbus commands 123


KOP 10, 36, 37, 60 Modbus commands for the ESP8266 I/O
module 178
L Modbus documentation 127
ladder diagram 51, 52, 55, 58, 60, 72 Modbus ESP8266 module 166
Ladder Logic 9 Modbus "FC" command 144
Ladder Logic Example 55 Modbus interface 167
lamp 37, 41 Modbus I/O modules 147
LANGUAGE_DEFAULT 33 Modbus libraries 167
LANGUAGE_ENGLISH 33 Modbus program for the Arduino 148
LED 114 Modbus register 139, 152
LedOK 94 Modbus RTU 120
Less equal 87 Modbus RTU module 147
less than or equal 81 Modbus-RTU pin assignment 148
letters 82 Modbus RTU RS485 multidrop network 158
library 47, 53 Modbus server 26
license 19 Modbus settings 136
link inputs 90 Modbus Slave 154
LN 87 Modbus TCP 8, 120
localhost:8080 24 ModbusTCP-DotNET-Master.zip 129
LOG 87 Modbus-TCP Master program 129
login 25 Modbus TCP module 159
loop 84, 90 Modbus TCP protocol 120
low pass 156 Modbus TCP Tester 129
Modicon 120
M modifier 61, 62
machine 8 modules 71, 72
MainForm.vb 133 Modulo 87
main menu 44 MomentaryButton 137, 138
MainTask 49 monitoring 25, 29, 41, 79, 129, 155
Making connections 116 monitors 120
Mario de Sousa 7 Mono Framework 43, 130, 134, 139
Master-Slave principle 120 motor 38, 41, 93
mathematical calculations 81 MotorDir 94
Matiec 7, 10, 36, 67 MotorLeft 107
menu 44, 50 MotorOff 107
microcontroller 53, 76, 147 MotorOn 94
micro SD card 10, 12, 13, 14 MotorRight 107
Microsoft Visual Studio 130 MotorState 107
minus 58, 69 MUL 80
MOD 87 multiplexer 74HC151 166
Modbus 120 Multiplication 80, 83, 87
Modbus ADC 153 my_button 37
Modbus addresses 127
Modbus address mapping 138 N
Modbus client 140 name 58, 69

● 190
Index

network 14, 24, 52, 55, 157 PLC-Example-FBD 68


New action block 110 PLC-Example-FUP 68
New branching 110 PLC-Example-KOP 121
New coil 60 PLC program 9, 25, 120
New connection 72, 110 PLC programming 8, 76
New function block 60, 110 PLC ST-Program 28
New jump 110 plus symbol 97, 100, 102, 103, 104
New power line 110 position 93
New project 50, 57, 76, 90 Power Rail left and right 60
New variable 72 power supply rails 55
Niklaus Wirth 54 power supply socket 147
NodeMCU module 158 Prepare Coils 126
NOT 80, 87, 92 Prepare Registers 126
Notepad++ 33, 37, 74 processing units (POUs) 81
not equal 81 process via monitors 140
NOT inverted 87 process visualization 140
production lines 109
O product version 46
opener 62 program block 90
OpenPLC 32, 34, 36, 44, 46, 48, 57 program comments 116
OpenPLC Editor 32, 44 program cycle 49, 109
OpenPLC runtime 25 program flow 109
OpenPLC Runtime 23, 24, 25, 26 program generated by the compiler 116
OpenPLC Runtime page 24 program interval 49
OpenPLC_Uno 148 program monitoring 129
OP LM7301 156, 159 Program Organization Unit 81
optocoupler 41, 42, 156, 159, 179 program runtime 49
OR 67, 80, 83, 87 program sequences 110
output 111, 113, 124 program step 114
output pins 122 program upload 164
overtemperature 42 project 33
overvoltage 42 project directory 73
project name 69
P project properties, settings 46
Parallel branching 118 protection mechanisms 42
Pascal 54, 81 proximity switch 94
password 14, 17, 25, 38 Pulse Timer 89
Pasting objects 50 PWM 124, 148
PHP program 143, 144, 169, 170 PWM LED 126
PHP server program 168, 169 PWM output 38, 41, 126
PilotLight 137 PWM register 127
pin assignment 38, 148 PyModbus 167
PIP 167 pymodbus_openplc_post.py 140
PLCAdressClick 138 PythonModbus 167
PLC Editor 136 python_modbus_client_tkinter_esp8266.py
PLC-Example-ConveyorBelt 94 167

● 191
PLC Programming with the Raspberry Pi and the OpenPLC Project

Python program 140 Run in terminal 142


Python test program 167 RUN Mode 26
running light 26
Q runtime library 7
qualifier 112, 113 runtime program 38
QX0.0 41
S
R Save As 50
Raspberry Pi 120 Save Changes 26
Raspberry Pi Documentation 14 Save device 154
Raspberry Pi Imager 12 scan cycles 128
Raspberry Pi Modbus Slave settings 154 Schneider 120
Raspberry PI ModbusTCP server 123 semicolon 83, 92
Raspberry PI Monitor 128 sensor 120
Raspberry Pi OS (32-bit) 13 SensorLeft 94
Raspberry Pi PLC 130 SensorRight 94
Raspberry Pi's IP address 21 sequence control 8, 109
Raspberry Pi (VNC Server) 15 sequential function chart 9, 51, 110
Raspberry Pi Zero W 11 Sequential Function Chart example 109
RaspiTestBoard 124 Sequential Function Chart (SFC) 55
Read Coils 179 Serial Monitor 165
reading outputs 125 server 38, 120
Real 82, 102 SET 69
Reference 98, 101 settings 25, 26, 46
register 124, 139, 180 SFC 9, 51, 55, 109
relay 34 SFC program blocks 115
relay coil 56 SFTP client 18
relay contact 64 SHL 87
relay outputs 8 SHR 87
REPEAT 90 simple state machine 110
REPEAT Cycle 84 SIN 87
REPEAT UNTIL 54 Slave Devices 153
RESET 69, 88 Small 87
Reset value 80 software architecture 9
RETURN Instruction 90 SPI 159
reverse-polarity protection 157 sqlquery.php 143, 172
RISC CPU 159 SQRT 87
rising edges 88 SR 88
Rising Edge Trigger 89 SR flipflop 69
ROL 87 SSH configuration 18
ROR 87 ST 80
RS 88 ST array type listing 100
RS-232 120 start condition 110, 111
RS-485 interface 158 start screen 32, 44
RS-485 network 120 start value 84
R_TRIG 88, 89 Statements 82

● 192
Index

ST code 37 timestamp 128


step 110, 113 time zone selection 14
ST example 89, 93 TOF 89
ST example data type ENUM 107 TON 89, 91, 92
ST example structure 103 Tools 163, 165
ST file 27, 50 TP 89
ST file listing 67 transistor 42, 155
STL files 93 transition 55, 110, 114
ST Listing 75 transition condition 113
ST listing structure 103 translate 10, 35, 66, 72, 74
ST listing with structure and arrays 106 translation 96
STN 80 translation completed 96
ST program 29 transmit 178, 179, 180
ST project save function 36 TRUNC 87
ST project (structured text) 50 type 58, 69, 82
Structured control language 54
structured data types 100, 103 U
Structured Text 9, 51 UART 159
Structured Text examples 81 underscore 58, 69, 82
structure tree 44 Unipi 1.1 8
structure variable 105 Unix epoch 128
SUB 80 Unix timestamp 128
Substraction 87 UNTIL 84
sudo apt-get install git 22 upload 10, 166
switch 124 USB 11
symbols 55, 67, 98, 109, 110 USB ID on the Raspberry Pi 153
system 168 USB-RS485 interface 157
system language 33 USB-Serial converter 164
user-defined data types 98
T user interface 10, 20, 47
tabs 83 username 14, 17
TAN 87
task settings 73 V
temperature 159 value 80
temperature and humidity sensor 159 variable 113, 120
Tensilica 159 variable list 136
terminal window 23, 30, 141, 153 Variable names 82
test board 29, 39, 138, 181, 182, 183, 184 version 2.52 30
Test Board 40 version number 28
Testing PLC programs with Modbus TCP 120 "View" menu 47
T-flip flop 110 visualize processes 10
Thiago Alves 7 Visual Studio 43, 134
time interval 113 Visual Studio Community software 130
timer 110, 137 Visual Studio Installer 132
Timer On 89 Visual Studio Toolbox 135
TimerReady 92 Visual Studio user interface 133

● 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.

The book ends with an overview of commands for ST and LD.


After reading the book, the reader will be able to create his own
controllers with the Raspberry PI. Elektor International Media
www.elektor.com

Josef Bernhardt

Cover PLC ENGLISH Josef Bernhardt.indd Alle pagina's 12-11-2021 13:51

You might also like