Two way Traffic Light System Design

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Hello Everyone ,

I have a breadboard, microcontroller, LEDs, and push buttons, and I'm interested in creating a two-way traffic light system for an experimental project. I have already designed a diagram for vehicle and pedestrian crossings.

1696689347354.png


Here are the simplified rules for both vehicles and pedestrians:

Rules for Vehicles:

  1. Red Light (Stop):
    • When the traffic signal is red for eastbound or westbound vehicles.
    • All vehicles in those lanes must come to a complete stop.
  2. Green Light (Go):
    • When the traffic signal is green for eastbound or westbound vehicles:
    • Vehicles in those lanes can go.
  3. Yellow Light (Prepare to Stop):
    • When the traffic signal changes from green to yellow for eastbound or westbound vehicles:
    • Vehicles should prepare to stop as the signal is about to turn red.

Rules for Pedestrians:

  1. Blue Light (Walk):
    • When the pedestrian signal is blue for northbound or southbound pedestrians:
    • Pedestrians can cross.
  2. Red Light (Stop):
    • When the pedestrian signal is red for northbound or southbound pedestrians:
    • Pedestrians must not cross.


For the traffic light system:

  • The red LED should be active for 20 seconds.
  • The green LED should also be active for 20 seconds.
  • A 3-second interval is allocated for the yellow LED.
  • The blue LED will be active for 20 seconds.


I'm facing some challenges in designing the logic for the pedestrian signal timing in traffic light system. Specifically, I'm finding it difficult to determine when and how much time should be allocated for pedestrian crossing.

I would greatly appreciate any suggestions you can provide to help me create a safe timing sequence for pedestrians.

Thank you for your any suggestion
 

Alec_t

Joined Sep 17, 2013
14,370
Which country are you in which has enforceable rules for herding pedestrians into north and south routes?
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Which country are you in which has enforceable rules for herding pedestrians into north and south routes?
I am not designing for a specific country that follows their rules. I want to make it simple and will try to add complex features later if I get the simple part done successfully
 

Alec_t

Joined Sep 17, 2013
14,370
I would greatly appreciate any suggestions you can provide to help me create a safe timing sequence for pedestrians.
Would it be possible to provide some way for elderly/disabled pedestrians to enable the normal period to be extended so that they can cross safely?
 

eetech00

Joined Jun 8, 2013
4,004
Suggestion-

Steady Blue = walk
Flashing Blue = don't walk impending
(Dark) Blue = don't walk

The pedestrian crossing timeout depends on the length of the crosswalk. It should be designed to be adjustable for your experiment. There also need to be a "trigger" that safely brings the vehicle traffic to a halt in both directions and starts the pedestrian crossing lights.
 

Jon Chandler

Joined Jun 12, 2008
1,070
Copied from code I wrote some 8 years ago to control a traffic light in a restaurant, here are some references I found. As an aside, if you're going to hang a traffic signal light in a restaurant to use as a decoration, be sure to use the style with screw-in incandescent bulbs so you can alter the brightness. An LED signal that you can see for two miles at night is beyond dazzling in a small restaurant!

Code:
(Cods tags to preserve formating)
http://ops.fhwa.dot.gov/publications/fhwahop08024/chapter5.htm#5.3

Traffic Signal Timing Manual
CHAPTER 5
BASIC SIGNAL TIMING PROCEDURE AND CONTROLLER PARAMETERS

Major Arterial (speed limit is 40 mph or less) 	7 to 15 seconds

Detailed articles on traffic light differences:
http://en.wikipedia.org/wiki/Traffic-light_signalling_and_operation
http://en.wikipedia.org/wiki/Traffic_light

Based on above:
Green: 12 seconds
Yellow: 3 secondshb
}
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Would it be possible to provide some way for elderly/disabled pedestrians to enable the normal period to be extended so that they can cross safely?
Yes, it would be possible, but not for now, as I mentioned earlier. My primary focus is to create a simple system first, and if I can successfully achieve that, I will consider adding more complex features, including you suggested for elderly and disabled pedestrians
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
I understand that this traffic light system is not ideal as it operates on a round-robin basis, where vehicles and pedestrians share equal time.

How can we create a system that allocates time for pedestrians only when there are people waiting to cross the road?
 

eetech00

Joined Jun 8, 2013
4,004
I understand that this traffic light system is not ideal as it operates on a round-robin basis, where vehicles and pedestrians share equal time.

How can we create a system that allocates time for pedestrians only when there are people waiting to cross the road?
That is what I was alluding to when a mentioned "trigger" in my previous post.
Perhaps this could be a push button to initiate the crosswalk sequence(?).
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
That is what I was alluding to when a mentioned "trigger" in my previous post.
Perhaps this could be a push button to initiate the crosswalk sequence(?).
I'm planning to use a 1 ms timer interrupt to track time, but I'm unsure about the method for handling buttons. Would you recommend using a polling method or an external interrupt for this purpose?
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
If you look at the reference one line #2 in my above post, every Timing detail you could possibly want is discussed.
I checked out the link you posted, and it looks informative. I'll need some time to go through the documents and gather relevant information
 

eetech00

Joined Jun 8, 2013
4,004
I'm planning to use a 1 ms timer interrupt to track time, but I'm unsure about the method for handling buttons. Would you recommend using a polling method or an external interrupt for this purpose?
Polling would probably be good enough for one button.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
Polling would probably be good enough for one button.
What about when we have more than one button to manage system? For example, we might need additional buttons to report system faults or indicate when the system is not working correctly
 

