Complete Guide How to Controlling Traffic using Arduino

Controlling Traffic

In this tutorial, I will show you Complete Guide How to Controlling Traffic using Arduino step by step complete process

Now let’s set your newfound knowledge to apply by solving a hypothetical problem. As the town planner for a rural shire, you have a problem with a single-lane bridge that joins the river. Every week, one or two accidents occur at night,  if tired drivers race across the bridge without first stopping to see if the road is open.

You have advised that traffic lights be fixed, but the mayor wants to see them demonstrated before signing off on the purchase. You could rent short lights, but they’re expensive. Instead, you’ve decided to make a model of the bridge with working traffic lights using LEDs and an Arduino.


≡ Amie


Our Amie is to establish three-color traffic lights at each end of the single-lane bridge. The lights allow traffic to flow only in one way at a time. When sensors found at either end of the bridge detect a car waiting at a red light, the lights will change and allow the traffic to flow in the opposite way.


≡ Algorithm


We’ll work two buttons to simulate the vehicle sensors at each end of the bridge. Every set of lights will have red, yellow, and green LEDs.  the system will permit traffic to flow from west to east, so the west-facing lights will be set to green and the east-facing lights will be set to red.

If a vehicle approaches the bridge (modeled by pressing the button) and the light is red, the system will turn the light on the reverse side from green to yellow to red, and then wait a set period of time to allow any vehicles already on the bridge to finish crossing.

Next, the yellow light on the waiting vehicle’s side will blink as a “get ready” notice for the driver, and finally, the light will change to green. The light will remain green until a vehicle approaches the other side, at which point the process repeats.


≡ Hardware


Here’s what you’ll need to create this project:

  1. Two red LEDs (LED1 and LED2)
  2. Two yellow LEDs (LED3 and LED4)
  3. Two green LEDs (LED5 and LED6)
  4. Six 560 Ω resistors (R1 to R6)
  5. Two 10 kΩ resistor (R7 and R8)
  6. Two 100 nF capacitors (C1 and C2)
  7. Two push buttons (S1 and S2)
  8. One medium-sized breadboard
  9. One Arduino and USB cable Various connecting wires.

≡ Schematic Controlling Traffic using Arduino


we’re dominating only six LEDs and receiving input from two buttons, the design will not be too difficult. Figure 1 shows the schematic for our project.

Controlling Traffic
Controlling Traffic

≡ Sketch Controlling Traffic using Arduino


// define the pins that the buttons and lights are connected to:
1  #define westButton 7
  #define eastButton 13
  #define westRed    2
  #define westYellow 1
  #define westGreen  0
  #define eastRed    12
  #define eastYellow 11
  #define eastGreen  10

  #define yellowBlinkTime 500 // 0.5 seconds for yellow light blink

2 boolean trafficWest = true;  // west = true, east = false
3 int     flowTime    = 10000; // amount of time to let traffic flow
4 int     changeDelay = 2000;  // amount of time between color changes

  void setup()
  {
    // setup digital I/O pins
    pinMode(westButton, INPUT);
    pinMode(eastButton, INPUT);
    pinMode(westRed,    OUTPUT);
    pinMode(westYellow, OUTPUT);
    pinMode(westGreen,  OUTPUT);
    pinMode(eastRed,    OUTPUT);
    pinMode(eastYellow, OUTPUT);
    pinMode(eastGreen,  OUTPUT);

    // set initial state for lights - west side is green first
    digitalWrite(westRed,    LOW);
    digitalWrite(westYellow, LOW);
    digitalWrite(westGreen,  HIGH);
    digitalWrite(eastRed,    HIGH);
    digitalWrite(eastYellow, LOW);
    digitalWrite(eastGreen,  LOW);
  }

  void loop()
  {
    if ( digitalRead(westButton) == HIGH ) // request west>east traffic flow
    {
      if ( trafficWest != true )
  // only continue if traffic flowing in the opposite (east) direction
      {
        trafficWest = true; // change traffic flow flag to west>east
        delay(flowTime);    // give time for traffic to flow
        digitalWrite(eastGreen, LOW); // change east-facing lights from green
                                      // to yellow to red
        digitalWrite(eastYellow, HIGH);
        delay(changeDelay);
        digitalWrite(eastYellow, LOW);
        digitalWrite(eastRed, HIGH);
        delay(changeDelay);
        for ( int a = 0; a < 5; a++ ) // blink yellow light
        {
          digitalWrite(westYellow, LOW);
          delay(yellowBlinkTime);
          digitalWrite(westYellow, HIGH);
          delay(yellowBlinkTime);
        }
        digitalWrite(westYellow, LOW);
        digitalWrite(westRed, LOW); // change west-facing lights from red to green
        digitalWrite(westGreen, HIGH);
      }
    }

    if ( digitalRead(eastButton) == HIGH ) // request east>west traffic flow
    {
      if ( trafficWest == true )
  // only continue if traffic flow is in the opposite (west) direction
      {
        trafficWest = false; // change traffic flow flag to east>west
        delay(flowTime);     // give time for traffic to flow
        digitalWrite(westGreen, LOW);
  // change west lights from green to yellow to red
        digitalWrite(westYellow, HIGH);
        delay(changeDelay);
        digitalWrite(westYellow, LOW);
        digitalWrite(westRed, HIGH);
        delay(changeDelay);
        for ( int a = 0 ; a < 5 ; a++ ) // blink yellow light
        {
          digitalWrite(eastYellow, LOW);
          delay(yellowBlinkTime);
          digitalWrite(eastYellow, HIGH);
          delay(yellowBlinkTime);
        }
        digitalWrite(eastYellow, LOW);
        digitalWrite(eastRed, LOW); // change east-facing lights from red to green
        digitalWrite(eastGreen, HIGH);
      }
    }
  }

