0% found this document useful (0 votes)
18 views47 pages

LC001 - Release

Uploaded by

MDB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views47 pages

LC001 - Release

Uploaded by

MDB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

LC001

The Basics Of PLC Programming With CODESYS

Liam Bee
ALVEARE LEARNING
Alveare Learning LC001 Liam Bee

Contents
LC001 - The Basics Of Programming With Codesys............................................. 3
Course Outline ........................................................................................................ 3
Story Board ............................................................................................................. 4
C-B-0001 - Introduction To CODESYS .................................................................... 5
About CODESYS .................................................................................................... 5
Versions.................................................................................................................. 5
Downloading CODESYS ........................................................................................ 5
Why Choose CODESYS?....................................................................................... 6
C-B-0002 - CODESYS Environment ........................................................................ 7
CODESYS Environment Overview ......................................................................... 7
Key Areas ............................................................................................................... 8
Additional Tools ...................................................................................................... 9
X-B-0001 - PLC Fundamentals .............................................................................. 11
IO Hardware Isn't Always Needed! ....................................................................... 12
PLC Data Types ................................................................................................... 12
Program Organisation........................................................................................... 18
C-B-0003 - CODESYS Hardware Setup ................................................................. 19
Setting Up Hardware ............................................................................................ 19
Adding A PLC ....................................................................................................... 19
Device Setup ........................................................................................................ 21
Configuration Of Sub-Devices / Fieldbus Networks .............................................. 21
Adding IO.............................................................................................................. 22
Installing New Devices.......................................................................................... 23
C-B-0004 - CODESYS POUs .................................................................................. 24
POU Overview ...................................................................................................... 24
Creating A POU .................................................................................................... 24
Configuring A New POU ....................................................................................... 25
POU Properties..................................................................................................... 26
Key Properties ...................................................................................................... 26
C-B-0005 - CODESYS Tasks .................................................................................. 27
What Is A “Task”? ................................................................................................. 27
Adding A Task To Projects ................................................................................... 27
Configuration Of Tasks ......................................................................................... 28
X-B-0002 - Ladder Logic Introduction .................................................................. 29
What Is Ladder Logic (LAD/LD)? .......................................................................... 29
Key Points ............................................................................................................ 29

Copyright – Alveare Learning / Liam Bee 1


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Strengths .............................................................................................................. 30
Weaknesses ......................................................................................................... 30
Common Instructions & Functions ........................................................................ 31
X-B-0003 - Structured Text Introduction .............................................................. 34
What Is Structured Text (ST/SCL)? ...................................................................... 34
Key Points ............................................................................................................ 34
Strengths .............................................................................................................. 35
Weaknesses ......................................................................................................... 35
Common Instructions & Functions ........................................................................ 35
X-B-0004 - Function Block Diagram Introduction ................................................ 37
What Is Function Block Diagram (FBD)? .............................................................. 37
Key Points ............................................................................................................ 38
Strengths .............................................................................................................. 38
Weaknesses ......................................................................................................... 38
Common Instructions & Functions ........................................................................ 39
C-B-0006 - Sequential Function Chart – CODESYS............................................. 40
What Is Sequential Function Chart (SFC)?........................................................... 40
Key Points ............................................................................................................ 41
Methods ................................................................................................................ 41
Strengths .............................................................................................................. 42
Weaknesses ......................................................................................................... 42
Things To Note ..................................................................................................... 42
C-B-0007 - Continuous Function Chart – CODESYS ........................................... 43
What Is Continuous Function Chart (CFC)? ......................................................... 43
Key Points ............................................................................................................ 44
Modes ................................................................................................................... 44
Strengths .............................................................................................................. 45
Weaknesses ......................................................................................................... 45
Common Instructions & Functions ........................................................................ 46

Copyright – Alveare Learning / Liam Bee 2


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

LC001 - The Basics Of Programming


With Codesys

Course Title The Basics Of Programming With CODESYS


Date Last Updated 23 May 2021
Primary Audience Beginners
Course Type Live - Hosted With Eventbrite
 General Modules
o X-B-0001, X-B-0002, X-B-0003, X-B-0004
 CODESYS Modules
Module References o C-B-0001, C-B-0002, C-B-0003, C-B-0004,
C-B-0005, C-B-0006, C-B-0007
o Project: C-B-9001

Course Outline
LC001 - The Basics Of PLC Programming With Codesys takes a student through the
basic principles of a PLC, CODESYS as a development platform and then looks at
key components to get started with such as POUs, Tasks and adding a Soft PLC to
a project to work with.

This course also features a simulated section where the course will explore multiple
languages that all control a simulated chemical make-up system.

Copyright – Alveare Learning / Liam Bee 3


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Story Board
When this material is taught live, the below story board outlays the lesson plan

Copyright – Alveare Learning / Liam Bee 4


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0001 - Introduction To CODESYS

About CODESYS
CODESYS is a “Free” development platform for a multitude of different devices.
Although CODESYS has been around for quite some time, it is still yet to receive the
recognition it deserves as a serious development platform, despite being the choice
platform for plenty of companies (some major companies too such as Schneider &
ABB)

