LadderLogicOnline

Learn › Tutorials

Tutorial: program a traffic light sequence with timers

A traffic intersection is a classic PLC exercise: a fixed sequence of steps, each lasting a set time, repeating forever, where a mistake is easy to see (and in the simulator, cars crash). You'll build it from a chain of TON timers.

This tutorial uses TON timers and their DN bits throughout. If you haven't met them yet, read TON: Timer On Delay first.

The plan

The intersection has an east–west road and a north–south road. Each direction gets green, then yellow, then red. Between one direction going red and the other going green, there's a short all-red phase where every light is red. One full cycle has six phases:

PhaseEast–westNorth–southTimeTimer
1GreenRed8 sT_EWG
2YellowRed3 sT_EWY
3RedRed2 sT_AR1
4RedGreen8 sT_NSG
5RedYellow3 sT_NSY
6RedRed2 sT_AR2

Each phase has its own timer. When one timer finishes, its DN bit starts the next. When the last one finishes, the whole chain resets and the cycle starts again.

Tags and parts

Open the Playground and pick the Traffic intersection scene from the simulator toolbar. Its starter already has the roads, four car spawners and four traffic lights. The two east–west lights use the tags EW_Red, EW_Yellow and EW_Green, and the two north–south lights use NS_Red, NS_Yellow and NS_Green, so both lights for a direction always show the same colour. The starter program is a single rung that keeps EW_Red on; delete it, since you're about to write the real thing.

Add six TIMER tags: T_EWG, T_EWY, T_AR1, T_NSG, T_NSY and T_AR2. You'll set each preset (in seconds) on its TON instruction, using the times in the table.

Rungs 0 to 5: the timer chain

 |   T_AR2.DN                                    |
 |----]/[--------------------------[TON  T_EWG  8 s]---|
 |   T_EWG.DN                                    |
 |----] [--------------------------[TON  T_EWY  3 s]---|
 |   T_EWY.DN                                    |
 |----] [--------------------------[TON  T_AR1  2 s]---|
 |   T_AR1.DN                                    |
 |----] [--------------------------[TON  T_NSG  8 s]---|
 |   T_NSG.DN                                    |
 |----] [--------------------------[TON  T_NSY  3 s]---|
 |   T_NSY.DN                                    |
 |----] [--------------------------[TON  T_AR2  2 s]---|
Six rungs, each timer started by the one before it.

Follow it through. When the program starts, every timer is off, so XIO T_AR2.DN is true and T_EWG starts timing. After 8 seconds its DN bit turns on, which makes rung 1 true and starts T_EWY. Like dominoes, each timer starts the next while the earlier ones stay done, because their rungs are still true.

The clever part is the reset. When T_AR2 finishes, XIO T_AR2.DN goes false, so T_EWG's rung goes false and the timer resets: its accumulator goes to 0 and DN turns off. Rung 1 is solved next in the same scan, sees T_EWG.DN off, and resets T_EWY. The reset ripples down the chain in a single scan, all the way to T_AR2 itself. On the next scan XIO T_AR2.DN is true again and the cycle restarts. This only works because the PLC solves rungs top to bottom and each rung sees the results of the ones above it (see the scan cycle).

Rungs 6 to 11: the lights

Now turn timer states into lamps. The trick is to describe each light by "which timers are done". For example, east–west yellow is on after T_EWG is done but before T_EWY is done.

 |   T_EWG.DN                                 EW_Green |
 |----]/[-----------------------------------------( )------|
 |   T_EWG.DN    T_EWY.DN                    EW_Yellow |
 |----] [---------]/[-----------------------------( )------|
 |   T_EWY.DN                                   EW_Red |
 |----] [-----------------------------------------( )------|
 |   T_AR1.DN    T_NSG.DN                     NS_Green |
 |----] [---------]/[-----------------------------( )------|
 |   T_NSG.DN    T_NSY.DN                    NS_Yellow |
 |----] [---------]/[-----------------------------( )------|
 |   T_AR1.DN                                   NS_Red |
 |----]/[----+------------------------------------( )------|
 |   T_NSY.DN|                                          |
 |----] [----+                                          |
Each lamp is a simple test of the timer done bits.

Check each one against the phase table:

Keep each lamp on exactly one OTE. If you're tempted to turn NS_Red on from two different rungs, use a branch instead: two coils for the same tag fight, and the lower rung always wins.

Test it

Press Run. Cars start arriving from all four directions. In this simulator, cars go on green and stop at red or at a dark light. On yellow they stop if they can, but carry on if they're too close to the line to stop. Watch a full cycle and check that every light follows the table, and that nobody is ever green at the same time as the crossing direction.

Now see why the all-red phase exists. Stop the program, set the T_AR1 and T_AR2 presets to 0, and run it again. Sooner or later a car that went through on yellow is still crossing when the other direction turns green, and the simulator shows a crash. The all-red phase gives the junction time to clear. Real signal engineers calculate it from the width of the junction and the speed limit; here, 2 seconds is plenty. Put the presets back.

Try it in the Playground

  1. Build the twelve rungs above in the Playground's traffic scene and run them.
  2. Watch the timers in the Tags panel: each shows ACC / PRE as it runs.
  3. Try a longer green for the busier road and a shorter one for the other, and watch how the queues change.

Extensions to try

Common mistakes