PLC Programming with the Raspberry Pi and the OpenPLC Project [Team-IRA] 3895764698, 9783895764691


2,297 204 29MB

English Pages [194]

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Content
Preface
Introduction
Chapter 1 • Installing the Raspberry Pi 4
1.1 Hardware description
1.2 Installing the operating system
1.3 Installing the VNC Viewer
1.4 Installing the File Transfer Software WinSCP
1.5 Installing the openplcproject runtime
Chapter 2 • OpenPLC Editor Installation
2.1 Download and Installation
2.2 Raspberry Pi pin descriptions
2.3 Additional Hardware I/O Test Board
2.4 Additional 24 V PLC Board hardware
Chapter 3 • The OpenPLC Editor
3.1 Description of the OpenPLC Editor
3.2 Ladder Logic Example (LD)
3.3 Function Block example (FBD)
3.4 Instruction List example (IL)
3.5 Structured Text examples (ST, SCL)
3.5.1 Variable
3.5.2 Control structures
3.5.3 Conversion operators
3.5.4 Standard function blocks according to IEC 61131-3
3.5.5 First ST program example
3.5.6 ST example for controlling a conveyor belt
3.5.7 Defining arrays with the OpenPLC Editor
3.5.8 Defining structures with the OpenPLC Editor
3.5.9 Combining structures with arrays using the OpenPLC Editor
3.5.10 Definition of ENUMs
3.6 Sequential Function Chart example (SFC)
Chapter 4 • OpenPLC and the Modbus
4.1 Testing PLC programs with Modbus TCP
4.2 Visualization of PLC programs with AdvancedHMI
4.3 Visualization of PLC programs via the Internet
Chapter 5 • Modbus I/O modules
5.1 Modbus RTU module with the Arduino Uno
5.2 Modbus TCP module with the ESP8266 and WLAN
5.3 Web server application with the ESP8266 I/O module
Chapter 6.1 • Bibliography, circuit diagrams and layouts
6.2 Web links
6.3 Modbus commands for the ESP8266 I/O module
6.4 Circuit diagrams and layouts
6.4.1 Circuit diagram for the test board with buttons and LEDs:
6.4.2 Circuit diagram for the test board with 24V input outputs:
6.4.3 Circuit diagram for the ESP8266 board with 24V input outputs:
Index
Recommend Papers

PLC Programming with the Raspberry Pi and the OpenPLC Project [Team-IRA]
 3895764698, 9783895764691

  • Commentary
  • Thanks to Team-IRA
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

books books

books

>

PLC Programming

ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266 Introduction to PLC programming with OpenPLC, the fi rst fully open source Programmable Logic Controller on the Raspberry Pi, and Modbus examples with Arduino Uno and ESP8286 PLC programming is very common in industry and home automation. This book describes how the Raspberry Pi 4 can be used as a Programmable Logic Controller. Before taking you into the programming, the author starts with the software installation on the Raspberry Pi and the PLC editor on the PC, followed by a description of the hardware. You'll then find interesting examples in the different programming languages complying with the IEC 61131-3 standard. This manual also explains in detail how to use the PLC editor and how to load and execute the programs on the Raspberry Pi. All IEC languages are explained with examples, starting with LD (Ladder Diagram) over ST (Structured Control Language) to SFC (Special Function Chart). All examples can be downloaded from the author's website. Networking gets thorough attention too. The Arduino Uno and the ESP8266 are programmed as ModbusRTU or ModbusTCP modules to get access to external peripherals, reading sensors and switching electrical loads. I/O circuits complying with the 24V industry standard may also be of interest for the reader. 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.

Josef Bernhardt became interested in electronics at a very young age, when he built his first detector receiver, followed over the years by many other circuits. He gained his first programming experience in the 1980s with the CommodoreVC20. He became familiar with assembler programming on the 8088 processor. Josef can look back on more than 30 years of electronics and software development at University of Regensburg. With his own SMD production, he also implements electronic customer projects, always driven by the pleasure of electronics and programming.

Elektor International Media www.elektor.com

PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt

with the Raspberry Pi and the OpenPLC project

PLC Programming

