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:
| Phase | East–west | North–south | Time | Timer |
|---|---|---|---|---|
| 1 | Green | Red | 8 s | T_EWG |
| 2 | Yellow | Red | 3 s | T_EWY |
| 3 | Red | Red | 2 s | T_AR1 |
| 4 | Red | Green | 8 s | T_NSG |
| 5 | Red | Yellow | 3 s | T_NSY |
| 6 | Red | Red | 2 s | T_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]---|
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| |
|----] [----+ |
Check each one against the phase table:
- EW_Green is on until
T_EWGis done: phase 1. - EW_Yellow is on from
T_EWGdone untilT_EWYdone: phase 2. - EW_Red is on from
T_EWYdone to the end of the cycle: phases 3 to 6. - NS_Green and NS_Yellow work the same way for phases 4 and 5.
- NS_Red needs two separate stretches, phases 1 to 3 and phase 6, so it uses a branch: on before
T_AR1is done, OR afterT_NSYis done.
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
- Build the twelve rungs above in the Playground's traffic scene and run them.
- Watch the timers in the Tags panel: each shows ACC / PRE as it runs.
- Try a longer green for the busier road and a shorter one for the other, and watch how the queues change.
Extensions to try
- Demand-actuated green. Place a vehicle sensor on the north–south approach just before the light, with the tag
NS_Det. Add a BOOLNS_Call: latch it withXIC NS_Det→OTL NS_Call, unlatch it whenNS_Greencomes on, and addXIC NS_Callin series on rung 1. East–west then stays green until a car actually arrives on the side road (see OTL and OTU). - A pedestrian button. Add a push button that requests an early end to the east–west green, but only after a minimum green time has passed. A compare on
T_EWG.ACCis one way to do it (see compare instructions). - One timer instead of six. Rebuild the sequence with a single 26 second timer that restarts itself, and
LIMITinstructions on its accumulator for each phase. Which version is easier to change later? - Night flash. Add a BOOL
Nightthat, when on, stops the sequence and flashes both yellows using two timers that toggle each other.
Common mistakes
- Leaving the starter rung in. A leftover
OTE EW_Redkeeps fighting your new red rung. - Lights rungs above the timer rungs. It still works, but each lamp lags one scan behind its timer. Keep logic in the order it flows: timers first, then outputs.
- No all-red time. It looks fine for a while, then a car runs a late yellow and meets crossing traffic.