PLC Part 2 Intro To CODESYS

Welcome to the next level in our learning series. The subject is an introduction to CODESYS. This is the software that has been selected for this training because it adheres to the industry standard for PLC programing languages. That makes it a great choice when we progress to learning Ladder Diagram later is the series. What is learned here should be re-applicable when programming other types of PLCs.

The prerequisite for this material is our first learning series, PLC Part 1 The Fundamentals. Something else that may help, take a look at the setup for a DIY PLC Training Station. The addressing and I/O mapping referenced in this series are based on the DIY PLC Training Station described. Exercises later in the series are designed to work with the training station described but you can cover a lot of the material using the CODESYS built in simulator if you chose.

Intro to CODESYS

To get an idea what’s covered here, take a peek at the table of contents. You’ll see that the beginning is a discussion on why CODESYS is the software selected for this training. At the end you will be learning about PLC addresses and getting ready to move on to actual programming.

Introduction

To begin learning how to program. we need programming software that is compatible with our PLC and a language must be selected.

There are 5 standard PLC languages to choose from. Ladder Diagram is a popular choice. It was developed to resemble electrical diagrams that technicians and electricians are already familiar with. The intent of this training document is to teach the principles of programming with Ladder Diagram.

There are many PLCs and compatible software from many vendors to chose from. Some more popular than others. A manufacturer’s specific programming software may or may not adhere to the industry standard and learning that system will prepare the student to work, primarily, with devices provided by one manufacturer. Programming software that adheres to the industry standard and works with multiple PLCs would seem ideal. The CODESYS Development System fits that description. It has been chosen for this training because what we learn from CODESYS is most likely re-applicable to multiple PLCs and their compatible software.

The best PLC training classes have a strong hands-on approach. Usually with PLC hardware available in the form of a training Station, with real I/O, to perform lab exercises. In the section DIY PLC Training Station instructions are provided to create a PLC trainer. It is made up of a Soft PLC application, installed on a PC, to serve as the CPU and an inexpensive microcontroller board is connected for I/O.

The CODESYS Development System comes with a built in PLC simulator. Though the exercises are designed to work with the training station, it is an option to perform the exercises in the simulation environment. The difference is, the simulation does not provide real I/O. For a description of how to work the CODESYS simulator see Exercise 5.

Once again, the intent of this documentation is to help people get started programming PLCs with ladder logic. It is not likely that it will make anyone into a CODESYS Guru. There are many features of the CODESYS Development System that are not covered here. However, it is a starting point, and the door is always open to learn more by self study.

Relationship Between Inputs, Logic And Outputs

When we learn the basic parts of a PLC system, we discover there are input and output sections. Switching devices are connected to the input section to feed information to the system. Load devices are connected to the output section to perform actions controlled by the system. What lies in between, processing the information and deciding on the actions, is logic. The logic of course resides in the CPU. It is the loaded application that was programmed by the PLC user.

The interactions between the I/O and the CPU happen on cyclical basis during the PLC Scan. The diagram titled Typical Synchronous PLC Scan illustrates the order of the interactions for most PLCs.

When a PLC is powered on, the CPU processor will perform a start-up status check. If a defect is detected the PLC scan will not start and a warning light or other status indicator will give a fault signal. Assuming the start-up check goes OK then the initial PLC scan will commence.

 Step 1 is to read the status of the connected, real world, inputs. That data will be stored in the memory that is assigned for inputs. Then on to the next step.

Step 2 is to execute the logic that has been written and loaded to the PLC. The input data that was stored in memory, during step 1, is taken into account as the program is interpreted and a decision made for each output. The updated output states are stored in the memory that is assigned for outputs. Then on to the next step.

Step 3 uses the output data that was stored in step 2 to update the real-world outputs that control the connected output devices. Then step 3 is complete.

Some of the processor scan time is dedicated to housekeeping duties but it is the 3-step process that was just described that does the real work. The scan is continuous and repeated every few milliseconds. It happens fast enough that the interaction between inputs and outputs would seem instant as perceived by human senses.

But, none of this can happen without the loaded application that was programmed by the PLC user. To create the application we need to learn a language to program PLCs.

Intro To The CODESYS Development System

The name CODESYS is a contraction for Controller Development System

CODESYS Development System

In general, to learn PLC programming, using the CODESYS Development System is a great way to start. CODESYS is a provider of manufacturer-independent automation software, so it is not limited to just one PLC platform. It is used by multiple PLC vendors. Festo, Eaton, Opto22 and Wago are just a few of the vendors with CODESYS compatible PLCs. Soft PLC runtimes are also available for platforms like Industrial PCs and other programmable automation devices. To see a list of compatible PLCs click here.

CODESYS conforms to IEC 61131-3. That is the industry standard for PLC programming languages. That means standard PLC languages are supported. There are 5 standard languages according to IEC 61131-3, made up of:

3 Graphic Languages

  • Ladder Diagram (LD)
  • Sequential Function Chart (SFC)
  • Function Block Diagram (FBD)

2 Textual Languages

  • Instruction List (IL)
  • Structured Text (ST)

A textual language relies on text like words, punctuation, or numbers. The characters are arranged according to a given syntax to build the program statements and expressions. The syntax is the set of rules that determines what combinations of characters have meaning in the programming language.

Graphic languages provide a more visual interface. Symbols like, lines, blocks and shapes draw a picture of the statements and expressions, instead of relying on text alone.

Since CODESYS supports the standard languages, much of what you learn here can be applied to other types of PLCs. This is certainly the case with the LD language that this training will focus on.

CODESYS Control Win Soft PLC

When you download and install the CODESYS Development System a soft PLC called CODESYS Control Win is included in the bundle. A soft PLC is software you can run on a computer that will function as a PLC processor. The benefit of the soft PLC, when leaning, is that you can run PLC applications that you develop in a real PLC environment without the cost of purchasing a PLC processor. In demo mode, the soft PLC runs for 2-hour intervals, with full features, without a license. After the 2-hour limit the soft PLC must be restarted.

You can learn more about CODESYS by visiting the website.      https://www.codesys.com/

Exercise 1 – Install CODESYS On Your PC

Skip this exercise if you have already setup the training station described in section DIY PLC Training Station.

If the CODESYS soft PLC and development system are not already installed on your PC you will have to download from the CODESYS website. It will be required to select between a 32 bit or 64 bit version. The 64 bit version is not suitable for a 32 bit PC. You will need to know ahead of time, what type of OS is on your PC. If you need to know how to check what version is installed, see the guide Check Properties On Your PC in the Help Section.

Download

Software is available from the CODESYS Store website.

In Canada, USA and Mexico go to  https://us.store.CODESYS.com/

Everywhere else, go to https://store.CODESYS.com/de/

An account is required and must be signed in to download. From the CODESYS Store website. Click on Sign In at the upper right of the screen. This will take you to the customer login screen.

From the customer login screen, you have options:

If you have an account, enter your email and password then sign in.


If you do not have an account, then click to create one as a company or individual.
 

Once registered and signed in you will return to the CODESYS Store page. From here you can select to download the 32 bit or 64 bit version.

When you select a version to download, the licence agreement will appear. After reading the agreement, you must accept the terms to continue with the download. The download location will vary depending on your browser and your chosen setup.

 

Install

From the file’s download location or wherever you chose to move it, open the file to start the installation. Alternately you can double-click it or, if you are not logged in to an administrator account, you may have to select Run as administrator. Follow the prompts to continue the installation.

When you are prompted to select the setup type, select Complete. This installs the full bundle including the CODESYS Control Win soft PLC.

Continue following the prompts to complete the installation. At that point, the necessary CODESYS software will have been installed on your PC.

This concludes Exercise 1.

Exercise 2 – Create New Project

To get a first look at the CODESYS Development System we will describe step by step how to create a new project and save it. Explaining a few things along the way.

If the Development System is installed on your PC, then you can start it:

The diagram titled Initial Display shows how CODESYS is presented, by default, at start-up. Components of the display are labelled in red text.

Menu Bar

The Menu Bar is a familiar convention in windows-based applications. Clicking on the Menu Bar opens dropdown menus for categories: File, Edit, View, Project, Build, Online, Tools, Windows and Help. This provides extensive access to all menu commands.

A good way to get more information on each command is to select a dropdown menu, hover the mouse pointer over a command and press the F1 button on the keyboard. This will open online help for the command you have queried.

Tool Bar

The Tool Bar is another familiar Windows convention. It is made up of symbols that provide quick, click on, access to frequently used menu commands.

A good way to get more information on each symbol is to hover the mouse pointer above until the name of the command appears.

For a more detailed description, hover the mouse pointer over the symbol and press the F1 button on the keyboard. This will open online help for the associated command

Devices View

A window to the left is known as the Devices View. When a new project is created, an hierarchical list is created that shows the devices and objects that make up the project. List items will appear as a tree structure that can be expanded or collapsed.

Toolbox

The toolbox window, to the right, provides the tools for graphical languages. When actively working with a graphical program editor, like ladder, a list of program elements will appear that can be used on a drag and drop basis.

Message Window

