04Level 2
Crossroad traffic lights
Two coordinated traffic lights driven by a single cycle timer and compare contacts on its elapsed time.
The simulator is designed for computers (screens 1024 px or wider).
Virtual plant
Street A: off. Street B: off
Shown at rest. In the simulator the plant moves according to your program's outputs.
Inputs and outputs
| Address | Name | Description |
|---|---|---|
| I0.0 | ON | Operation switch |
| Q0.0 | RED_A | Red light, street A |
| Q0.1 | AMBER_A | Amber light, street A |
| Q0.2 | GREEN_A | Green light, street A |
| Q0.3 | RED_B | Red light, street B |
| Q0.4 | AMBER_B | Amber light, street B |
| Q0.5 | GREEN_B | Green light, street B |
| T0 | CYCLE | 20 s cycle timer |
| M0.0 | CYCLE_END | Pulse at the end of each cycle |
Program
Ladder diagram (LD)
Rung 1 — Self-restarting 20 s cycle timer.
Rung 2 — Street A: green from 0 to 8 s.
Rung 3 — Street A: amber from 8 to 10 s.
Rung 4 — Street A: red from 10 to 20 s.
Rung 5 — Street B: red from 0 to 10 s.
Rung 6 — Street B: green from 10 to 18 s.
Rung 7 — Street B: amber from 18 to 20 s.
Function block diagram (FBD)
Network 1 — Self-restarting 20 s cycle timer.
Network 2 — Street A: green from 0 to 8 s.
Network 3 — Street A: amber from 8 to 10 s.
Network 4 — Street A: red from 10 to 20 s.
Network 5 — Street B: red from 0 to 10 s.
Network 6 — Street B: green from 10 to 18 s.
Network 7 — Street B: amber from 18 to 20 s.
Structured text (ST)
(* Crossroad traffic lights — converted from Ladder *)
VAR
CYCLE : TON;
END_VAR
(* Rung 1: Self-restarting 20 s cycle timer. *)
CYCLE(IN := ON AND NOT CYCLE_END, PT := T#20s);
CYCLE_END := CYCLE.Q;
(* Rung 2: Street A: green from 0 to 8 s. *)
GREEN_A := ON AND T0.ET < T#8s;
(* Rung 3: Street A: amber from 8 to 10 s. *)
AMBER_A := ON AND T0.ET >= T#8s AND T0.ET < T#10s;
(* Rung 4: Street A: red from 10 to 20 s. *)
RED_A := ON AND T0.ET >= T#10s;
(* Rung 5: Street B: red from 0 to 10 s. *)
RED_B := ON AND T0.ET < T#10s;
(* Rung 6: Street B: green from 10 to 18 s. *)
GREEN_B := ON AND T0.ET >= T#10s AND T0.ET < T#18s;
(* Rung 7: Street B: amber from 18 to 20 s. *)
AMBER_B := ON AND T0.ET >= T#18s;Instruction list (IL)
(* Crossroad traffic lights — converted from Ladder *)
VAR
CYCLE : TON;
RUNG_1 : BOOL;
END_VAR
(* Rung 1: Self-restarting 20 s cycle timer. *)
LD ON
ANDN CYCLE_END
ST RUNG_1
CAL CYCLE(IN := RUNG_1, PT := T#20s)
LD CYCLE.Q
ST CYCLE_END
(* Rung 2: Street A: green from 0 to 8 s. *)
LD ON
AND( T0.ET
LT T#8s
)
ST GREEN_A
(* Rung 3: Street A: amber from 8 to 10 s. *)
LD ON
AND( T0.ET
GE T#8s
)
AND( T0.ET
LT T#10s
)
ST AMBER_A
(* Rung 4: Street A: red from 10 to 20 s. *)
LD ON
AND( T0.ET
GE T#10s
)
ST RED_A
(* Rung 5: Street B: red from 0 to 10 s. *)
LD ON
AND( T0.ET
LT T#10s
)
ST RED_B
(* Rung 6: Street B: green from 10 to 18 s. *)
LD ON
AND( T0.ET
GE T#10s
)
AND( T0.ET
LT T#18s
)
ST GREEN_B
(* Rung 7: Street B: amber from 18 to 20 s. *)
LD ON
AND( T0.ET
GE T#18s
)
ST AMBER_B
Step by step
- 1Press ▶ Run (F5) and turn on the ON switch (I0.0).
- 2Rung 1 is a 20 s timer that restarts itself: when it finishes, CYCLE_END turns on, its NC contact resets the timer and everything starts over.
- 3The other rungs compare the elapsed time (CYCLE.ET) with fixed limits: green A from 0 to 8 s, amber A from 8 to 10 s, red A from 10 to 20 s; street B does the opposite.
- 4Watch T0.ET in the monitor and check that two green lights are never on together. Try changing the times in the compare contacts.