Sample Program

Let us now start to write a small example program. It is for a small traffic signal unit which is supposed to control two trafficsignals at an intersection. The red/green phases of both trafficsignals alternate and, in order to avoid accidents, we will insert between the phases yellow or yellow/red transitional phases. The latter will be shorter than the former. In this example you will see how time dependent programs can be shown with the language ressources of the IEC61131-3 standard, how one can edit the different languages of the standard with the help of TwinCAT PLC Control.

Create POU

First start TwinCAT PLC Control and choose "File" "New". In the dialog box which appears, the first POU should have the name PLC_PRG. The type of POU should definitely be a program. In this case we choose as the language of this POU the sequential function chart (SFC). Now create two 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 function block in the language Function Block Diagram (FBD) by the names of TRAFFICSIGNAL, along with a POU WAIT, also of the type function block, which we want to program as an instruction list (IL).

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.


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.
PLC_PRG will combine everything at the end so that the right light lights up at the right time for the desired time period.


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 five possible conditions, that is one for the TRAFFICSIGNAL phases green, yellow, yellow/red, red, and off.

Therefore our TRAFFICSIGNAL has four outputs, that is RED, YELLOW, GREEN, and OFF. You should declare these four variables. Then the declaration part of our function block TRAFFICSIGNAL will look like this:

Sample Program 1:

Function block TRAFFICSIGNAL, declaration part


Now it is time to determine from the input STATUS of the POU the values of the output variables. For 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 1). You have now selected the first network. Now choose the menu item "Insert" "Operator". In the first network a box is inserted with the operator AND and two inputs:

Sample Program 2:

Click on the text AND with the mouse pointer and change the text into EQ. Select the three question marks from the upper of the two inputs and enter the variable STATUS. Then select the lower of the three question marks and put a 1 underneath it. You get the following network:

Sample Program 3:

Click now on a place behind the EQ Box. Now the output of the EQ operation is selected. Choose "Insert" "Assignment". Change the three question marks ??? to GREEN. You now have created a network with the following structure:

Sample Program 4:

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 or for OFF we need three more net-works. You create them with the command "Insert" "Network (after)". These networks should be set up as in the example. The finished POU now is as follows:

Sample Program 5:

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" "Operator". Otherwise you can setup 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.

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. In addition you will find a short description of all POUs from the standard library in the appendix.

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, END_VAR). The declaration part of WAIT thus looks like this:

Sample Program 6:

Function Block WAIT, Declaration Part

In order to create the desired timer, the body of the POU must be programmed as follows:

Sample Program 7:

Function Block WAIT, Instruction Part

At first the question is posed whether Q has already been set at TRUE (as though the counting had already been executed), in this case we change nothing with the occupation of ZAB, but rather we call the function block ZAB without input (in order to check whether the time period is already over).

Otherwise we set the variable IN in ZAB at FALSE, and therefore at the same time ET at 0 and Q at FALSE. In this way all variables are set at the desired initial condition. Now we assign the necessary time from the vari-able TIME into the variable PT, and call ZAB with IN:=TRUE. In the function block ZAB the variable ET is now calculated until it reaches the value TIME, then Q is set at FALSE.

The negated value of Q is saved in OK after each execution of WAIT. As soon as Q is FALSE, then OK produces TRUE.

The timer is finished at this point. Now it is time to combine our two function blocks WAIT and TRAFFICSIGNAL in the main program PLC_PRG.

First we declare the variables we need. They are two instances of the function block TRAFFICSIGNAL (TRAFFICSIGNAL1, TRAFFICSIGNAL2) and one of the type WAIT (DELAY as delay). PLC_PRG now appears as follows:

Sample Program 8:

Program PLC_PRG, First Expansion Level, Declaration Part

The beginning diagram of a POU in SFC always consists of an action "Init" of a following transition "Trans0" and a jump back to Init. We should expand that somewhat.

First let us determine the structure of the diagrams before we program the individual action and transitions. First we need for each TRAFFICSIGNAL phase a step.. Insert it by marking Trans0 and choosing "Insert" "Step transition (behind)". Repeat this procedure three more times.