The Message Window is towards the bottom of the display, in auto hide mode. The box will open to display relevant errors, warnings and status updates. Message are generated related to search results, cross-references, checks, comparisons or compiling.

Status Bar

The status bar is at the very bottom of the display. It provides information regarding CODESYS current actions. There are indicators to show project status for simulation mode, online, run, stop, how many errors or warning among other things.

Upper Right Corner

To the upper right of the screen are the usual gadgets   performing the normal functions to minimize, maximize, change the size, or exit the application.

Start Page

To the center of the initial display is the Start Page. The Start Page offers the options to open a new project or open an existing project. The existing project could be from listed recent projects, opened from the PLC or select Open Project to access any location available to the PC.

Returning to our objective, to create a new project, we will select New Project from the Start Page.

From the New Project dialog box, different templates are available. For this example, we will select Standard Project. The Standard Project template creates a project with the objects we need to develop a basic PLC application.

This is also where the project is given a name and a location to store the file.

Once the selections have been made, its time to click OK.

The Standard Project template initiates a wizard to assist with project creation. The type of device and PLC language must be selected from the dropdown lists. Select a version of the CODESYS Control Win Soft PLC as the device. Think back to when we installed the CODESYS software. Did you install the 32 bit or 64 bit version. You need to select the device accordingly. If you need to know how to check what version is installed, see the guide Check What Version Of CODESYS Control Win Soft PLC Is Installed in the Help Section.

For this example, the language will be ladder logic.

Once the selections are made clicking OK will create the project.

When the project has been created it will appear as shown in the diagram titled New Project.

To the left in the Devices View, objects related to the project appear in a tree structure.

The Start Page is no longer available in the center of the display. This range will be dedicated as the Workspace when object editing begins.

Workspace And Editor

To get a better look at the Workspace, double click the object titled PLC_PRG (PRG) to open the associated editor.

The diagram titled New Project Ladder Editor shows the project with editors open in the workspace. An area called the Declaration Editor appears in the upper part of the Workspace. This is where variables for PLC_PRG (PRG) will be declared.

The Ladder Editor takes up the bottom area of the Workspace. This is where the ladder programming will happen.

Save Project

To complete the test drive, all that remains is to save the project. A couple of different ways are:

Click the File dropdown menu and select Save Project

Or, click the Save symbol that resembles a floppy disk.
 

This concludes Exercise 2.

Objects

We had a glimpse of the Devices View when we created a new project. In the Devices View we could see project objects arranged in a tree structure.

Let’s take a closer look at the objects of a typical project to understand what they are for.

Project

At the top of the tree there is a folder with the project name. When it is expanded, the project’s child objects come into view.

Device

The Device is a child object of the project. It represents the type of PLC or automation device that the project is configured for. If we were using a PLC from a specific manufacturer, the model number might appear in the label. We are using the CODESYS Control Win soft PLC  loaded on our PC for this example. When it is expanded, child objects of the device come into view.

PLC Logic and Application

The PLC Logic object is a child of the device. The PLC Logic is the programmable branch of the PLC or automation device. So, it is natural that the Application object is immediately below. It is possible to have multiple Application objects but our example just has the one. The Application object groups together components that make up an executable control program that will be complied into downloadable PLC code. When the Application object it is expanded, related child objects come into view.

Library Manager

The Library Manager object is a child of the application. Libraries are files that contain computer code that the computer uses to create the application. The Library Manager can group together multiple libraries. Viewing Library Manager Properties can show all libraries connected to the application. It can also add or delete libraries..

Program Organization Unit (POU)

The POU is a child of the application. The POU is probably the most important object to us for this training. POUs are where the PLC programming happens. A POU can be a program, function, or function block. When a CODESYS standard project is created a program type POU is generated and automatically titled PLC_PRG.

Remember the diagram New Project Ladder Editor from when we were exploring the workspace in the middle of the main display. We opened the editor for the POU named PLC_PRG. It was noted that within the PLC_PRG that there was an area to edit declarations and an area to edit ladder logic. It is true for all POUs that there is an area for declarations and an area for logic programed with the selected PLC language.

The object display identifies the POU type in brackets after the title.

  • (PRG) for Program
  • (FB) for Function Block
  • (FUN) for Function

POUs have different symbols for different languages.

There can be multiple Program Organization Units within an application. For a more complex project it can be beneficial to separate different parts of the logic into several POUs to keep it organized.  For our example we just have the one POU.

Task Configuration and Tasks