djsfantasi

Joined Apr 11, 2010
9,180
Polling is often sufficient. I’d use interrupts for a real-time application where response time must be measured in microseconds. For a response time of a few milliseconds, polling will suffice.
You can be more specific in the requirements that dictate interrupts versus polling, but this is a good start.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
You can be more specific in the requirements that dictate interrupts versus polling, but this is a good start.
I plan to test how my traffic system would respond to various inputs. This includes two push buttons for pedestrian inputs. Additionally, I'll incorporate an emergency button to simulate a fault in the traffic light system, which should trigger flashing all red lights to indicate a system malfunction
 

Ya’akov

Joined Jan 27, 2019
9,229
This manual is probably as close to definitive (for the US) as you are likely to find. In addition to timing it will also provide clues to both the taxonomy of traffic signals and how that affects thinking about timing solutions. There is also a list of resources that includes a couple more downloadable documents.

Traffic Signal Timing Manual
is an 18-month effort to develop a concise, practical, user friendly and modular guide to signal timing practitioners, focusing on the principals of traffic signal timing, identifying sound timing practices, and a practical and comprehensive tabletop resource.

The target audience for this manual, expected to be completed in March of 2008, will be state, city and local individuals responsible for the day-to-day timing and coordination of traffic signals, including planning, design, operations and maintenance staff who design, operate and maintain traffic signals, whether it be an individual signal, large signal system or somewhere in between.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
482
The program aims to create a traffic light sequence that follows these rules:

  • Red LED is active for 20 seconds.
  • After 20 seconds of the Red LED being active, it switches to the Green LED, turning off the Red LED.
  • Green LED is active for 20 seconds.
  • After 20 seconds of the Green LED being active, it switches to the Yellow LED, turning off the Green LED.
  • Yellow LED is active for 3 seconds.
  • After 3 seconds of the Yellow LED being active, it switches back to the Red LED, turning off the Yellow LED.
  • This sequence then repeats indefinitely, creating the one way traffic light cycle.

C:
// Crystal frequency: 8MHz
//PIC18F45K80

#include <xc.h>
#include "config.h"

// Define state flags
#define RED_ON      0
#define GREEN_ON    1
#define YELLOW_ON   2

// Define LED control macros
#define RED     LATB0
#define GREEN   LATB1
#define YELLOW  LATB2

// Initialize variables
unsigned char traffic_state = RED_ON;
unsigned long red_timer = 0;
unsigned long green_timer = 0;
unsigned long yellow_timer = 0;

void initializeTimer2(void) {
    // Timer2 initialization code
    T2CONbits.T2OUTPS = 0b0000; // 1:1 postscale
    T2CONbits.TMR2ON = 1;       // TMR2 on
    T2CONbits.T2CKPS = 0b10;    // 1:16 prescale
    PR2 = 124;                   // Count 0 to 124 = 125 counts.

    // Enable Timer2 interrupt
    PIE1bits.TMR2IE = 1; // Enable Timer2 interrupt
}

void main(void) {
    // Set the direction of LATB pins as outputs for LEDs
    TRISB0 = 0; // RED
    TRISB1 = 0; // GREEN
    TRISB2 = 0; // YELLOW
    
    // Initialize LEDs to low
    RED = 0;
    YELLOW = 0;
    GREEN = 0;

    // Initialize Timer2
    initializeTimer2();

    // Initialize INTCON register
    INTCON = 0; // Clear INTCON register

    // Enable global interrupts
    INTCONbits.GIE = 1;  // Enable global interrupts
    INTCONbits.PEIE = 1; // Enable peripheral interrupts

    while (1) {
        // Main program loop
        // Handle traffic light logic based on timers
        switch (traffic_state) {
            case RED_ON:
                // Check if it's time to switch to GREEN
                if (red_timer >= 20000) { // 20 seconds
                    traffic_state = GREEN_ON;
                    red_timer = 0; // Reset the red timer
                }
                break;
            case GREEN_ON:
                // Check if it's time to switch to YELLOW
                if (green_timer >= 20000) { // 20 seconds
                    traffic_state = YELLOW_ON;
                    green_timer = 0; // Reset the green timer
                }
                break;
            case YELLOW_ON:
                // Check if it's time to switch to RED
                if (yellow_timer >= 3000) { // 3 seconds
                    traffic_state = RED_ON;
                    yellow_timer = 0; // Reset the yellow timer
                }
                break;
        }
        
        // Control LEDs based on the current state
        switch (traffic_state) {
            case RED_ON:
                RED = 1;   // Turn on red LED
                GREEN = 0; // Turn off green LED
                YELLOW = 0; // Turn off yellow LED
                break;
            case GREEN_ON:
                RED = 0;   // Turn off red LED
                GREEN = 1; // Turn on green LED
                YELLOW = 0; // Turn off yellow LED
                break;
            case YELLOW_ON:
                RED = 0;   // Turn off red LED
                GREEN = 0; // Turn off green LED
                YELLOW = 1; // Turn on yellow LED
                break;
        }
    }
}

// Interrupt Service Routine for Timer2 1ms interval
void __interrupt() ISR(void) {
    // Clear Timer2 interrupt flag
    PIR1bits.TMR2IF = 0;

    // Increment the timers for each state
    red_timer++;
    green_timer++;
    yellow_timer++;
}
When I run my code, the Red LED remains on continuously, and the traffic light sequence doesn't seem to follow the expected pattern.
 
Top