Versions
CODESYS has two different major releases, version 2.3 and version 3.5. The
differences between these two versions are quite substantial and 3.5 vastly improved
on its previous version.
CODESYS as we see it now is version 3.5 + any service packs that are installed.
Most of the courses supplied by Alveare Learning will be built on Version 3.5 +
SP16

Downloading CODESYS
Codesys can be downloaded by visiting the 3S CODESYS market place -
https://store.codesys.com/

Copyright – Alveare Learning / Liam Bee 5


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Why Choose CODESYS?


CODESYS offers something that almost no other IDE in the PLC Automation space
offers… Diversity amongst Hardware. Whilst CODESYS can be wrapped up in
different manufacturer implementations, the base is always the same. This means
that in most cases, you can export from once instance of CODESYS and import into
another instance, with different hardware, and the import will be successful. This
approach opens the door to common and standard approaches between different
manufacturers equipment, helping to keep code consistent even when the Hardware
is not.
CODESYS also offers a great simulation solution, especially considering it is Free.
By using the Win V3 Soft PLC, you can check nearly all of the logic you can without
having to purchase licences or hardware. This point alone makes CODESYS really
attractive for development and learning purposes

Copyright – Alveare Learning / Liam Bee 6


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0002 - CODESYS Environment


Version
The version Of CODESYS documented here is CODESYS 3.5 + SP16 + Patch 2

CODESYS Environment Overview


The Development Environment in CODESYS is split into different Tabs and
Windows, most of which can be collapsed or “pulled out” to float or be placed in
some other location
This style of IDE is familiar to most developers that use multiple software
environments, its similar to Siemens TIA Portal and the concept is the same as
Visual Studio, although laid out differently.

Copyright – Alveare Learning / Liam Bee 7


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Key Areas
Below details the key areas of the development environment

Legend

1. Project Explorer (Device View / POU View)


1.1. This is the main navigation area, its where POUs are created and opened for
editing / monitoring
2. Development Area
2.1. The “Program Here” area. What is displayed here depends on what POU is
open (or any other screen)
3. Tab Navigation
3.1. Almost everything in CODESYS opens in Tabs, they will be displayed along
the top here (with a drop down on the furthest right). Changing the tab
changes the Development Area view
4. Information Bar
4.1. Similar to other IDEs, CODESYS utilizes an information bar at the bottom.
This displays a multitude of different information depending on what is
currently happening. Items such as compilation warnings, cross references
and variable watches are all displayed here. You’ll notice that this also has a
tabbed approach, these tabs can be pulled out and re-organized like other
tabs / windows can be too.
5. Toolbox
5.1. This area contains relative tools available for the type of tab in the
Development Area. Sometimes this will be blank if the particular tab has no
tools available. In this area, instructions for programming are displayed

Copyright – Alveare Learning / Liam Bee 8


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Additional Tools
CODESYS is packed full of different tools and utilities, however, there are some that
aren’t immediately obvious that shouldn’t be overlooked

 Simulation Mode
o Located in the Online menu

The Simulation mode is a great way to simply get started without any hardware.
Once simulation is on, you’ll see a simulation icon on the bottom status bar of the
CODESYS Editor that looks like this

Copyright – Alveare Learning / Liam Bee 9


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Note
The version Of CODESYS documented here is CODESYS 3.5 + SP16 + Patch 2

There is an important difference between Simulation Mode and the built in Soft
PLC. The biggest difference is that the Soft PLC is network capable, meaning
protocols such as Modbus and Profinet can be tested. This is not the case for
Simulation Mode

 Communication Settings
o This is accessible via double clicking the Device from the Project
Explorer (Area 1 above)
o Once in this view, you can connect to different PLC / Devices that fit
your application configuration
o You’ll need to do this if you ever connect to physical hardware for a
live project, or if you want to connect to the Soft PLC in windows.

The above shows connecting CODESYS to a Soft PLC running on a Desktop


Machine via the Localhost IP

Copyright – Alveare Learning / Liam Bee 10


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

X-B-0001 - PLC Fundamentals


A PLC is a more than just "Programmable Logic Control", there's a little bit more to
consider.

A PLC needs to acquire data, process that data and then respond via it's defined
logic. Above we can see that in action. The CPU is on the left, followed by an Input
Card and then an Output Card.

This type of setup is a modular setup, meaning more cards can be added, in
different orders, or even no cards can be included if the inputs /outputs are coming
from a network for example.

The above diagram shows that the PLC is receiving data from the two valves and the
pump. This data could be feedback in for form of Digital signals to say when the
pump is running and the valves are closed / open.

Once the Input Card processes the signal received, it is passed to the CPU where we
can interact with it via our logical program.

The logic is stored and executed in the CPU (most commonly referred to as "The
PLC"). The CPU is the brains of the operation, receiving data, processing that data
and passing outputs back to the Output Cards.

When the Output Cards receive a command from the CPU, they close relays /
transistors to create electrical signals that are then used to signal to valves / pumps
to operate correctly.

Copyright – Alveare Learning / Liam Bee 11


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

IO Hardware Isn't Always Needed!


Not all PLCs need physical I/O Cards! Depending on the configuration of the project,
information may come directly from networked assets such as variable speed pumps
(VSDs) as shown below

In this example, no I/O cards are required because the information is relayed to the
CPU directly via the Ethernet Network that the VSDs are connected to.

This type of configuration is important to remember as sometimes, especially if your


physical hardware you need to control is far away from your PLC, you may want to
go with Remote IO. RIO (Remote IO) comes in many shapes and forms, but
essentially consists of some sort of Gateway that is connected to local IO. This
gateway then converts the IO connected to it to a digitized message that can be sent
to the CPU via a network. This type of configuration allows a single CPU to be
connected to many IO points that are not local to the CPU's physical location

PLC Data Types


What Is A Data Type?

A data type is as simple as the name suggests, its a Type that tells the PLC program
what sort of information will be stored in the Variable. These different types have

Copyright – Alveare Learning / Liam Bee 12


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

different Lengths and consume more memory. Some Types take up different
amounts of memory depending on what they are placed between. Some Types can
even be converted into other Types too.

These Data Types are the bread and butter of programming, you'll get nothing done
without them!

PLC Data Type Length (Size) & Common Uses

Each Data Type has a specified size and also a "Common" use, although this can vary
depending on the project and what you are actually doing with the data.

Lets take a look at the sizes and some examples of uses

Boolean (True / False)

The "Boolean" Data Type is probably the most common type that gets used, you'll
use these for all of your TRUE / FALSE conditions. They're as simple as a data type
typically gets, however the memory they consume can be a little complicated to get
your head around

The above tells us that the only options we have for this Type is "True or False". The
little numbered squares show how many "Bits" in a "Byte" of memory the "Boolean"
Type uses. In this case, it uses 1 Bit of the Byte.

However, if we had two Booleans next to each other, our memory consumption
would still be 1 Byte, but 2 Bits in that Byte would be used. This continues until we've
filled all Bits in our Byte. This means that 8 Booleans = 1 Byte and also anything
between 1 and 8 still equals 1 Byte when they are not consecutively followed by
more Booleans!. So be careful if you are scattering Booleans between other Types as
this can increase the memory consumption quite considerably!

Byte (Hexadecimal)

The "Byte" Data Type is also a heavily used Data Type, especially useful for things
such as Statuses and collective "bit" data. This is because a "Byte" is a numerical
value that directly represents the bits that are turned on.

A Byte cannot be anything else other than a Byte, two next to each other would
simply be 2 Bytes next to each other and nothing else.

Copyright – Alveare Learning / Liam Bee 13


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

A Byte is made up of 8 bits and when these bits are switched on and off, there are up
to 256 combinations using Binary. This means that a Byte has an applicable
numerical range of 0 - 255.

Bytes, due to the nature of the Data Type, can be easily converted between
individual Booleans and a complete byte. For example, in the PLC you may have a
Byte named as "MyByte1", but you want to access the 6th bit of that Byte as you are
storing Boolean values packed together in a Byte. You can do this by using
MyByte1.%X6 (Siemens). In other editors, you should be able to access with
MyByte1.6

The approach of addressing Boolean Values in a Byte Type is not a new concept, its
exactly how I/O Cards work, you'll notice that IO nearly always comes with channels
that are in multiples of 8, and in nearly all cases you can address an entire 8 channel
IO card with a single Byte (however, for simplicity and ease of use, I don't actually
recommend that this is done)

Word (Hexadecimal)

The "Word" Data Type is simply an extended version of a Byte. It is double the
length, so holds 16 bits of data

Just like the Byte, this Data Type is typically used for statuses and Boolean
compacting into "Words". You'll often see in documentation for VSD drives (and
other networked devices) the terminology "Status Word" and "Control Word".
These Words refer to the fact that they are Word Types and all of the current states
are stored in these 16 bits. In modern and newer applications, the "Control Word"
may contain more than just one Word, but this is where the name comes from.

Note that due to the longer length of the Data Type, the numerical range has
increased to 65535, which is the number of available patterns that can be made with
16 bits (2 Bytes)

DWord (Hexadecimal)

The "DWord" Data Type is an extension again of the Word, meaning that 4 Bytes are
used, or 2 Words to create a "Double Word".

Copyright – Alveare Learning / Liam Bee 14


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

This is the largest Data Type in the Hexadecimal range for a 32-bit PLC. Once again,
it follows the same theme for usage as the Byte and Word.

How Hexadecimal Values Are Determined By "Bits"


Something that isn't immediately obvious to beginners is exactly HOW turning on
Bit "3" results in a value of 8. This is down to how Binary works . I purposely left this
until after explaining the Data Types above to remove any confusion down the line.

Take a look at the below

The "Representative Value" is the amount that Bit is worth when it is turned ON
(True). So if the following Byte value was 155, the bits would have to look like this:

The summated "Bits" correspond to the current value, so when Bits 7, 4, 3, 1 & 0 are
all True, the representative value for this = 155.

This also works the other way around, because the value is 155, we know that Bits 7,
4, 3, 1 & 0 are True, meaning we don't need to test all 5 as Boolean variables, we can
simply check the value as a direct Byte by checking if the Byte = 155.

SInt / USInt (Decimal)

This is the "Small Integer" Data Type.