If you click directly on the name of a transition or a step, then this is marked and you can change it. Name the first transition after Init "TRUE", and all other transitions "DELAY.OK".

The first transition always switches through, and all others switch through when DELAY in OK produces TRUE, i.e. when the set time period is finished.


The steps (from top to bottom) receive the names Switch1, Green2, Switch2, Green1, whereby Init of course keeps its Name. "Switch" should mean each time a yellow phase, at Green1 TRAFFICSIGNAL1 at Green2 TRAFFICSIGNAL2 will be green. Finally change the return address of Init after Switch1. If you have done everything right, then the diagram should be as follows:

Sample Program 9:

Program PLC_PRG, First Expansion Level, Instruction Part


Now we have to finish programming the individual steps. If you doubleclick on the field of a step, then you open a dialog to open a new action. In our case we will use IL (Instruction list).


Actions and transition conditions

In the action of the step Init the variables are initialized, the STATUS of TRAFFICSIGNAL1 should be 1 (green). The state of TRAFFICSIGNAL2 should be 3 (red). The action Init then is as follows:

Sample Program 10:

Action Init


Switch1 changes the State of TRAFFICSIGNAL1 to 2 (yellow), and that of TRAFFICSIGNAL2 to 4 (yellow-red). In addition, a time delay of 2000 milli-seconds is now set. The action is now as follows:

Sample Program 11:

Action Switch1


With Green2 TRAFFICSIGNAL1 is red (STATUS:=3), TRAFFICSIGNAL2 green (STATUS:=1), and the delay time is at 5000.

Sample Program 12:

Action Green2


At Switch2 the STATUS of TRAFFICSIGNAL1 changes to 4 (yellow-red), that of TRAFFICSIGNAL2 to 2 (yellow). A time delay of 2000 milliseconds is now set.

Sample Program 13:

Action Switch2


With Green1 TRAFFICSIGNAL1 is green (STATUS:=1), TRAFFICSIG-NAL2 is red (STATUS:=3), and the time delay is set at 5000.

Sample Program 14:

Action Green1

The first expansion phase of our program is completed. Now you can com-pile it and also test the simulation.


In order to ensure that our diagram has at least one alternative branch, and so that we can turn off our traffic light unit at night, we now include in our program a counter which, after a certain number of TRAFFICSIGNAL cycles, turns the unit off.

At first we need a new variable COUNTER of the type INT. Declare this as usual in the declaration part of PLC_PRG, and initialize it in Init with 0.

Sample Program 15:

Action Init, Second Version


Now select the transition after Switch1 and insert a step and then a transition. Select the resulting transition and insert an alternative branch to its left. After the left transition insert a step and a transition. After the resulting new transition insert a jump after Switch1. Select the transition below the new step. Open the context menu and select "Zoom action/transition". Select IL for the language of the transition.

Name the new parts as follows: the upper of the two new steps should be called "Count" and the lower "Off". The transitions are called (from top to bottom and from left to right) EXIT, TRUE and DELAY.OK. The new part should therefore look like the part with the black border:

Sample Program 16:

Traffic light unit

Now two new actions and a new transition condition are to be implemented. At the step Count, the only thing that happens is that COUNTER is increased by one:
Sample Program 17:

The EXIT transition checks whether the counter is greater than a certain number, let us say 7:
Sample Program 18:

At Off the state of both lights is set at (OFF), the COUNTER is reset to 0, and a time delay of 10 seconds is set:
Sample Program 19:

In our hypothetical situation, night falls after seven TRAFFICSIGNAL cycles, for ten seconds the TRAFFICSIGNAL turns itself off, then we have daylight again, the traffic light unit turns itself on again, and the whole process starts again from the beginning.

Now test your program. For this you must compile it ("Project" "Rebuild all") login ("Online" "Login" and then load it "Online" "Download"). If you now select "Online" "Run", the chronological order of the individual steps of your main program can be followed. The window of the POU PLC_PRG has now changed to the monitor 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.