Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Projects and Practice

PLC Project - Traffic Light Controller

Build a traffic light controller in PLC style: requirements, I/O list, state machine, timers, pedestrian button, night mode and fault flashing, with a runnable C simulation.

Time to combine what you know. A traffic light is a classic first project because it uses a state machine, timers, an edge-detected button, modes and fault handling, yet fits on one page. You will design it the way a PLC engineer would: requirements first, then I/O, then logic, then tests.

PLC software and hardware are not available here. The design is shown as a plan and read-only Structured Text, and the runnable programs are C stand-ins that simulate the controller. This project is a learning exercise, not a certified traffic system.

Requirements

A junction with a main road (M) and a side road (S), each with red, yellow and green lamps, and a pedestrian crossing on the main road with a request button.

  1. Normal cycle: M green 8 s, M yellow 2 s, all red 1 s, S green 5 s, S yellow 2 s, all red 1 s, repeat.
  2. A pedestrian press during the cycle is remembered and serves a pedestrian phase (both roads red, walk lamp on for 4 s) once M has turned red.
  3. A fault input (for example lamp or detector failure) puts both roads into flashing yellow.
  4. A night mode switch also uses flashing yellow.
  5. After any fault or mode change, the controller restarts from all-red.

I/O list

Name Type Meaning
PedButton Input Pedestrian request
NightSwitch Input Night mode
LampFault Input Fault feedback
M_Red/M_Yel/M_Grn Output Main road lamps
S_Red/S_Yel/S_Grn Output Side road lamps
Walk, DontWalk Output Pedestrian lamps

Design: a timed state machine

stateDiagram-v2
    [*] --> AllRedStart
    AllRedStart --> MainGreen: 1 s
    MainGreen --> MainYellow: 8 s
    MainYellow --> AllRed1: 2 s
    AllRed1 --> Pedestrian: request stored
    AllRed1 --> SideGreen: no request
    Pedestrian --> SideGreen: 4 s
    SideGreen --> SideYellow: 5 s
    SideYellow --> AllRed2: 2 s
    AllRed2 --> MainGreen: 1 s
    MainGreen --> Flashing: night or fault
    Flashing --> AllRedStart: normal again

Each state has one timer. Night mode or a fault pulls the machine into Flashing.

Structured Text (read-only) for the heart of the logic would look like this:

TEXT
CASE state OF
    ST_MAIN_GREEN:
        M_Grn := TRUE; S_Red := TRUE;
        IF stepTimer.Q THEN state := ST_MAIN_YELLOW; END_IF;
    ST_MAIN_YELLOW:
        M_Yel := TRUE; S_Red := TRUE;
        IF stepTimer.Q THEN state := ST_ALL_RED_1; END_IF;
    (* ... other states ... *)
END_CASE;

The step timer is a TON whose IN is true during any timed state and restarted whenever the state changes. The pedestrian press is remembered with a latch set by a rising edge of PedButton and cleared when the pedestrian phase starts.

Simulating the normal cycle

The C program models one scan per second. A table holds each state's lamps and duration, which keeps the code short and makes timings easy to change (a good habit: data in tables, logic in code).

C C stand-in: traffic light cycle with a pedestrian request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The button press at t=5 is remembered during MAIN_GREEN and served at the next ALL_RED_1, giving the pedestrian phase. Try changing the press time or the table values.

Night mode and fault flashing

Flashing needs a blinking signal. In a PLC you build a clock bit with two timers (or use a system clock memory byte). Flashing yellow replaces the normal cycle in night mode or on a fault. The program must also remember to go through all-red when returning to normal.

C C stand-in: flashing yellow in night mode, restart from all red

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Tests to run

  1. Normal cycle lasts 8+2+1+5+2+1 = 19 s without a request, 23 s with one.
  2. A press at any time is remembered, and a double press still gives one pedestrian phase.
  3. Main and side roads are never green at the same time. Add a check in your test harness (see commissioning).
  4. A fault in any state gives flashing yellow immediately.
  5. After night mode, the system returns through all-red.

Ideas to extend

  • Add a vehicle detector on the side road that skips S green when no car waits.
  • Use a counter to log how many pedestrian phases were served.
  • Show the state and remaining time on an HMI screen.
  • Add a conflict monitor that forces flashing red if both roads get green (a fault check on the outputs).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why remember the pedestrian press instead of reacting to the button level?

    A stored request (set by a rising edge) survives until the controller reaches a state where the pedestrian phase is allowed.
  2. 2. Why is there an all-red state between yellow and the next green?

    The all-red clearance interval protects against conflicts.
  3. 3. What is a benefit of storing phase durations in a table?

    Data in tables is easy to adjust and review; logic stays small and stable.
  4. 4. What must the controller do when returning from flashing mode?

    A defined restart from all-red avoids unexpected conflicting greens.

Summary

  • Start with requirements and an I/O list, then design a state machine with timers.
  • Remember requests with an edge-detected latch; use tables for phase data.
  • Add modes (night, fault flashing) and a safe restart through all-red.
  • Test timing, conflicts and recovery.
  • The C programs are stand-ins simulating the controller.

Next: Project: Tank Level Control