The Task Configuration is a child to the application. The POU may be where the logic is, but it is the Task that controls whether the logic is executed and when. Every task is assigned a type that determines when the task is triggered. The different task types are:

  • Cyclic – The task is triggered based on a set time interval. The default interval is 20ms. If the set interval is 20 ms then the task is triggered repeatedly every 20ms.
  • Freewheeling – The task is triggered continuously, for every program scan, with no waiting interval.
  • Event – The task is triggered at the rising edge of an assigned variable.
  • Status – The task is triggered when an assigned variable is at the Boolean value, TRUE.

Tasks are also assigned a priority. If 2 tasks, based on their types, were poised to trigger at the same time then the one with the highest priority would go first.

Task are configured to call selected programs (program type POUs). So, when a task is triggered, the configured programs will execute. It is in this manner that the tasks control the order of program execution. To clarify the order of things, a task can call a program, a program can call a program, a function or function block.

An application can have multiple Tasks. Task objects will appear as child objects to the Task Configuration object.

Our example has only one task. It is a cyclic type with an interval of 20ms.

By the way, you may have noticed when our single task (MainTask) is expanded, the program PLC_Prg appears as a child object to the task. You may recall we have already seen a POU named PLC_Prg as a child object to the application. This is not a separate program that happens to have the same name. It shows that the POU is called by the task. When a standard project is created, PLC_PRG is added to the call list by default. When it is visible here, we know that PLC_Prg will execute when the task is triggered.

PLC I/O Configuration

In Exercise 2, we created a project for CODESYS Control Win soft PLC, but that project has no I/O. To make inputs and outputs accessible to the soft PLC, devices will be added to the Devices View. For the next exercise we will be setting up a configuration for a slave device that uses the Modbus protocol. It is compatible with the training station described in the section DIY PLC Training Station.

Exercise 3 – Add Slave Device Modbus Config To Project

Skip this exercise if you have already setup the training station described in the section DIY PLC Training Station.

To begin, we will save our project, “MyFirstProject”, from Exercise 2 with a new name. So, make sure you have saved the project “MyFirstProject”.

With the project “MyFirstProject” open, from the File menu select Save Project As.

Save with new name “Training_Station_Base”.

To begin the I/O configuration, right click the Device object and select Add Device.

The Add Device dialog box will appear. Several types of Fieldbuses are available from here. A fieldbus is a means of communicating with I/O devices. For our exercise we will be using Modbus.

Under the heading Modbus, double click Modbus COM to add the device. This sets up a COM port for Modbus communication. Then click on the X symbol at the upper right of the Add Device dialog box to close.

Next is setting of the COM port parameters. Double click the Modbus_COM object to open the object editor. Select the General tab to set the baud rate to 115200 and the parity to NONE. It is not likely that the slave device is connected at this point, so leave the COM port number at the default (1) for now.

If it so happens, you have already prepared and connected the slave device, the COM port number will be the COM port assigned to connect to the slave device via USB. You can confirm the COM port with the device manager. The method to check the device manger is in the Help Section, titled Check The COM Port In Windows Device Manager.

Right click the Modbus_COM object and select Add Device.

Under the heading Modbus Serial Master double click Modbus Master, Com Port to add the device. This configures the soft PLC as the Master on the assigned COM port. Then click on the X symbol at the upper right of the Add Device dialog box to close.

Right click the Modbus_Master_COM_Port object and select Add Device.

Under the heading Modbus Serial Slave double click Modbus Slave, Com Port to add the device. This configures the slave device and gives access to set up the parameters of the slave device. Then click on the X symbol at the upper right of the Add Device dialog box to close.

Double click the Modbus_Slave_COM object to open the object editor. Select the Modbus Slave Channel and then select Add Channel to set up Channel 0.

Channel 0 will be setup to read the 5 digital inputs from the slave device.

Set the Parameters for Channel 0 as shown below. Then click OK to accept changes.

Select Add Channel to set up Channel 1.

Channel 1 will be setup to read the 6 analog inputs from the slave device.

Set the Parameters for Channel 1 as shown below. Then click OK to accept changes

Select Add Channel to set up Channel 2.

Channel 2 will be setup to write the 4 digital outputs to the slave device

Set the Parameters for Channel 2 as shown below. Then click OK to accept changes.

Select Add Channel to set up Channel 3.

Channel 3 will be setup to write the 3 analog outputs to the slave device

Set the Parameters for Channel 3 as shown below. Then click OK to accept changes.

At this point the slave device configuration is completed. It is a good time to save the project.

This concludes Exercise 3.

Addresses                            

PLCs have addressable memory. When the CPU was introduced as one of the basic parts of a PLC, we learned that the CPU contains the system memory. The memory is broken into different ranges depending on the job it must do. By default, the CODESYS Control Win soft PLC has 4096 bytes for inputs, 4096 bytes for outputs and 4096 bytes of internal memory. To identify different parts of the memory an addressing system is required.