Our sketch begins by using #define at 1 to associate digital pin numbers with labels for all the LEDs worked, as well as the two buttons. We have red, yellow, and green LEDs and a button all for the west and east sides of the bridge. The Boolean variable traffic west at 2 is utilized to keep track of which way the traffic is flowing—true is west to east, and false is east to west.

NOTE

Notice that trafficWest is a single Boolean variable with the traffic direction set as either true or false. Having a single variable like this alternatively of two (one for east and one for west) ensures that both directions cannot accidentally be true at the same time, which helps avoid a crash!

The integer variable flowTime at 3 is the minimum period of time that vehicles have to cross the bridge. If a vehicle pulls up at a red light, the system delays this period to give the opposing traffic time to cross the bridge. The integer variable changeDelay at 4 is the period of time between the traffic lights switching from green to yellow to red. Ere the sketch enters the void loop() section, it is set for traffic to flow from west to east in void setup().


≡Running the Sketch


Once it’s working, the sketch does nothing until one of the buttons is pressed. If the east button is pressed, the line

if ( trafficWest == true )

assures that the lights change only if the traffic is heading in the opposite way. The rest of the code section is composed of a easy sequence of waiting and then of turning on and off various LEDs to simulate the traffic light operation.


≡ Analog vs. Digital Signals


In this section, you’ll see the difference between digital and analog signals, and you’ll learn how to measure analog signals with the analog input pins.

Until now, our sketches have been utilizing digital electrical signals, with just two discrete levels. Especially, we used digitalWrite(pin, HIGH) and digitalWrite(pin, LOW) to blink an LED and digitalRead() to measure whether a digital pin had a voltage used to it (HIGH) or not (LOW). Figure 2 is a visual image of a digital signal that alternates between high and low.

 A digital signal, with HIGHs appearing as horizontal lines at the top, and LOWs appearing at the bottom
Figure 2. A digital signal, with HIGHs appearing as horizontal lines at the top, and LOWs appearing at the bottom

Unlike digital signals, analog signals can vary with an infinite number of steps between high and low. For example, Figure 3 shows an analog signal of a sine wave. Notice in the figure that as time proceeds, the voltage moves fluidly between high and low levels.

An analog signal of a sine wave
Figure 3. An analog signal of a sine wave

With our Arduino, high is closer to 5 V and low is closer to 0 V, or GND. We can measure the voltage values of an analog signal with our Arduino utilizing the six analog inputs shown in Figure 4. These analog inputs can safely measure voltages from 0 (GND) to no more than 5 V.

 Analog inputs on the Arduino Uno
Figure 4. Analog inputs on the Arduino Uno

If you use the function analogRead(), then the Arduino will return a number between 0 and 1,023 in relationship to the voltage applied to the analog pin. For example, you might use analogRead() to store the value of analog pin zero in the integer variable a:

a = analogRead(0); // read analog input pin 0 (A0) // returns 0 to 1023 which is usually 0.000 to 4.995 volts

All Arduino tutorial available Click here

 ARDUINO TUTORIAL 

 37 total views,  1 views today