To create traffic light PLC code, you must implement a sequential timer loop where one output (Green) triggers a timer that, upon completion, disables the first output and enables the second (Yellow), which then triggers the third (Red) in a continuous cycle. This achieves a basic automated traffic control system by ensuring only one light is active at a time and each remains lit for a specific, predetermined duration.

What You'll Need

  • A Programmable Logic Controller (PLC) supporting Ladder Logic (LD) or Function Block Diagram (FBD).
  • PLC Programming Software (such as TIA Portal, RSLogix, GX Works, or CODESYS).
  • Three digital output modules connected to Red, Yellow, and Green lamps.
  • One digital input module connected to a Start pushbutton (Normally Open).
  • One digital input module connected to a Stop pushbutton (Normally Closed).
  • A power supply compatible with your PLC and lighting hardware.

Step-by-Step

  1. Define Your I/O Mapping: Before writing code, assign your physical hardware to software tags. This prevents confusion during the programming phase. Map your inputs as follows: I0.0 as the Start button, I0.1 as the Stop button. Map your outputs as: Q0.0 for the Green light, Q0.1 for the Yellow light, and Q0.2 for the Red light. Create internal memory bits (merkers) for the system state, such as M0.0 for "System Run".
  2. Create the Start/Stop Latch: In your first ladder rung, create a seal-in circuit. Place the Start button (Normally Open) in parallel with the System Run bit (M0.0), and place the Stop button (Normally Closed) in series. When the Start button is pressed, M0.0 becomes true and stays true even after the button is released, until the Stop button is pressed. This ensures the traffic light sequence only operates when the system is officially "ON".
  3. Program the Green Light Logic: Create a rung where the System Run bit (M0.0) and the completion of the Red light timer (T3) trigger the Green light output (Q0.0). To ensure the Green light turns off after a set time, add a timer (T1) in parallel to the output. Set the preset value for T1 (e.g., 10 seconds). The Green light should remain active as long as T1 has not yet reached its preset value. Use a normally closed contact of T1 to break the circuit for Q0.0.
  4. Program the Yellow Light Logic: The Yellow light should trigger immediately after the Green light timer (T1) finishes. In the next rung, use the completion bit of T1 as the trigger for the Yellow light output (Q0.1). Similar to the Green light, add a second timer (T2) to control the Yellow duration (e.g., 3 seconds). Use a normally closed contact of T2 to ensure the Yellow light turns off once its time has elapsed.
  5. Program the Red Light Logic: The Red light is triggered by the completion of the Yellow light timer (T2). In the third rung, use the T2 completion bit to activate the Red light output (Q0.2). Add a third timer (T3) to control the Red light duration (e.g., 10 seconds). Use a normally closed contact of T3 to disable the Red light output.
  6. Close the Loop: To make the system cycle indefinitely, the completion of the final timer (T3) must reset the first timer (T1) and re-trigger the Green light. Because the logic is scanned from top to bottom by the PLC, the transition from T3 back to the Green light happens automatically in the next scan cycle, provided the System Run bit remains active.
  7. Implement Safety Interlocks: To prevent a catastrophic failure where two lights are on at once (e.g., Green and Red), add interlocking contacts. In the Green light rung, add a normally closed contact of the Red light (Q0.2) and Yellow light (Q0.1). Do the same for the other rungs. This ensures that if a timer fails or a bit is forced, the PLC will not energize conflicting outputs.
  8. Download and Test: Compile the code to check for syntax errors. Download the program to the PLC. First, test the logic in a simulator if available. If testing on hardware, use a multimeter to verify the output voltages before connecting the actual high-voltage lamps. Verify that the sequence is Green → Yellow → Red → Green.

Common Mistakes

One of the most frequent errors is failing to use a latching circuit for the start button. Beginners often simply put the start button in series with the lights; this results in the lights only working while the finger is physically holding the button down. Another common mistake is the "race condition," where multiple timers trigger at the same time because the logic isn't strictly sequential. If you don't use the completion bit of the previous timer to start the next one, all lights may flash or stay on simultaneously.

Additionally, many programmers forget the Stop button's physical state. If using a physical Normally Closed (NC) button for safety, the logic in the PLC must be written as a Normally Open (NO) contact because the button is already sending a "1" signal to the PLC. Adding another NC contact in the code would invert the logic, meaning the system would run when the button is pressed and stop when it is released.

Tips

For better maintainability, use "Timer On Delay" (TON) instructions rather than complex counter-based logic. TON timers are the industry standard for sequential processes like traffic lights because they are easy to adjust and debug. If you need to change the timing for a specific intersection, you only need to change one numerical value in the timer preset rather than rewriting the logic.

Consider using a State Machine approach if the project grows. Instead of just timers, use an integer variable to represent the current state (e.g., State 0 = Off, State 1 = Green, State 2 = Yellow, State 3 = Red). This makes the code much easier to read and allows you to add complex features, such as a "Pedestrian Request" button, without creating a "spaghetti" of crossing wires in your ladder logic.

Troubleshooting

If the lights do not turn on at all, first check the System Run bit (M0.0). If the bit is not active, the Stop button may be wired incorrectly or the Start button is not making a connection. Check the PLC input LEDs to see if the signal is actually reaching the controller. If the inputs are working but the outputs are not, verify the power supply to the output modules; PLC outputs often require a separate 24VDC or 120VAC source to power the actual lamps.

If the sequence skips a light (e.g., goes from Green straight to Red), check the Yellow timer's preset value. If the preset is set to 0 or a very low value, the PLC scan cycle (which happens in milliseconds) may pass through the Yellow state so quickly that the physical lamp doesn't have time to illuminate. Ensure all timers are set to at least 500ms for visible testing.

If all lights are on at once, you likely have a logic error in your interlocking or are using "Set" (S) instructions without corresponding "Reset" (R) instructions. In a traffic light system, avoid using Set/Reset for the lights themselves; instead, use conditional coils that depend on the timer states. This ensures that when the condition becomes false, the light automatically turns off.

Frequently Asked Questions

Can I add a pedestrian crossing button to this code?

Yes. You would add a digital input for the button and a memory bit to store the "request." When the system is in the Green state and the request bit is true, you can trigger a shorter Green timer to accelerate the transition to Red, allowing the pedestrian to cross.

What happens if the PLC loses power?

By default, most PLC outputs will turn off. When power is restored, the system will remain in the "Off" state until the Start button is pressed again, provided you used a non-retentive memory bit for the System Run state.

Do I need a different timer for each light?

Yes, you must use separate timer instances (T1, T2, T3). If you use the same timer for all three lights, the PLC will overwrite the accumulated value every time a new rung is scanned, and the sequence will never progress.

Can this logic be used for a 4-way intersection?

Yes, but you must create a "Phase" system. While North-South is Green, East-West must be locked in Red. You would duplicate the timer logic for the second set of lights but invert the triggers so that the East-West Green only starts after the North-South Red timer has finished.

Why use Ladder Logic instead of Structured Text?

Ladder Logic is preferred for traffic systems because it visually represents the electrical flow. This allows technicians to troubleshoot the system using the software's "online monitoring" mode, where they can see exactly which "wire" is not energizing in real-time.