Why do we need to know this? If we want to make use of the information coming from the real-world inputs or send data to control real-world outputs, we need to know what memory addresses are associated with each I/O.

To identify a memory element, the addressing system for CODESYS uses letters to indicate memory range and size. Numbering is used to define the exact word, byte or bit within a memory range. The syntax dictates that all memory addresses have a “percent sign” (%) at the beginning. The characters to define an address must be in a specific order.

Addressing can vary depending on the PLC device and configuration. Particularly when defining addresses at the bit level. Depending on the device, the numbering may identify the byte number or word number and then a bit number separated by a dot.

The examples given here are consistent with the CODESYS Control Win soft PLC with Modbus slave device from section DIY PLC Training Station. Like the one we are learning on.

Bit level addresses identify the byte number and then bit number after the dot.

The diagram titled Bit Level Input Address Example shows a bit level address in the input range of memory where:

  • % (The percent sign) starts off the address
  • I is for input range
  • X is for size of 1 bit
  • 6 is the byte number
  • 2 is the bit number

For W word (16 bit) level addresses the word number is identified.

The diagram titled Word Level Address Examples shows a word level address in the internal memory (M) range of memory where:

  • % (The percent sign) starts off the address
  • M is for internal memory range
  • W is for word sized
  • 6 is the word number

For D double word (32 bit)  level addresses the Dword number is identified.

The diagram titled Word Level Address Examples shows a Dword level address in the internal memory (M) range of memory where:

  • % (The percent sign) starts off the address
  • M is for internal memory range
  • D is for Dword sized
  • 4 is the Dword number

To get used to the idea here are a few more addresses:

  • %IX4.3     – Input Byte 4, Bit 3
  • %QX8.7   – Output Byte 8. Bit 7
  • %MD55    – Memory DWord 55
  • %MW30   Memory Word 30
  • %MB5      – Memory Byte 5

Memory Overlap

Imagine that we were using the DWord address %MD1 for one purpose in the PLC and decided to assign Word address %MW2 to another part of an application. The addresses may be different but they both share a word of memory in the M memory range. Where %MD1 accesses the 3rd and 4th word of the memory range, %MW2 accesses the 3rd word as well. The 3rd word in the memory range is overlapped by both addresses. Data that is intended for one purpose could be corrupting data for another. It is important to assign addresses carefully to avoid these kinds of overlaps.

Imagine that we are using byte addresses %MB3 and %MB2 in a PLC program along with Word address %MW1. All 3 addresses are accessing the 2nd word of the memory range with one byte address pointing at the lower half and the other byte address pointing at the upper half. We have a similar overlap problem to the one we discussed with Words and Dwords.

What if byte address %MB2 and DWord address %MD0 are both used? Would they overlap?

Be mindful that addresses at the bit level can overlap with elements of other sizes just like the overlaps we discussed for Word, DWord and Bytes.

I/O Mapping

To better understand what I/O is assigned to what address, we can view an I/O map of the CODESYS project. The I/O map editor can be accessed by right clicking the Device object and then select Edit I/O Mapping from the drop-down menu.

The I/O map shown in the image titled I/O Map Of Project Training Station Base is from the project created in section DIY PLC Training Station. The map shows all I/O assigned to the project. In this case I/O is connected via a Modbus slave device.

In the description column we see Read Discrete Inputs. The description refers to the action performed by the Modbus protocol that interacts with the slave device. To the left of the description there are 5 bits assigned, %IX0.0  to %IX0.4 for digital inputs.

In similar fashion we see the description Read Input Registers. These are word addresses reserved for analog inputs. There are 6 words assigned, %IW1 to % IW6.

The description Write Multiple Coils refers to digital outputs. 4 bits are assigned, %QX0.0 to %QX0.3.

The description Write Multiple Registers refers to the analog outputs. 3 words are assigned, %QW1 to %QW3.

The diagram titled I/O Connections shows the Arduino slave device pinout with PLC addresses.

Thank you for reading the learning series, PLC Part 2 Intro To CODESYS.

Click on this link to proceed to the next section PLC Part 3 Basic Ladder Diagram.

References – Books And Weblinks

You can explore more on the subjects discussed in PLC Part 2 Intro To CODESYS with these books links.

CODESYS –  Codesys help website –  https://help.codesys.com/

Arduino Uno –  Arduno Learn website –  https://docs.arduino.cc/learn/

Open PLC and Slave Device –  Open PLC Project website –  https://www.openplcproject.com/