Simulating Traffic Light Control System: Easy PLC Programming

Simulating Traffic Light Control System: Easy PLC Programming

Simulating Traffic Light Control System: Easy PLC Programming

Today, we are going to discuss how to use Siemens PLC to create a simulated traffic light control system. This project is particularly suitable for practice and can teach you many basic skills.

Traffic light control seems simple, but there are actually many logical details, especially regarding the switching of red and green lights, pedestrian crossings, and timing. If you’re not careful, problems can arise. Next, we will tackle it step by step.

Project Requirements Analysis

Simulating Traffic Light Control System: Easy PLC Programming

A common traffic light at an intersection has red, yellow, and green lights for both the main road and the side road, and the following functions are required:

  1. 1. The lights on the main road and side road alternate, with the main road given priority.
  2. 2. The red light lasts for 10 seconds, the yellow light for 3 seconds, and the green light for 10 seconds.
  3. 3. The side road lights only turn on after the main road green light ends.
  4. 4. Add a pedestrian button: when pressed, the main road light turns red, and the pedestrian indicator light turns green, lasting for 10 seconds.

The core logic is to clarify the order of the lights, timing switches, and the priority of button triggers.

Common Issues and Solutions

Simulating Traffic Light Control System: Easy PLC Programming

1. Confused light switching logic: When starting to write the program, the order of light switching can easily get mixed up, such as jumping directly from yellow to red, or having both the main and side roads on green at the same time. This issue usually arises from an incorrectly written state machine and unclear logical conditions.

Solution: Use state variables to manage the status of each light, for example, <span><span>0</span></span> represents red light, <span><span>1</span></span> for yellow light, and <span><span>2</span></span> for green light. The status changes of each light only depend on time and the status of other lights, clarifying the priority.

2. Pedestrian button malfunction or priority conflict: When the pedestrian button is pressed, the main road red light does not switch in time, or there is no response when the button is pressed. This is due to the trigger logic of the button not being handled separately or conflicting with the main road light logic.

Solution: Use a separate trigger signal to store the button status, such as using a bit to store whether the button is pressed, and then use a timer to complete the pedestrian crossing logic.

3. Inaccurate timing: The timing for light switches sometimes can be off by a few seconds, which is usually due to incorrect preset times for the timer or the timing logic being interrupted by other conditions.

Solution: Use the PLC’s timer to accurately control the delays, such as the TON timer, setting precise delay values to ensure that other logic does not interfere..

PLC Program Design

Simulating Traffic Light Control System: Easy PLC Programming

We will use Siemens’ STEP 7 software to write the program, assuming we are using the S7-1200 series PLC.

Hardware Allocation

  1. 1. Main road red light: Q0.0
  2. 2. Main road yellow light: Q0.1
  3. 3. Main road green light: Q0.2
  4. 4. Side road red light: Q0.3
  5. 5. Side road yellow light: Q0.4
  6. 6. Side road green light: Q0.5
  7. 7. Pedestrian button: I0.0
  8. 8. Pedestrian indicator light: Q0.6

Code Implementation

Below is the explanation of the main program’s ladder logic, combined with state variables and timers.

// State variables
DB1.DBX0.0 // Main road light status (red, yellow, green)
DB1.DBX0.1 // Side road light status (red, yellow, green)
DB1.DBX0.2 // Pedestrian button trigger status

// Timers
T1 // Main road green light delay
T2 // Main road yellow light delay
T3 // Main road red light delay
T4 // Pedestrian crossing delay
  1. 1. Main road light logic:
// Main road green light
A M0.0 // Status signal
TON T1, 10s
= Q0.2

// Main road yellow light
A M0.1 // Status signal
TON T2, 3s
= Q0.1

// Main road red light
A M0.2 // Status signal
TON T3, 10s
= Q0.0

The main road lights switch through status signals, with the green light ending followed by the yellow light, and then the red light, cycling in that order.

  1. 2. Side road light logic:
// Side road red light
A M0.2
= Q0.3

// Side road green light
A M1.0
TON T1, 10s
= Q0.5

The side road has a lower priority, only switching to green when the main road light is red.

  1. 3. Pedestrian button logic:
// Pedestrian button trigger
A I0.0 // Button input
S DB1.DBX0.2 // Pedestrian status signal

// Pedestrian green light
A DB1.DBX0.2
TON T4, 10s
= Q0.6

// Main road red light priority
A DB1.DBX0.2
= Q0.0

When the pedestrian button is pressed, it triggers the pedestrian status signal, giving priority to turning the main road light red while the pedestrian indicator light turns green.

Program Optimization

Simulating Traffic Light Control System: Easy PLC Programming

1. Add debugging features: Incorporate status monitoring into the program, such as using HMI to display the current status of the lights and the remaining time on the timers for easier debugging.

2. Add emergency mode: An emergency button can be added, which when pressed, turns all lights red to prioritize safety.

3. Adjustable delay parameters: Delay parameters such as red and green light timing can be adjusted via the HMI interface or PLC data blocks, allowing flexibility based on actual needs.

Conclusion

Simulating Traffic Light Control System: Easy PLC Programming

Traffic light control may seem simple, but there are actually many logical elements involved. However, as long as the states and priorities are clearly organized, it can be easily managed. If anyone has questions or better ideas, feel free to leave a message and we can discuss and improve together!

Leave a Comment