Software for Education and
OOP (Object Oriented
CODESYS Made Simple
Want to learn how to use Object Oriented
Programming (OOP) to program a PLC? Tired of watching videos
that move too fast with
an accent you can't understand? Don't want to spend thousands of
dollars to travel hundreds of miles to attend a class? OOP (Object Oriented
Programming) wth Codesys Made Simple will show
you step-by-step how to program a PLC using OOP with Codesys in simple,
easy to understand, examples.
What is a OOP (Object Oriented Programming)?
OOP is a way to package computer code or PLC (Programmable
instructions into reusable blocks or "objects" that can be used
multiple times in a program or in other programs.
OOP is a
way to add structure to a program and keep program sections separate
from one another so that the program doesn't end up with "spaghetti
code" where multiple instructions affecting multiple elements are
all mixed up together. OOP is not a new programming language in-and-of
itself, but rather a way to package code to make programs more
efficient, reusable, and modifiable. High level computer
languages like C and C++ have used OOP for
years, and recently OOP has crept into programming of PLCs.
PLCs can be programmed in a number of
- Ladder Diagrams (LD)
- Function Block Diagrams (FBD)
- Structured Text (ST)
- Instruction Lists (IL)
- Sequential Function Charts (SFC)
- Continuous Function Charts (CFC)
Of these PLC programming
languages, Structured Text is the
most common language in which OOP is used. This
tutorial assumes you know how to launch Codesys, run Codesys programs
in Simulation mode, force inputs to have certain values, and have some
familiarity with Codesys Structured Text programming. If you
new to Structured Text programming with Codesys, or just need a
refresher on it, we recommend our tutorial entitled PLC
Structured Text with Codesys.
What are "Objects"?
The "Objects" of Object
Oriented Programming (OOP) are blocks of code created
from Classes. Classes are like cookie-cutters, or
templates, that are used to produce Objects that
are like the cookies. When an Object is created from a Class
it is called an Instance of the Class. Thus, we say
an Object is instantiated
or created from a Class. As many Objects can be instantiated
from a Class as you want, just like you can make as may
cookies from a cookie cutter as you want. A Class can include
Methods (Subroutines and Functions), Properties,
Methods and Subroutines are groups of code that you can call whenever
you want to get something done, instead of re-writing those
lines of code each time you need that thing done. For
instance, scaling an input, making a repetitive calculation, or
spell-checking a document might be done by a Method. Methods
are Subroutines that return a value to the program that called it.
Properties are characteristics of the Class, like whether the cookies
have chocolate chips or not. Methods can be called to change
the Properties of the Class. So you might write a Method to
add chocolate chips to your cookies, thereby changing the chocolate
chip Property of the Class.
In this tutorial we will
begin by showing step-by-step how to create a
Motor_Starter Class that mimics the common motor
circuit shown below. Then we will use the
Motor_Starter Class and Inheritance to create a Jog_Motor Class and a
Rev_Motor Class. Then other concepts of OOP like passing parameters,
access modifiers, polymorphism, and interfaces will be explained with
Common motor starting
There are two obstacles in using Codesys OOP to design the control for
a machine or system:
1) deciding what should be the Objects
2) learning the syntax that Codesys uses to implement OOP.
Hopefully this tutorial will help overcome these obstacles.
If you aren't designing a system, but rather just troubleshooting or
modify a machine that is controlled by a PLC program that uses OOP
techniques, having a working knowledge of OOP will make the job much
How to use this tutorial:
This tutorial is written by a Professional
Engineer with 14 years industrial experience and 20 years experience
teaching electrical engineering technology at the college level.
If you want to learn how to use CODESYS to program PLC Ladder Diagram
and Function Block programs, you can purchase my PLC Ladder Logic and
Function Block Programming with CODESYS V3.5 tutorial at
. Or if you want to
learn how to use CODESYS to program PLC Instruction List, you can
purchase my PLC Instruction List with CODESYS V3.5 tutorial at
If you want to run and solve Ladder Logic programs without using
CODESYS you can purchase my PLC Simulator 10 at http://www.knoware-online.com/pc-plc.html
This simulator turns your PC into a PLC, has the "look and feel" of
(though not exact), runs and solves Ladder Logic programs that you
write, and allows you to build machines with limit switches, pilot
lights, selector switches, solenoids, and conveyors that move in
response to the ladder programs you write.
This tutorial was written with three
principles in mind:
1) All things can be taught by example (credit Albert
2) Most people learn better by "doing" as opposed to
listening or reading.
3) Sometimes we just need to get a "foothold" on a new
concept so that further explanations make sense.
Read and perform the chapters in this tutorial in the order
which they are presented, since they tend to build on each other.
What you need to use this tutorial:
OOP (Object Oriented
CODESYS Made Simple
3. Adding Properties to a Class
4. Adding Methods to a Class
5. Creating Objects from a Class
7. Passing Parameters to Methods
8. Access Modifiers
6) Create a Class named Rev_Motor
now assume that we need to add a 3 phase induction motor to the
production line that can be jogged and reversed.
below shows a reversing motor starter in relay logic with jog
7 Reversing motor starter with jog capability
than create a Rev_Motor Class from scratch, we can simply inherit
Jog_Motor Class, then add Forward and Reverse inputs, and
and Coil_R outputs. In other words, we can inherit an already
inherited Class. This is like inheriting from your father who inherited
from his father and is sometimes called Deep Inheritance.
create the Rev_Motor Class, right-click on Application, then click on
Add Object>POU to open the Add POU window as shown in Figure 8
below. Enter Rev_Motor for the Name of the POU, select
Block, click the Extends box,
click on the .... box to open the Input Assistant, click on Jog_Motor,
select Structured Text (ST) as the Implementation Language, then click
The Extends box tells
the Rev_Motor Class to inherit the Jog_Motor Class. In a
we are extending the Jog_Motor Class into the Rev_Motor Class.
Figure 8 Creating the
Your screen should now look like Figure 9 below. Notice that
Rev_Motor Class has been added to the program tree on the left side of
the screen, and that it has been labeled Rev_Motor (FB), meaning
Function Block. In other words, the Class you created named
Rev_Motor is simply another user-defined Function Block. Also
notice that a "code window" tab has been added on the right side of the
screen, and the first line indicates that the Rev_Motor Class EXTENDS
the Jog_Motor Class. This indicates that the Rev_Motor Class
inherited the Jog_Motor Class.
7) Add Inputs and Outputs to the Rev_Motor
Add the Forward and Reverse inputs and the Coil_F and Coil_R outputs to
the Rev_Motor Class as shown in Figure 9 below.
9 After the Rev_Motor Class is created