Note: ALL OSOYOO Products for Arduino are Third Party Board which is fully compatitable with Arduino

Authorized Online Retailers:

AMAZON
amzuk

Content

      1. Introduction
      2. Preparations
      3. About this project
      4. Connection
      5. Code Program
      6. Program Running Result

Introduction

The real time traffic light controller is a complex piece of equipment which consists of power cabinet, main controller or processor, relays, control panel with switches or keys, communication ports etc.

In this lesson, we will go over how to build a traffic light circuit with an arduin microcontroller.

Preparations

Hardware

      • Osoyoo basic Board (Fully compatible with Arduino UNO rev.3) x 1
      • One Digit 7-Segment LED Display x 1
      • 74HC595 x 1
      • 200 ohm Resistor x 7
      • LEDs (Red LED x 2, Yellow LED x 2, Green LED x 2)
      • Breadboard x 1
      • Jumpers
      • USB Cable x 1
      • PC x 1

Software

      • Arduino IDE (version 1.6.4+)

About this project

The use of personal vehicles is very common now a days and a result, the number of vehicles on the roads are exponentially increasing. Roads without any supervision or guidance can lead in to traffic congestions and accidents.

Traffic Lights or Traffic Signals are signalling devices that are used to control the flow of traffic. Generally, they are positioned at junctions, intersections, ‘X’ roads, pedestrian crossings etc. and alternate the priority of who has to wait and who has to go.

The traffic lights will provide instructions to the users (drivers and pedestrians) by displaying lights of standard color. The three colors used in traffic lights are Red, Yellow and Green.

In this project, an Traffic Light Controller system is designed. It is a simple implementation of traffic lights system but can be extended to a real time system with programmable timings, pedestrian lighting etc. There is a green LED, which represents the green light. A yellow LED, which represents the yellow light. And a red LED, which represents the red light.

We will show all the hardware connections and the software needed to make this circuit work.

Connection

Build the circuit as below:

In this experiment, we use a 7-segment display to count down and set two groups of traffic lights to represent two directions, let’s say, north-south (TF1) and east-west (TF2), as shown in the above picture.

Code Program

After above operations are completed, connect the board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your board.

/*  ___   ___  ___  _   _  ___   ___   ____ ___  ____  
 * / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \|    \ 
 *| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
 * \___/(___/ \___/ \__  |\___/ \___(_)____)___/|_|_|_|
 *                  (____/ 
 *In this lesson, we will go over how to build a traffic 
 *light circuit with an arduin microcontroller.
 * Tutorial URL https://osoyoo.com/2017/08/16/traffic-light-controller/
 * CopyRight www.osoyoo.com
 */
const int red1Pin= 5; //red1 led attach to 
const int yellow1Pin =6 ; //yellow1 led attach to 
const int green1Pin= 7; //green1 led attach to 

const int red2Pin= 2; //red2 led attach to 
const int yellow2Pin =3 ; //yellow2 led attach to 
const int green2Pin= 4; //green2 led attach to 

const int STcp = 12;//Pin connected to ST_CP of 74HC595
const int SHcp = 8;//Pin connected to SH_CP of 74HC595 
const int DS = 11; //Pin connected to DS of 74HC595 
//display 1,2,3,4,5,6,7,8,9
int datArray[16] = {
  96, 218, 242, 102, 182, 190, 224, 254, 246};

void setup()
{
  pinMode(red1Pin, OUTPUT);  //set the redPin as an output
  pinMode(yellow1Pin, OUTPUT);  //set the yellowPin as an output
  pinMode(green1Pin, OUTPUT);  //set the greenPin as an output

  pinMode(red2Pin, OUTPUT);  //set the redPin as an output
  pinMode(yellow2Pin, OUTPUT);  //set the yellowPin as an output
  pinMode(green2Pin, OUTPUT);  //set the greenPin as an output
  //set pins to output
  pinMode(STcp,OUTPUT);
  pinMode(SHcp,OUTPUT);
  pinMode(DS,OUTPUT);
  Serial.begin(9600);  // start serial port at 9600 bps:
}

void loop()
{
  State1();
  State2(); 
}
void State1()
{
  digitalWrite(red1Pin,HIGH);   //turn on a red led
  for(int num = 8; num >=0; num--)   //display 9-1 and turn on a green led
  {
    digitalWrite(green2Pin,HIGH);
    digitalWrite(STcp,LOW); //ground ST_CP and hold low for transmitting
    shiftOut(DS,SHcp,MSBFIRST,datArray[num]);
    digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
    delay(1000); //wait for a second
  }
  digitalWrite(green2Pin,LOW);   //turn off the green led
  for(int num = 2 ;num >=0; num--)   //diaplay 3 to 1 and turn on the yellow led 
  {
    digitalWrite(yellow2Pin,HIGH);
    digitalWrite(STcp,LOW); //ground ST_CP and hold low for transmitting
    shiftOut(DS,SHcp,MSBFIRST,datArray[num]);
    digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
    delay(1000); //wait for a second
  }
  digitalWrite(yellow2Pin,LOW);  //turn off the yellow led
  digitalWrite(red1Pin,LOW);  //the red led finally turn off
}
void State2()
{
  digitalWrite(red2Pin,HIGH);
  for(int num = 8; num >=0; num--)
  {
    digitalWrite(green1Pin,HIGH);
    digitalWrite(STcp,LOW); //ground ST_CP and hold low for as long as you are transmitting
    shiftOut(DS,SHcp,MSBFIRST,datArray[num]);
    digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
    delay(1000); //wait for a second
  }
  digitalWrite(green1Pin,LOW);
  for(int num = 2 ;num >=0; num--)
  {
    digitalWrite(yellow1Pin,HIGH);
    digitalWrite(STcp,LOW); //ground ST_CP and hold low for as long as you are transmitting
    shiftOut(DS,SHcp,MSBFIRST,datArray[num]);
    digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
    delay(1000); //wait for a second
  }
  digitalWrite(yellow1Pin,LOW);
  digitalWrite(red2Pin,LOW);
}

Running Result

A few seconds after the upload finishes, you can see what is similar to the traffic light now. First, the 7-segment display counts down from 9s, and the red light in the TF1 and the green one in the TF2 light up. Then it counts down from 3, and the green LED in the TF2 goes out when the yellow lights up, with the TF1 red light still on. 3s later, the 7-segment counts down from 9s again. Meanwhile, the red light in the TF2 and the green in the TF1 light up. After 9s, it counts down from 3s, when the yellow light in the TF1 lights up and the red in the TF2 keeps on. And this repeats over and over again, as a traffic light would.

Although it is not the ideal implementation for real life scenarios, it gives an idea of the process behind the traffic light control system.

Note: The project implemented here doesn’t include the pedestrian crossing and pedestrian signaling in to consideration.

Once you know how the software operates, you can change the values to make the LEDs be on or off for any period of time. For example, instead of being on for 9 seconds, you can easily change it to 15 seconds or 30 seconds. You could make the yellow LED be on just for 1 second or 2 seconds.