The "Sint" and "USint" Data Types are the smallest numerical Data Types available.
They are the same size as a Byte and therefore can only hold 8 bits of information.
The key difference between the two is that the "U" in USint stands for "Unsigned",
this means the number can only be positive, where as the Sint Data Type is "Signed"
and can be negative.

Copyright – Alveare Learning / Liam Bee 15


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Notice how the USint data type has exactly the same numerical range as the Byte
data type, but the Sint data type has the same spread, but different start and end
ranges. This is because "Signed" types use the last Bit to determine if a value is
positive or negative. If the last bit is TRUE (Bit 7 in this case), then the value is
negative.

Int / UInt (Decimal)

This is the "Integer" Data Type

The same principles apply for Int & UInt as they do for SInt & USInt. The Int / UInt
data type is 2 Bytes long, like the Word type.

The above shows us the same setup that we've seen already with SInt, the last Bit
(15) is used for signage and therefore reduces the maximum available positive value
so that the negative range is also accessible.

You'll also notice that the Unsigned range is the same as the Word Data Type, this is
because they use the same amount of Bits.

Copyright – Alveare Learning / Liam Bee 16


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

DInt / UDint (Decimal)

This is the "Double Integer" Data Type

Just as we've seen with the Double Word, its a 4 Byte Type and capable of a much
higher value range

Again, the last Bit of the Data for the DInt is used to show signage.

Real (32Bit - Floating Point)

The "Real" Data Type is a special and commonly used type. You'll see and use this
type often when working with things such as Scaled Values, as a degree of accuracy
is required.

Unlike the above Data Types, the Real Type is a little more complicated under the
hood. Whilst it uses 4 Bytes, the degree of accuracy can change depending the
values being worked with.

The range of a Real is enormous due a system that's used by the PLC to represent
additional value against Bits. In the above image, the Blue Bits are used to hold
decimal information and the Yellow Bits are used to hold the Integer part.

At this point, a Wiki on floating point does a much better job of explaining how this
type works than I can - You can find it here

However, one thing that you should consider is that Reals are subject to rounding in
almost 100% of cases, not all numbers are actually obtainable and as numbers get
further away from 0, the accuracy drops away exponentially. These traits can cause
issues with logic if you are not careful.

Copyright – Alveare Learning / Liam Bee 17


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Key Notes On Data Types


All of the Data Types above serve as the core types for a 32-bit PLC. There are 64-
bit PLCs out there now days which also have "L" versions of Data Types, these
simply are two "D" types together.

One thing to realise is that between the above data types, every type that is used
in any other Type is referenced above. Every single type, custom or standard must
consist of Bytes, Words or DWords. In essence, every type is made up of Bytes.

Program Organisation
Almost every PLC has some form of Task Management. There are two common
types of this in two different forms, but they both share a commonality - Objects.

1. Task Management

a. Some PLCs use Tasks to organise the execution of code. These are Tasks
that manage Entry Points into a program. Once the Entry Point has
started, it continues until the code is finished. The Task may then call the
same, or a different Entry Point depending on what is happening.

2. Hierarchical Call Stack

a. A Parent Object (or program block) is predefined within the editor and
acts as an Entry Point. This then calls Child Objects as and when they
are scanned in the Parent’s logic. Different Parent Objects can be utilized
(together or independently) to create interrupts and segregated tasks

In both of the above scenarios, the Entry Points will still call other Objects such as
Function Blocks, Functions and other technical objects that your PLC program may
be using

Once all of these aspects come together, this forms the PLC programs we use to
control our physical assets.

Copyright – Alveare Learning / Liam Bee 18


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0003 - CODESYS Hardware Setup


Setting Up Hardware
There are two main areas to setting up Hardware in CODESYS (and almost ever
other development environment)
1. Add The Device
2. Configure the Device

Adding A PLC
Win V3 PLC
In the Examples below, we’re adding a Win V3 PLC. This is the Soft PLC used by
Codesys that allows you to test your program functionality with a generic PLC
that’s simulated inside your Windows Computer.

1. Right-click your project from the Devices pane on the left of CODESYS
(Sometimes referred to as the Project Explorer)
2. Choose Add Device

Copyright – Alveare Learning / Liam Bee 19


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

3. Choose the Device you need from the list available

Note: There may be more in your list if you’ve installed more PLCs in the Device
Repository
4. Click Add Device and the device will be added to your project.
Multiple Devices
More than one device, even of the same type, can be added to a project. Just
remember though, only 1 can be simulated at any one time

Copyright – Alveare Learning / Liam Bee 20


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Device Setup
You can access the device setup, where you can configure items such as the
communication settings, Backup settings, PLC Settings and more, by simply double
clicking the Device in the left-hand side Device pane

Configuration Of Sub-Devices / Fieldbus Networks


Once your device is added, you may want to expand it with IO or network
capabilities. These are added in the same manner as the Device itself, except this
time, you right-click your Device and select New Device

Example: Adding a Fieldbus Network to the Win V3 PLC

Copyright – Alveare Learning / Liam Bee 21


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Adding IO
If you have added a PLC with configurable / expandable IO, you can add IO modules
into the virtual chassis that appears under the compatible device:

If you right-click on the <Empty> module slots and choose Plug Device, you can
pop-up the familiar window that allows you to choose a device.
Once you add the device to the module slot, you are then able to access the devices
mapping facilities by double clicking the module you’ve added your device to as
shown below

Win V3 PLC
You can’t add IO to a Soft PLC

Copyright – Alveare Learning / Liam Bee 22


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Installing New Devices


To install a new device, you’ll need to have the Device Description, which you can
obtain from the manufactures website or the CODESYS Store.
Once you have this, you can navigate to Tools → Device Repository → <Install> and
choose the device descriptions to install them to your CODESYS Instance.

Missing Libraries
Once you’ve added hardware via Device Descriptions, you may have errors about
missing libraries. If you do, double click the error in the information pane at the
bottom of the CODESYS screen. You’ll be taken to the Library Manager where
you can then click the Download Missing Libraries button and finish off your
install

Copyright – Alveare Learning / Liam Bee 23


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0004 - CODESYS POUs


POU Overview
POU stands for Program Organisation Unit. Its an object that holds anything to do
with Program Logic and they can be created with different options and languages.
POU’s are an essential part of a PLC program, they are the building blocks that
create the complete solution.

Creating A POU

1. In the Devices Pane (Project Explorer) on the left hand side, right-click in on
any folder or on the Application Icon
2. Choose Add Object
3. Choose POU…

Copyright – Alveare Learning / Liam Bee 24


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Configuring A New POU

Once a new POU has been added, a new dialogue will appear. This is then used to
configure what type of POU is required
There are a few areas to consider
 Name
o The name you want to give your POU
o Try to keep names meaningful QUARTER_TURN_VALVE_MANAGER
means a lot more than VALVE
 Type
o Program
 This is an Entry Point into your project / solution. Programs are
linked to Tasks and are called cyclically or by other means to
execute their program requirements
o Function Block
 The most common type of re-usable code
 Allows for Instances of the same code, allowing two Function
Blocks to behave the same, but with different data
 Capable of retaining data between Program Cycles
 CODESYS is capable of “OOP” when using function blocks,
this is configured using the additional options when
selecting a Function Block. This is advanced
o Function
 Re-usable code block
 Does not have any memory, it “forgets” values every time it is
used
 Implementation Language
o The language that the POU is written in

Copyright – Alveare Learning / Liam Bee 25


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

POU Properties
By right-clicking on a POU and choosing Properties, you can configure some
additional options

Advanced Options
Some of the options in the Properties dialog are advanced and can fundamentally
change the way the POU behaves. Its highly unlikely that at this stage all options
would be required

Key Properties
 Build
o Exclude From Build
 This is extremely handy for testing purposes. By checking this
box you can exclude the POU from being compiled and
downloaded to the PLC. It’s highlighted green once this is
selected so indication is given that its not being included
o External Implementation - Advanced
 This allows the POU to skip the compiling process and the PLC
links the POU to the process during runtime. This is advanced
and is sometimes required when dynamic information that is
created in Runtime is not available in development time and
causes compilation of the POU to fail
 CFC Execution Order
o This is useful if the POU being developed (which needs to use CFC as
the language) needs to have an explicit flow of data. The Execution
Order can be modified to suit the POUs needs, it can also be applied
to all POUs of this implementation language
 Access Control - Advanced
o Using the CODESYS Administration Utilities, the POU can be
protected against unwanted user interaction
 Bitmap
o Create your own image associated with the object

This can be useful if many different libraries or POUs are in use. Also
helpful for standardisation of POUs from a visual perspective

Copyright – Alveare Learning / Liam Bee 26


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0005 - CODESYS Tasks


What Is A “Task”?
A Task is the equivalent of an Entry Point into the executable runtime of the PLC
program. A Task specifies the trigger point that starts a Program and the
requirements such as the Watchdog time that the program must be completed
within.

Siemens
For people familiar with Siemens, a Task in CODESYS is the equivalent of using
the appropriate OB#

Adding A Task To Projects

1. Right-Click on the Application and choose


Add Object → Task Configuration

Note: This may already exist when some devices are added / Projects are
created
2. Right-Click on the Task Configuration and choose
Add Object → Task
3. A new Task will be created underneath the parent Task Configuration
4. Drag a POU that is a Program (PRG) type onto the Task to add it to the
Task’s execution responsibilities

Copyright – Alveare Learning / Liam Bee 27


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Configuration Of Tasks
By double-clicking on a Task, the Task Configuration will open and options
presented that allow configuration of how the Task operates

 Priority
o Tasks can have different priorities. This means that if a Task is
triggered at the same time as other Task, the one with the lowest
priority will execute first (the lower the number the higher the priority)
 Type
o Cyclic
 Run the attached POU’s cyclically at a declared interval period
o Event
 When a Variable that is specified becomes True, the POU will
execute once. The variable must go False before the POU can
run again
o Freewheeling
 The POU’s specified will run again as soon as all are complete
o Status
 The POU’s specified will run in the same manner as
Freewheeling, but only when the specified Variable is True
 Watchdog
o The Watchdog allows the Task to be given a finite amount of time to
finish a complete execution. This is helpful for safety sensitive
applications.
o Time
 The time required before a Watchdog condition is deemed
erroneous
o Sensitivity
 Essentially the amount of consecutive exceptions of the
watchdog are allowed before an actual watchdog fault occurs.

POU Execution Order


The order that the POUs appear in the Task are the order in which they are
executed. The POUs can be re-ordered in the Task configuration panel

Copyright – Alveare Learning / Liam Bee 28


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

X-B-0002 - Ladder Logic Introduction


What Is Ladder Logic (LAD/LD)?

Example Of Ladder Logic In CODESYS V3.5

Ladder Logic is probably the most common language used in PLC programming,
however that is slowly changing as PLCs become more and more powerful.
Ladder has become so popular due to its reliability, ease of use and its ability to suit
most types of application. It gets its name from the “Ladder” like view it has, although
most development environments no longer join the right hand vertical rail.

Key Points
 Left → Right then Up → Down scan cycle
 Left most vertical rail is the Power Rail - a constant TRUE signal
 Logic is applied along the horizontal “Rung”, ending with a Coil or Instruction
 When monitored, logic flows from the Power Rail to the coils through
instructions and contacts
 Areas of code are segmented into “Networks” or “Rungs”
 Networks usually must start with an instruction or comparator, however some
environments allow exceptions
 Some configurations of Instructions may not be allowed, such as branching
around Timers or Function Blocks, however this changes depending on what
environment the development is in

Copyright – Alveare Learning / Liam Bee 29


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Strengths

Example Of Ladder Logic In Siemens TIA Portal

 Widely accepted as “Industry Standard”


 Easy To Learn
 Similar In Theory To Function Block Diagram
o Some PLC Development Environments can convert between Ladder &
Function Block on the fly
 Use of Networks nicely compartmentalises code sections, allowing for easy
documentation and navigation of code

Weaknesses
 Poor portability between different environments
 Difficult to handle mathematical equations and other numerical scenarios
 Tendency to always be used over other languages for “Familiarity” purposes
 Unable to iterate over loops without JUMP instructions, which can lead to
poorly organised code
 Comments / Documentation usually limited in some fashion due to size of
Network

Copyright – Alveare Learning / Liam Bee 30


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Common Instructions & Functions


 Boolean / Bit Instructions

o Contact
 Test for a TRUE value
o Negated Contact
 Test for a FALSE value
o Coil
 Writes the value of the preceding logic to the variable assigned
 If preceding logic results in a TRUE the coil value is also TRUE
 If preceding logic results in a FALSE the coil value is also
FALSE
o Set Coil
 If preceding logic results in a TRUE the coil variable is set to
TRUE and remains set to TRUE until a Reset Coil sets the
variable value to FALSE
o Reset Coil
 If preceding logic results in a TRUE the coil variable is set to
FALSE

Copyright – Alveare Learning / Liam Bee 31


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

 Comparators

o EQ - Equal
 OUTPUT is TRUE when INPUT 1 and INPUT 2 are equal
o NE - NOT Equal
 OUTPUT is TRUE when INPUT 1 and INPUT 2 are NOT equal
o LT - Less Than (<)
 OUTPUT is TRUE when INPUT 1 is less than INPUT 2
o GT - Greater Than (>)
 OUTPUT is TRUE when INPUT 1 is greater than INPUT 2
o LE - Less Than Or Equal To (<=)
 OUTPUT is TRUE when INPUT 1 is less than or equal to
INPUT 2
o GE - Greater Than Or Equal To (>=)
 OUTPUT is TRUE when INPUT 1 is greater than or equal to
INPUT 2

 Math Functions

o Basic mathematical functions, some PLCs may have other math


functions pre-built.

Copyright – Alveare Learning / Liam Bee 32


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

 Other Key Instructions


o MOVE
 Moves a value from one variable to another variable. Both
Variables must be the same type
o AND / OR
 Due to the fact Ladder is great and providing Boolean logic
solutions, AND & OR instructions are rarely used. However,
they are great for performing Bitwise operations such as
Masking. This is where you pass two binary values such as a
Word to a AND/OR and they are “merged” together on a ruleset
to create a new value, switching off and on individual bits based
on the values passed and whether a AND or an OR was used.
o JUMP
 NOT RECOMMENDED!
 Allows the code to “Jump” from the JUMP instruction to a label
of the same name.
 This is not recommended as it becomes confusing if too many
are used and can introduce Jitter in the program that's difficult
to find

Copyright – Alveare Learning / Liam Bee 33


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

X-B-0003 - Structured Text


Introduction
What Is Structured Text (ST/SCL)?

Unlike other languages, Structured Text is a purely Text Based language. This
means it can be written in any text editor, easily transported between different
development environments.
Structured Text is quickly becoming a popular language to develop PLC logic with.
This is mainly because of the fact it’s so similar to other text based languages and
also because of the advantages it holds with Loops.

Key Points
 Top → Down Scan Cycle
 Unlike most other PLC Languages, outputs appear before the conditions that
set them
 Does not require intermediate variables in order to process multi-conditional
statements
 Extremely useful with Arrays due to Loop capabilities
 Comments can be much more in depth thanks to free-text comments
 Can be copied and pasted into other editors with relative ease

