Analog Input Module

This example project utilizes the four channel Analog 0-20mA Current input module- P2-04AD-1.

This project can also be easily modified to be used with an eight (8) channel analog input module, the P2-08AD-1.

Datasheets, specifications and setup for the module can be found here- Current Input

Introduction

The P2-04AD-1 Current Analog Input module provides four channels for receiving 0–20 mA signals for use with the P2CDS-622 system.

This module does not require any configuration of the input signal channels, i.e. each channels is fixed to a 0-20mA topology.

The programming language chosen for this project will be “Structured Text” and will be a very basic program that reads the mA Current value and sets some alarm variables based on the values.

The main purpose for this project is to introduce you to an Analog Input type modules and the associated configuration data flow and status signals.

System Requirements and Installation

This section requires the following:
  • P2CDS-622 CPU

  • P2000 Base (any variant)

  • P2000 PowerSupply (any variant)

  • P2-04AD-1 Current Module (or -08)

  • Latest version of CODESYS installed on Host PC

  • Familiarity with the “Structured Text” programming language.

Note

If Ethernet ports are not configured, see Refer to the Ethernet section.

Recommendation for Data Protection

In order to minimize the risk of data security breaches, we recommend the following organizational and technical measures for the system that will run your applications:

  • Whenever possible, avoid exposing PLCs and controller networks to public networks and Internet.

  • Use additional data link layers for protection, such as a VPN for remote access, and install firewall mechanisms.

  • Restrict access to authorized persons only, and change any existing default passwords during the initial commissioning, and change them regularly.

If you want to publish a web visualization, then we strongly recommend utilizing HTTPS and assigning simple password protection to prevent access to the functionality of your PLC over the Internet.

Help

Help is provided in the Help menu, as well as the context-sensitive <F1> key when in the IDE whether a project is open or not..

Currently, the web-based online help opens by default. This can be disabled in the CODESYS options so that the offline help (CHM format) is used.

Caution

To do this project, the steps outlined in the previous section Preparation have to be completed successfully.

Create a Project

The project can be made up of many program/design PRG files. These are referred to as Programmable Object Units or POUs.

Start CODESYS and Create a Project

Start CODESYS