with the Raspberry Pi and the OpenPLC project ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266

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 started 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 programs. Later, when the Raspberry Pi became increasingly popular, Thiago Alves started the "openplcproject". 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 purposes because it abides by the IEC standard. Beginners will also learn everything about installation and programming in the five programming 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 "openplcproject" 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 AdvancedHMI 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 using 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 website 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 widely 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 operating 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 Imager 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 successful 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 Documentation 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 selection. 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 installation 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 between 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 example, 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 Terminal 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. Configuration 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 Documents), 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 diagram (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 compiled. 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 schematic). 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 Raspberry 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 interesting 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 galvanic 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 protection 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 programming 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, product 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

Paste

Pasting objects from the clipboard into the 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 microcontrollers. 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 modern 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 graphical symbols. The symbols are arranged in networks similar to current paths of relay schematics. 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 Raspberry 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 subdirectory 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 "Program" 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. under Name, Class and Type. Select Now we enter the variables in the middle field with 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. Selecting 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 contact 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 " icon. This file contains the ST program you need to upload to the the "Create program 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 symbols 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 subdirectory 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 "Program", 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. , you enter the variables in the middle field under Name, Class and Type. Now, with 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

Program " icon to After all connections are made, go ahead and try to use the "Create 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 translated 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 assembly 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 functions 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

ANDN OR ORN XOR XORN

Bitwise AND of the accumulator and the negated operand, the result will be in the accumulator 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 accumulator Bitwise exclusive OR of the accumulator and the operand, the result will be in the accumulator Bitwise exclusive OR of the accumulator and the negated operand, the result will be in 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

MUL DIV

● 80

Multiplication of the accumulator with the operand, the result is loaded into the accumulator Division of the accumulator by the operand, the result is loaded into the accumulator

Chapter 3 • The OpenPLC Editor

GT GE EQ NE LE LT

The accumulator value is compared with the operand value (> (greater than)). The value (TRUE or FALSE) is loaded into the accumulator. The accumulator value is compared with the operand value (> (greater than or equal to)). The value (TRUE or FALSE) is loaded into the accumulator. The accumulator value is compared with the operand value (= (equal)). The value (TRUE or FALSE) is loaded into the accumulator. The accumulator value is compared with the operand value ( (not equal)). The value (TRUE or FALSE) is loaded into the accumulator. The accumulator value is compared with the operand value (

PLC Programming

ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266 Introduction to PLC programming with OpenPLC, the first fully open source Programmable Logic Controller on the Raspberry Pi, and Modbus examples with Arduino Uno and ESP8286 PLC programming is very common in industry and home automation. This book describes how the Raspberry PI 4 can be used as a Programmable Logic Controller. Before taking you into the programming, the author starts with the software installation on the Raspberry PI and the PLC editor on the PC, followed by a description of the hardware. You'll then find interesting examp les in the different programming languages complying with the IEC 61131-3 standard. This manual also explains in detail how to use the PLC editor and how to load and execute the programs on the Raspberry PI. All IEC languages are explained with examples, starting with LD (Ladder Diagram) over ST (Structured Control Language) to SFC (Special Function Chart). All examples can be downloaded from the author's website. Networking gets thorough attention too.The Arduino UNO and the ESP8266 are programmed as ModbusRTU or ModbusTCP modules to get access to external peripherals, reading sensors and switching electrical loads. I/O circuits complying with the 24V industry standard may also be of interest for the reader. 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.

Josef Bernhardt became interested in electronics at a very young age, when he built his first detector receiver, followed over the years by many other circuits. He gained his first programming experience in the 1980s with the Commodore VC20. He became familiar with assembler programming on the 8088 processor. Josef can look back on more than 30 years of electronics and software development at UNI Regensburg. With his own SMD production, he also implements electronic customer projects, always driven by the pleasure of electronics and programming.

Elektor International Media www.elektor.com

PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt

with the Raspberry Pi and the OpenPLC project

PLC Programming

with the Raspberry Pi and the OpenPLC project ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266

Josef Bernhardt

Cover PLC ENGLISH Josef Bernhardt.indd Alle pagina's

12-11-2021 13:51