Copyright – Alveare Learning / Liam Bee 34


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Strengths

Example Of A For Loop In Structured Text

 Capability to loop sections of code, without using JUMP instructions


 Larger instruction set that that of other languages
 Easy to move between different PLCs and different development platforms
 Easy to generate via some other process (Excel for example)
 Similar to other text based languages in computing software

Weaknesses
 Difficult to read unless a format is adopted such as the use of comments and
regions
 Prone to user-error and mistyping
 Lack of support from lower end PLCs and older development platforms
 Monitoring is ugly in nearly all editors

Common Instructions & Functions


“Instructions / Functions”
Structured Text is not like other languages, it doesn’t really have “Instructions” or
“Functions” in the same way ladder does. You can achieve the same behaviour of
course, but you can’t select an EQ function from a toolbox when using Structured
Text.
In some Development Environments, you can drag and drop pre-populated
statements that you then adapt to your own needs.

 Basic Instructions
o Setting A Value
Bool1 := True;
Bool1 := False;

o Notice the :=, this means Set Value, whereas = only means EQ

Copyright – Alveare Learning / Liam Bee 35


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

o Reading A Value
Value := Bool1;

o Note the ;, this must be present at the end of any code that sets a
variable or calls a function.

 Calling A Function / Function Block


//Calling A Function Block
MyFunctionBlock(In_1:= Var_1,
In_2:= Var_2,
Out=> Result);

//Calling A Function
Result := MyFunction(In_1:= Var_1, In_2:= Var_2);

o The => denotes an Output, whereas the := denotes an Input (or InOut)

 Calling A Loop
//Loop Through Array And Check If Any Values Are True
FOR i := 0 TO 9 BY 1 DO
IF Result = FALSE THEN
Result := VarArray[i];
END_IF
END_FOR

o The FOR instruction iterates between specified values (or variables)


and performs the same piece of code each iteration. In the example
above, the VarArray is having each element of the Array checked to
see if it is true.

 Math Functions
Result := ((Var_1 + Var_2) / Var_3) * Var_4;

o Math Functions in ST is far easier than any other language as no


objects need to be dropped and no intermediate variables are required
either. Be careful though, debugging large equations can be tricky
when written like this

More ST
There are a lot more ST examples and instructions that can be given and
explained. It warrants an entire course rather than a simple module

Copyright – Alveare Learning / Liam Bee 36


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

X-B-0004 - Function Block Diagram


Introduction
What Is Function Block Diagram (FBD)?

Example Of Function Block Diagram In Codesys

Similar to Ladder Logic, Function Block Diagram is usually represented in


“Networks”, which containerises code sections. FBD tends to span more vertically
than it does horizontally due to the nature of the graphical design of the language. As
shown in the example above, FBD excels in displaying how logic comes together to
form a common output or result.

Copyright – Alveare Learning / Liam Bee 37


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Key Points
 All Block Inputs → Block Outputs Scan Cycle
 Areas of code are segmented into “Networks” or “Rungs”
 All “Instructions” are Function Blocks / Functions (or are at least represented
in that manner)

Strengths

 Very graphical, logic flow is very easy to follow when monitoring


 Inputs to Function Blocks can be outputs of other Function Blocks. This is
helpful when building complicated networks of logic.
 Easy to read in “Offline”. The focus is on logic flow rather than actual
functionality of each block

Weaknesses
 Lines cannot be crossed, resulting in variables being used to “bridge the
gaps” (as seen above with OSR.Q)
 Some unnecessary & functions are required sometimes due to the inability to
cross lines
 Networks can become massive and long vertically if a lot of & / OR blocks are
used in one network
Conversion To Ladder
Some PLC environments can convert freely between Ladder and Function Block
Diagram as their representation always compiles to the same base code.

Copyright – Alveare Learning / Liam Bee 38


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Common Instructions & Functions


 Boolean / Bit Instructions

o AND and OR instructions are the most commonly used “Boolean”


instructions in FBD. Some PLCs also allow for XOR and some also
allowed other gates such as NOR (however many don’t since inputs
can be negated)
o As with Ladder, Set and Resets can be used on output assignments

Other Aspects
Nearly all other instructions / functions in FBD are the same as Ladder Logic. The
only difference is how it is graphically represented

Copyright – Alveare Learning / Liam Bee 39


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0006 - Sequential Function Chart –


CODESYS
What Is Sequential Function Chart (SFC)?

Sequential Function Chart is different from other languages, in that it isn’t really a
“Language” in its own right. SFC is still capable of controlling basic functions such as
setting a Boolean value to TRUE or FALSE, but it falls short in being able to do much
else without employing some special measures.
SFC is best at one very particular type of logic control… Sequences. The sequence
flow employed by SFC is simple, STEP TRANSITION JUMP. Steps must be
preceded by a transition and followed by a transition. Transitions must be Boolean
results, of which a TRUE result “opens the gate” and allows the sequence to
transition to the next step.
A Jump will be at the end of the sequence, returning to a known step so that the
sequence can start again. Jumps can also be utilised elsewhere to jump transitions
to other steps.

Copyright – Alveare Learning / Liam Bee 40


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Key Points
 Top → Down Scan Cycle
 Very structured sequence pattern of Steps & Transitions
 Capable of basic Boolean control by using Action Associations
 Steps are capable of calling Entry, Exit and Active Step Logic, in any
language as an Action
o This particular feature makes CODESYS' implementation of SFC
extremely powerful

Methods
There are two different methods to using SFC, they can be used in tandem or one
can be picked over the other
1. Action Associations

a. Using Qualifiers, different actions can be associated against a defined


variable. For example, the Qualifier N = Non-Stored and has the affect
of writing TRUE in the associated variable whilst the step is active.
b. You can find a full list of the Qualifiers for Actions in SFC here -
https://help.codesys.com/api-
content/2/codesys/3.5.13.0/en/_cds_sfc_action_qualifier/

2. Step Actions

a. Step actions allow the Parent SFC Object to create child Actions that
are then associated with a step. These actions can be implemented in
any language and share the variables from their parent. The available
action types are:
b. Entry Action - When entering the step, the assigned action will run
once (shown by an E in the bottom left of the step)
c. Step Action - When the step is active, the assigned action will run
once per scan until the step is no longer active (shown by a “folded
corner” in the top right of the step)
d. Exit Action - When exiting the step, the assigned action will run once
(shown by an X in the bottom right of the step)

Copyright – Alveare Learning / Liam Bee 41


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Strengths
 Built for sequences
 Steps able to call any implementation language
 Actions for Steps act as encapsulation, ensuring simplicity in sequence
 Graphical and easy to follow

Weaknesses
 Transitions can become long, which causes long sideways scrolling
 Jumps from parallel branches can cause strange behaviour if not properly
controlled
 Large sequences end up with many child Actions, which can become
confusing without a proper naming convention
 Online edits of the code need to be carefully managed, much more so than
other languages as more elements are involved at once
 Different editors employ different design principles, how CODESYS works
does not necessarily translate to other platforms

Things To Note
Logic Pattern
The SFC design for logic must be Initial Block → Transition (Sequence Start) →
Step → Transition … Jump (End Of Sequence)

Copyright – Alveare Learning / Liam Bee 42


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

C-B-0007 - Continuous Function Chart –


CODESYS
What Is Continuous Function Chart (CFC)?

Example of Non-Page Oriented CFC Diagram

CFC is an adaption of Sequential Function Chart that allows the developer a little
more free-reign in where objects are located and how objects interact with each
other.
Rather than using Transition gates, CFC uses Wires or Flows to move information
around the POU. Inputs and Outputs are assigned using dedicated elements.
There are two view modes in CFC:
 Page Oriented
 Non-Page Oriented
CFC in CODESYS is more popular amongst users who have migrated from
platforms such as PCS7

Copyright – Alveare Learning / Liam Bee 43


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Key Points
 Scan Cycle determined by Flow of POU
 Two types of modes - Page driven and non-page driven
 Flow mode can be auto or explicitly defined
 Highly visual, great for high-level print outs
 Offers some functionality that is not available in other languages such as
“breaking out” of structures and variable lists into easy to use, on the fly
interfaces

Modes
When a POU is being added and the implementation language is being selected,
CFC has two options

Page Oriented

 When using the CFC (page oriented), inputs are placed on the left and
outputs are placed on the right
 This gives the appearance and feel of creating an interface for the logic in the
center
 This also makes things easier to read
 Pages are listed on the left of the development pane. Each page can be given
a name
 Variables are shared between pages, allowing data to be passed between
pages from the Output of page 1 to the input of page 2 for examples.

Copyright – Alveare Learning / Liam Bee 44


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Non-Page Oriented

 Input and Output elements can be placed anywhere as structure is not


enforced
 The POU can grow vertically and horizontally (within limits) allowing for larger
diagrams

Strengths
 Very visual, useful for offline viewing / printing
 Great for high-level flow control
 Useful for I/O to Variable mapping where I/O is conditioned

Weaknesses
 Tedious to work with due to lines automatically routing
 Slow to program due to Input / Output Elements needing placing all the time
and “Click & Drag” style linking
 Can become extremely messy easily
 If “Flow Mode” is set to explicit and is not monitored carefully, the execution of
blocks may not be in the order expected and lead to unexpected results.

Copyright – Alveare Learning / Liam Bee 45


www.alvearelearning.co.uk
Alveare Learning LC001 Liam Bee

Common Instructions & Functions


Note
CFC does not have any “Instructions” or “Functions” of its own. It simply uses the
Box object that can be used to create instructions as found in other languages.
There are some elements in the toolbox for CFC that are worth mentioning though

 Composer / Selector

o The above is the Selector which allows a data type or Variable List to
be wired to it and then each element in that data set will appear as an
output of the Selector. This is great for providing on the fly interfaces
to parts of data sets. In other languages this can be tedious.
o The Composer is simply the reverse of the above, where many input
elements are brought together and placed in an output defined as a
data type.
Note: the Composer requires the connected output to be instantiated,
this means Global Variable Lists cannot be used, but a UDT (User
Defined Type) inside a Global Variable List can be used as it’s
instantiated in the variable list.

Copyright – Alveare Learning / Liam Bee 46


www.alvearelearning.co.uk

You might also like