(1). Start CODESYS from the Start menu (by default, the path is Programs > CODESYS (version).

You can also click the CODESYS icon that is located on the desktop after installation.

Create a project

(2). Click File > New Project

The example project here will be named CurrentInput

(3). In the New Project window, in the Templates section, select the Standard Project template.

(4). Specify a name and a storage location for the project and click OK

The project opens in the CODESYS Standard Project frame window.

(5). In the Device drop-down location, select the P2CDS622 device.

The next step will be to define the language for the initial program you will create (default name is PLC_PRG). We will rename this file later to my preference of Main.

(6). In the PLC_PRG in drop-down location, select Structured Text (ST). This will be the language we will use for the first program.

Your Project Tree view should look something like below with the Name of your project and the associated Device (P2CDS622) as the target.

alternate text

The default Base (P2-04B) is inserted in the tree with no modules added yet, i.e. <Empty>. If your hardware topology is a different Base, we will change that later to match.

The elements of the project tree can be investigated further by going to the Help section online by pressing <F1> in the device tree area and searching for a specific item of interest.

Rename Design File and Add Fieldbusses

It is not required, but we will change the name of the PLC_PRG to Main.

(7). Right Click on PLC_PRG and in the drop-down select Properties. In the Common dialog box, rename PLC_PRG to Main. Click OK.

(8). A dialog box will prompt regarding the name change to be applied over the entire project. Click Yes.

Another dialog box entitled Refactoring will pop up displaying all the areas in the project where this new name will be applied. Click OK.

(9). If a Fieldbus (Modbus or EtherNet/IP) was to be used, this is the point where you would add these. We do NOT require a Fieldbus for this project.

Refer to section Fieldbus for more information if desired.

Add Fieldbus

(Not applicable for this project. Refer to section Ethernet for more information if desired. )

Change Base Type (if req’d)

If you are using a Base topology other than the P2-04B, you can change it now.

(10). In the device tree, highlight with a single click the Base (P2-04B) element, then Right-click. Select Plug Device.

The Plug Device dialogue box should appear.

(11). Under the Miscellaneous name, find the Base unit device you want and double-click. Close the box.

The updated Base should be showing.

Add the IO Module P2-04AD-1

The IO Module will be pulled from the Device Library and inserted into Slot 1 of the Base (P2-04B).

(12). In the device tree, below the Base (P2-04B) element, in the top slot (Slot 1), highlight it with a single click, then Right-click. Select Plug Device.

The Plug Device dialogue box should appear.

(13). Under the Miscellaneous name, find the P2-04AD-1 device and double-click on it. Close the box.

The module should now appear in the first Slot of the Base. Rename the current “Module” to “Slot1”.

(14). Right Click on Module and in the drop-down select Properties. In the Common dialog box, rename Module to Slot1. Click OK. Save the project.

Write the Control Program

This simple program will read the Current input channels and based on certain values will illuminate ac certain LED that will be emulated in the visualization tool “WebVisu”.

(15). Double-click on Main to view the program file.

All POUs are comprised of two sections, the Declaration part that on the top defines the variables and their associated Data Types and the Implementation section where the code is entered.

alternate text

A View of the Module

The parameters that require configuration and the associated status signals are defined in the IO Module Configuration section (P2-04AD-1)- Configuration

Parameters

See reference Configuration - P2-04AD-1

IO Mapping

The IO Mapping includes the Input Data and the Status signals.

Status

This indicates if the module is currently running.

Information

This gives general information regarding the module like description, revision, etc.

Create the Main Program

This program will first read the module status bits and determine if there are any error conditions.

Then the Current input channels are read and based on the Current range, an actionable step is taken.

In addition, a visualization will be created with some LEDs and some input text boxes to display the input channel current values.

IO Mapping
The Module has the following:
  • Input current- Channels 1 -4

  • Status signals: Channel Range Error and General Module status

(16). Name the Channel Input Data and Status signals as below.

alternate text

Implementation

Enter the following main functional code:

alternate text

Declaration

These are the Variables and their associated Data Types that need to be entered:

alternate text

Tip

To help identify with variables are associated with an IO Module or a Fieldbus, you can append the suffix “_FI” (Field In) or “_FO” (Field output). This proves helpful when debugging your design.

The “Task” Configuration

Another very important area in the Device Tree is the Task Configuration section which defines the way in which the program tasks are defined (which POUs are to run on a scan) and the characteristics of the task (priority, speed etc.).

The online help goes into detail for this area. The defaults are good for this project.

alternate text

Configuring the Module

The only item to configure in the Parameters section is the number of channels to be enabled.

Select the IO Module and in the Parameters > Ch. Select field Select Ch. Select 1-4.

Compile the Project

Next we will compile and build the project to download to the P2CDS-622 target.

(17). Select from the drop-down menus- Build > Generate Code.

Sensor/Input Wiring

The P2-04AD-1 is an input only, Current mode device. The inputs are driven by a 0-20mA source.

The resolution of the module is 16 bits, so the conversion counts will range from 0 - 65535d.

The following specification document goes into greater detail and shows the wiring requirements P2-04AD-1

Connect to PLC and Download

The project needs to be downloaded to the target and until now, we have not connected to it.

(18). Physically connect the Host PC to one of the Ethernet ports on the P2CDS-622.

Configure Connection Channel to PLC

(19). In the Device Tree, Double-click on the Device(P2CDS622).

A Device tab should appear with the first tab on the left selected entitled Communication Settings selected. Now we need to scan the network to find our device.

(20). In the Communication Settings section, click on the Scan network.

A dialog box should pop up with all the CODESYS PLCs available to connect to on this network.

Note

If you are unsure which CPU unit is the one you want to connect to, on the right side is a Wink select box. Click on this and this P2CDS-622 front panel RUN light should start to flash for 5 seconds.

(21). Select the applicable P2CDS-622 unit and Click OK.

(22). Toggle the Run/Stop switch.

Now the following should be visible- both the Gateway and the P2CDS-622 have the green indicators present indicating the IDE is connected to the P2CDS-622.

alternate text

Download Application to PLC

(23). Click Online > Login.

A dialog opens up if there is an existing application on the PLC, if so Click Yes to continue.

Successful Download. Ready to Run

The system should show green highlighted Device and Application as shown below:

The alternate text icon indicates connection is active (but program is not executing).

alternate text

Run and Watch the Application

This section will start the application running and using the debugger features, allow a user to watch the variables in real time, set breakpoints and force variables to a specific value.

Start the Application

(24). Click Debug > Start.

The IDE should now be showing a green “Run” in the lower portion of the status area. Also, the Main Task has the running icon alternate text next to it.

Watch the Application

There are several ways to monitor the variables of the application program and to influence them in the watch view: 1. Online views of individual POUs 2. Write and force variable values 3. Defined variable lists in separate watch lists

Online View of the POU

This view of a POU shows the current values of the watchable expressions contained in it.

(25). To open the online view, in the device tree double-click Main, or select it and in the context menu click Edit Object.

In the bottom part of the view, you see the lines of code as specified in offline mode. They are supplemented by the little inline monitoring views after each variable which show the current value.

In the top part, a table shows the watchable expressions of the POU. This means the corresponding variables with Type and current Value.

alternate text

Writing and Forcing Variables

You can write or force a Prepared Value in the PLC’s variable. The dialog is used to prepare or set a value that will be used when a force command of that value is initiated.

For example for the variable Ch1Current_FI, we want to write or force this variable to 50000d (decimal),

(26). In the Prepared Value column in the top section of the applicable program for the variable of interest e.g. Ch1Current_FI, select the field, press the spacebar to open an input field. Specify an integer value (50000d) and press the enter key or click outside of the field to close the field.

(27). In the menu, click Debug > Write Values or Force Values. You see the corresponding result in the Value column

Caution

Using the Write Value command writes the value for one(1) cycle then releases it (can be overwritten). The Force will always hold the value at that written value and cannot be overwritten until Force is removed. See the associated Online Help for further details.

Using Watch Lists

Watch lists can be used to compile in different lists the expressions from the application to be viewed. This can be useful for debugging purposes when specific variables need to be checked at a glance.

(28). In the menu, click View > Watch - Watch 1. The view opens.

(29). In the Expression column, click in the first row to open an input field. Specify the path for the variable to be watched. We recommend that you use the Input Assistant for this by right-clicking in the blank expression field, and select Input Assistant. Select for our example- Device > Application > Main Ch1TooHigh. Press the enter key to close the input field.

The data type of the variable is configured automatically in the row. Insert more rows for other variables if desired .

alternate text

Debug the Application

One of the most powerful features of CODESYS is its debugging features. You can set multiple breakpoints and use a different step-through commands.

Set Breakpoints

In Online/Login mode (not “Started” yet), you can set breakpoints where any program execution should be halted. When a breakpoint is reached, the program can be processed in steps. At each breakpoint and in each step, you can check the current value of the variables in the watch views.

With the Ch1Current_FI sensor input forced to a 50000 (which is above the SENSOR_MAX threshold), the breakpoint should be hit.

(30). To demonstrate, select “Main” and set the cursor in line 21. In the menu, select Debug > Toggle Breakpoint to insert a Breakpoint where your cursor is.

The breakpoint is displayed in the program.

(31). Click Debug > Start to run the program.

The application will run to line 21 where the breakpoint is and halt. It is currently in the stop status, it will look like:

alternate text

Note

The line where the breakpoint is executed/stops has NOT been executed yet ( Ch1TooHigh flag is still FALSE ).

Stepping Through the Program

Now you can press <F8> repeatedly to execute the Step Into command in the Debug menu and run the program in single-step mode. This also steps into the function block instance.

(32). To skip this function block processing, instead of <F8> you can press <F10> to execute the Step Over command.

The current variable values are displayed at the processing position just reached.

Tip

The debugger can be single stepped also with the use of the Hot Keys that can be found in the following link: Hot Keys

(33). See also the Breakpoints dialog View > Breakpoints The currently defined breakpoints are listed here and can be edited or new ones can be added.

Note

Note that the breakpoint positions are saved even when you log out of the PLC. The next time you log in, they will be displayed as light red markers and can be reactivated.

Execute Code Back to Beginning

So let’s say you hit a breakpoint and are stepping through and want to restart from the beginning of your program while still Online.

(34). Select Online > Reset Warm.

This will take you to the beginning of the program in Stop mode, waiting for you to initiate another Start.

Additional Resources

  • An excellent reference text in general for CODESYS development is entitled “The Book of CODESYS” by Pratt.