Each network in LD consists on the left side of a network of contacts (contacts are represented by two
parallel lines: | |) which from left to right show the condition "On" or "Off".
These conditions correspond to the Boolean values TRUE and FALSE. A Boolean variable belongs to
each contact. If this variable is TRUE, then the condition is passed on by the connecting line from left
to right, otherwise the right connection receives the value "Out".
Contacts can be connected in parallel, then one of the parallel branches must transmit the value "On"
so that the parallel branch transmits the value "On"; or the contacts are connected in series, then
contacts must transmit the condition "On" so that the last contact transmits the "On" condition. This
therefore corresponds to an electric parallel or series circuit.
A contact can also be negated, recognizable by the slash in the contact symbol: |/|. Then the value of
the line is transmitted if the variable is FALSE.
On the right side of a network in LD there can be any number of so-called coils which are represented
by parentheses:( ). They can only be in parallel. A coil transmits the value of the connections from left
to right and copies it in an appropriate Boolean variable. At the entry line the value ON (corresponds
to the Boolean variable TRUE) or the value OFF (corresponding to FALSE) can be present.
Contacts and coils can also be negated (in the example the contact SWITCH1 and the coil %QX3.0 is
negated). If a coil is negated (recognizable by the slash in the coil symbol: (/)), then it copies the
negated value in the appropriate Boolean variable. If a contact is negated, then it connects through
only if the appropriate Boolean value is FALSE.
Function blocks in the Ladder Diagram
Along with contacts and coils you can also enter function blocks and programs In the network they
must have an input and an output with Boolean values and can be used at the same places as
contacts, that is on the left side of the LD network
Coils can also be defined as set or reset coils. One can recognize a set coil by the "S" in the coil
symbol: (S)) It never writes over the value TRUE in the appropriate Boolean variable. That is, if the
variable was once set at TRUE, then it remains so.
One can recognize a reset coil by the "R" in the coil symbol: (R)) It never writes over the value FALSE
in the appropriate Boolean variable: If the variable has been once set on FALSE, then it remains so.
LD as FBD
When working with LD it is very possible that you will want to use the result of the contact switch for
controlling other POUs. On the one hand you can use the coils to put the result in a global variable
which can then be used in another place. You can, however, also insert the possible call directly into
your LD network. For this you introduce a POU with EN input.
Such POUs are completely normal operands, functions, programs, or function blocks which have an
additional input which is labeled with EN. The EN input is always of the BOOL type and its meaning is:
The POU with EN input is evaluated when EN has the value TRUE.
An EN POU is wired parallel to the coils, whereby the EN input is connected to the connecting line
between the contacts and the coils. If the ON information is transmitted through this line, this POU will
be evaluated completely normally.
Starting from such an EN POU, you can create networks similar to FBD.
Example of a LD network with an EN POU
Debugging, Online Functions...
The Sampling Trace allows you to trace the value sequence of variables, depending upon the so-
called trigger event. This is the rising edge or falling edge of a previously defined Boolean variable
(trigger variable). CoDeSys permits the tracing of up to 20 variables. 500 values can be traced for
The debugging functions of CoDeSys make it easier for you to find errors.
In order to debug, run the command 'Project' 'Options' and in the dialog box that pops up under
Build options select activate option Debugging.
A breakpoint is a place in the program at which the processing is stopped. Thus it is possible to look
at the values of variables at specific places within the program.
Breakpoints can be set in all editors. In the text editors breakpoints are set at line numbers, in FBD
and LD at network numbers, in CFC at POUs and in SFC at steps. No breakpoints can be set in
function block instances.
Single step means:
· In IL: Execute the program until the next CAL, LD or JMP command.
· In ST: Execute the next instruction.
· In FBD, LD: Execute the next network.
· In SFC: Continue the action until the next step.
By proceeding step by step you can check the logical correctness of your program.
If Single cycle has been chosen, then the execution is stopped after each cycle.
Change values online
During operations variables can be set once at a certain value (write value) or also described again
with a certain value after each cycle (force value). In online mode one also can change the variable
value by double click on the value. By that boolean variables change from TRUE to FALSE or the
other way round, for each other types of variables one gets the dialog Write Variable xy, where the
actual value of the variable can be edited.
In Online mode, all displayable variables are read from the controller and displayed in real time. You
will find this display in the declarations and program editor; you can also read out current values of
variables in the watch and receipt manager and in a visualization. If variables from instances of
function blocks are to be monitored, the corresponding instance must first be opened.
In monitoring VAR_IN_OUT variables, the de-referenced value is output.
In monitoring pointers, both the pointer and the de-referenced value are output in the declaration
portion. In the program portion, only the pointer is output:
+ --pointervar = '<'pointervalue'>'
POINTERs in the de-referenced value are also displayed accordingly. With a simple click on the cross
or a double-click on the line, the display is either expanded or truncated.
Example for Monitoring of Pointers
In the implementations, the value of the pointer is displayed. For de-referencing, however, the de-
referenced value is displayed.
Monitoring of ARRAY components: In addition to array components indexed by a constant,
components are also displayed which are indexed by a variable:
anarray = 5
anarray[i] = 1
If the index consists of an expression (e.g. [i+j] or [i+1]), the component can not be displayed.
During the simulation the created PLC program is not processed in the PLC, but rather in the
calculator on which CoDeSys is running. All online functions are available. That allows you to test the
logical correctness of your program without PLC hardware.
Please regard: POUs of external libraries do not run in simulation mode.
The log chronologically records user actions, internal processes, state changes and exceptions during
Online mode processing. It is used for monitoring and for error tracing (see Online Functions).
The standard IEC 61131-3 is an international standard for programming languages of Programmable
The programming languages offered in CoDeSys conform to the requirements of the standard.
According to this standard, a program consists of the following elements:
· Structures (see Data Types)
· Global Variables
The general language elements are described in the sections Identifier, Addresses, Types,
Comments, and Constants.
The processing of a CoDeSys program starts with the special POU PLC_PRG. The POU PLC_PRG
can call other POUs.
We Write a Little Program
Controlling a Traffic Signal Unit...
Let us now start to write a small example program. It is for a simple traffic signal unit which is
supposed to control two traffic signals at an intersection. The red/green phases of both traffic signals
alternate and, in order to avoid accidents, we will insert yellow or yellow/red transitional phases. The
latter will be longer than the former.
In this example you will see how time dependent programs can be shown with the language resources
of the IEC1131-3 standard, how one can edit the different languages of the standard with the help of
CoDeSys, and how one can easily connect them while becoming familiar with the simulation of
Starting always is easy: Start CoDeSys and choose 'File' 'New'.
In the dialog box which appears, the first POU has already been given the default name PLC_PRG.
Keep this name, and the type of POU should definitely be a program. Each project needs a program
with this name. In this case we choose as the language of this POU the Continuous Function Chart
Now create three more objects with the command 'Project' 'Object Add' with the menu bar or with the
context menu (press right mouse button in the Object Organizer). A program in the language
Sequential Function Chart (SFC) named SEQUENCE, a function block in the language Function Block
Diagram (FBD) named TRAFFICSIGNAL, along with a POU WAIT, also of the type function block,
which we want to program as an Instruction List (IL).
What does TRAFFICSIGNAL do?
In the POU TRAFFICSIGNAL we will assign the individual trafficsignal phases to the lights, i.e. we will
make sure that the red light is lit red in the red phase and in the yellow/red phase, the yellow light in
the yellow and yellow/red phases, etc.
What does WAIT do?
In WAIT we will program a simple timer which as input will receive the length of the phase in
milliseconds, and as output will produce TRUE as soon as the time period is finished.
What does SEQUENCE do?
In SEQUENCE all is combined so that the right light lights up at the right time for the desired time
What does PLC_PRG do?
In PLC_PRG the input start signal is connected to the traffic lights' sequence and the "color
instructions" for each lamp are provided as outputs.
Now test your program in simulation mode. Compile ('Project' 'Build') and load ('Online' 'Login') it.
Start the program by 'Online' 'Start', then set variable ON to TRUE, e.g. by a double-click on the
entry "ON" in the input box of the CFC editor. This will mark the variable as prepared to be set to
<TRUE>. Then press <Strg><F7> or command 'Online' 'Write values', to set the value. Now variable
START in ABLAUF (which we had set to TRUE manually in the first extension level of the program)
gets this value by variable ON, which is used in PLC_PRG. This will make run the traffic light cycles.
PLC_PRG has changed to a monitoring window. Click twice on the plus sign in the declaration editor,
the variable display drops down, and you can see the values of the individual variables.
Let us now turn to the POU TRAFFICSIGNAL. In the declaration editor you declare as input variable
(between the keywords VAR_INPUT and END_VAR) a variable named STATUS of the type INT.
STATUS will have four possible conditions, that is one for the TRAFFICSIGNAL phases green, yellow,
Correspondingly our TRAFFICSIGNAL has three outputs, that is RED, YELLOW and GREEN. You
should declare these three variables. Then the declaration part of our function block TRAFFICSIGNAL
will look like this:
Function block TRAFFICSIGNAL, declaration part
Now we determine the values of the output variables depending on the input STATUS of the POU. To
do this go into the body of the POU. Click on the field to the left beside the first network (the gray field
with the number 0001). You have now selected the first network. Choose the menu item 'Insert' 'Box'.
In the first network a box is inserted with the operator AND and two inputs:
Click on the text AND, so that it appears selected and change the text into EQ. Select then for each of
the two inputs the three question marks and overwrite them with "STATUS" respecively "1".
Click now on a place behind the EQ Box. Now the output of the EQ operation is selected. Choose
'Insert' 'Assign'. Change the three question marks ??? to GREEN. You now have created a network
with the following structure:
STATUS is compared with 1, the result is assigned to GREEN. This network thus switches to GREEN
if the preset state value is 1.
For the other TRAFFICSIGNAL colors we need two more networks. To create the first one execute
command 'Insert' 'Network (after)' and insert an EQ-Box like described above. Then select the
output pin of this box and use again command 'Insert' 'Box'. In the new box replace "AND" by "OR".
Now select the first output pin of the OR-box and use command 'Insert' 'Assign' to assign it to
"GELB". Select the second input of the OR-box by a mouse-click on the horizontal line next to the
three question marks, so that it appears marked by a dotted rectangle. Now use 'Insert' 'Box' to add
a further EQ-box like described above. Finally the network should look like shown in the follwoing:
Function block TRAFFICSIGNAL, instruction part
In order to insert an operator in front of another operator, you must select the place where the input to
which you want to attach the operator feeds into the box.
Then use the command 'Insert' 'Box'. Otherwise you can set up these networks in the same way as
the first network.
Now our first POU has been finished. TRAFFICSIGNAL, according to the input of the value STATUS,
controls whichever light color we wish.
Connecting the standard.lib
For the timer in the POU WAIT we need a POU from the standard library. Therefore, open the library
manager with 'Window' 'Library Manager. Choose 'Insert' 'Additional library'. The dialog box appears
for opening files. From the list of the libraries choose standard.lib.
Now let us turn to the POU WAIT. This POU is supposed to become a timer with which we can
determine the length of the time period of each TRAFFICSIGNAL phase. Our POU receives as input
variable a variable TIME of the type TIME, and as output it produces a Boolean value which we want
to call OK and which should be TRUE when the desired time period is finished. We set this value with
FALSE by inserting at the end of the declaration (before the semicolon, however) " := FALSE ".
For our purposes we need the POU TP, a clock generator. This has two inputs (IN, PT) and two
outputs (Q, ET). TP does the following:
As long as IN is FALSE, ET is 0 and Q is FALSE. As soon as IN provides the value TRUE, the time is
calculated at the output ET in milliseconds. When ET reaches the value PT, then ET is no longer
counted. Meanwhile Q produces TRUE as long as ET is smaller than PT. As soon as the value PT
has been reached, then Q produces FALSE again. See the chapter on the standard library for short
descriptions of all POUs.
In order to use the POU TP in the POU WAIT we must create a local instance from TP. For this we
declare a local variable ZAB (for elapsed time) of the type TP (between the keywords VAR,
The declaration part of WAIT thus looks like this:
Function Block WAIT, Declaration Part
In order to create the desired timer, the body of the POU must be programmed as follows:
Function Block WAIT, Instruction Part
Documents you may be interested
Documents you may be interested