Bird
Raised Fist0
PLC Programmingscripting~5 mins

Traffic light sequence in PLC Programming

Choose your learning style10 modes available
Introduction
A traffic light sequence controls the lights at intersections to keep cars and people safe by telling them when to stop and go.
At road intersections to manage vehicle and pedestrian flow.
In factories to control signals for machine operations.
In parking lots to guide cars safely.
For training beginners on basic PLC programming concepts.
Syntax
PLC Programming
PROGRAM TrafficLightSequence
VAR
  RedLight : BOOL := TRUE;
  YellowLight : BOOL := FALSE;
  GreenLight : BOOL := FALSE;
  Timer : TON;
  State : INT := 0;
END_VAR

// Timer preset times in milliseconds
CONST
  RedTime : TIME := T#10s;
  GreenTime : TIME := T#15s;
  YellowTime : TIME := T#3s;
END_CONST

// Main program cycle
CASE State OF
  0: // Red light on
    RedLight := TRUE;
    YellowLight := FALSE;
    GreenLight := FALSE;
    Timer(IN := TRUE, PT := RedTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 1;
    END_IF
  1: // Green light on
    RedLight := FALSE;
    YellowLight := FALSE;
    GreenLight := TRUE;
    Timer(IN := TRUE, PT := GreenTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 2;
    END_IF
  2: // Yellow light on
    RedLight := FALSE;
    YellowLight := TRUE;
    GreenLight := FALSE;
    Timer(IN := TRUE, PT := YellowTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 0;
    END_IF
END_CASE
END_PROGRAM
This example uses a simple state machine with three states: red, green, and yellow.
The TON timer function block is used to create delays for each light.
Examples
Turn on the red light and turn off the others.
PLC Programming
RedLight := TRUE;
YellowLight := FALSE;
GreenLight := FALSE;
Start a timer for 10 seconds and check if it finished.
PLC Programming
Timer(IN := TRUE, PT := T#10s);
IF Timer.Q THEN
  // Timer finished
END_IF
Use a CASE statement to handle different traffic light states.
PLC Programming
CASE State OF
  0: // Red light
  1: // Green light
  2: // Yellow light
END_CASE
Sample Program
This program cycles through red, green, and yellow lights using a timer and state machine. Each light stays on for a set time.
PLC Programming
PROGRAM TrafficLightSequence
VAR
  RedLight : BOOL := TRUE;
  YellowLight : BOOL := FALSE;
  GreenLight : BOOL := FALSE;
  Timer : TON;
  State : INT := 0;
END_VAR

CONST
  RedTime : TIME := T#5s;
  GreenTime : TIME := T#5s;
  YellowTime : TIME := T#2s;
END_CONST

// Main cycle
CASE State OF
  0: // Red light
    RedLight := TRUE;
    YellowLight := FALSE;
    GreenLight := FALSE;
    Timer(IN := TRUE, PT := RedTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 1;
    END_IF
  1: // Green light
    RedLight := FALSE;
    YellowLight := FALSE;
    GreenLight := TRUE;
    Timer(IN := TRUE, PT := GreenTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 2;
    END_IF
  2: // Yellow light
    RedLight := FALSE;
    YellowLight := TRUE;
    GreenLight := FALSE;
    Timer(IN := TRUE, PT := YellowTime);
    IF Timer.Q THEN
      Timer(IN := FALSE);
      State := 0;
    END_IF
END_CASE

// Output status (simulated print)
IF RedLight THEN
  // print("Red Light ON")
ELSIF GreenLight THEN
  // print("Green Light ON")
ELSIF YellowLight THEN
  // print("Yellow Light ON")
END_IF
END_PROGRAM
OutputSuccess
Important Notes
PLC programs run in cycles, so timers and states update every cycle.
Use timers to create delays without stopping the whole program.
Make sure to reset timers when changing states to avoid errors.
Summary
Traffic light sequences use states to control which light is on.
Timers help keep each light on for the right amount of time.
A simple state machine cycles through red, green, and yellow lights.

Practice

(1/5)
1. What is the main purpose of a traffic light sequence in PLC programming?
easy
A. To control the order and timing of red, yellow, and green lights
B. To measure the speed of vehicles
C. To detect weather conditions
D. To count the number of cars passing

Solution

  1. Step 1: Understand traffic light function

    Traffic lights manage traffic by showing red, yellow, and green lights in a set order.
  2. Step 2: Identify PLC role

    PLC programs control the timing and sequence of these lights to keep traffic safe and flowing.
  3. Final Answer:

    To control the order and timing of red, yellow, and green lights -> Option A
  4. Quick Check:

    Traffic light sequence = control lights order and timing [OK]
Hint: Traffic lights cycle colors in order to manage traffic flow [OK]
Common Mistakes:
  • Confusing traffic light sequence with vehicle speed measurement
  • Thinking traffic lights detect weather
  • Assuming traffic lights count cars
2. Which of the following is the correct way to represent a state for the green light in a PLC program?
easy
A. state = GREEN
B. state = YELLOW
C. state = RED
D. state = OFF

Solution

  1. Step 1: Identify the green light state

    The green light state should be named clearly as GREEN to represent that light.
  2. Step 2: Match option to green state

    Only state = GREEN assigns the state variable to GREEN, which is correct for the green light.
  3. Final Answer:

    state = GREEN -> Option A
  4. Quick Check:

    Green light state = GREEN [OK]
Hint: Use clear names like GREEN for green light state [OK]
Common Mistakes:
  • Using RED or YELLOW for green light state
  • Using OFF which means no light
  • Confusing state names
3. Given this PLC pseudocode snippet for a traffic light sequence:
timer = 0
state = 'RED'
if timer < 10:
  light = 'RED'
elif timer < 15:
  light = 'GREEN'
else:
  light = 'YELLOW'

What will be the value of light when timer is 12?
medium
A. 'YELLOW'
B. 'RED'
C. 'GREEN'
D. Error

Solution

  1. Step 1: Check timer conditions for 12

    12 is not less than 10, so first if is false. 12 is less than 15, so second condition is true.
  2. Step 2: Determine light value

    Since timer < 15 is true, light is set to 'GREEN'.
  3. Final Answer:

    'GREEN' -> Option C
  4. Quick Check:

    timer=12 <15 -> light='GREEN' [OK]
Hint: Check conditions in order, timer 12 fits second condition [OK]
Common Mistakes:
  • Choosing 'RED' because timer is close to 10
  • Choosing 'YELLOW' by ignoring conditions order
  • Thinking code causes error
4. This PLC code snippet is intended to cycle traffic lights but does not work correctly:
state = 'RED'
if state == 'RED':
  state = 'GREEN'
elif state == 'GREEN':
  state = 'YELLOW'
elif state == 'YELLOW':
  state = 'RED'

What is the main problem?
medium
A. The state variable is misspelled
B. The state changes only once because conditions are checked once
C. The code uses wrong comparison operators
D. The code lacks a timer to delay state changes

Solution

  1. Step 1: Analyze state change logic

    The code changes state immediately but only once; no loop or repeated check.
  2. Step 2: Identify missing repeated execution

    Without looping or timer, state changes once and stops, so no cycling happens.
  3. Final Answer:

    The state changes only once because conditions are checked once -> Option B
  4. Quick Check:

    Single run code = no cycling [OK]
Hint: State must update repeatedly, not just once [OK]
Common Mistakes:
  • Thinking variable name is wrong
  • Believing comparison operators are incorrect
  • Ignoring need for timing or looping
5. You want to create a PLC program that cycles traffic lights with these timings: red for 10 seconds, green for 15 seconds, yellow for 3 seconds. Which approach best implements this sequence?
hard
A. Use separate timers for each light but no state variable
B. Use a single timer that counts total time without resetting
C. Use random delays between light changes
D. Use a state machine with a timer that resets after each state duration

Solution

  1. Step 1: Understand timing requirements

    Each light needs a specific duration before switching to the next.
  2. Step 2: Choose best control method

    A state machine with a timer that resets after each state's time allows precise control and cycling.
  3. Final Answer:

    Use a state machine with a timer that resets after each state duration -> Option D
  4. Quick Check:

    State machine + timer reset = correct timing control [OK]
Hint: Reset timer each state for exact light durations [OK]
Common Mistakes:
  • Using one timer without reset causes wrong timing
  • Random delays break predictable sequence
  • No state variable causes confusion in sequence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard