World of Electronics

Traffic Light Controller! Full Tutorial Code.

For this tutorial, you’ll need:

  1. 1 x Arduino UNO
  2. 9 x Resistor 330E
  3. 9 x LED 5MM
  4. Jumper Wires
  5. 3x Mini BreadBoard

Please note that the items needed will be updated with each part!

First Part: Explaining the project & Figure Out How to Know if a Led is Really ON?

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

Test if a LED is ON:

define led 8
 define feedback 7
 int i =0;
 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(led, OUTPUT);
   pinMode(feedback, INPUT);
 digitalWrite(led, LOW);
 }
 void loop() {
   // put your main code here, to run repeatedly:
 digitalWrite(led, HIGH);
 if (digitalRead(feedback) == 1)
     {
       Serial.println("The led is ON");
     }
   else
   {
     Serial.println("The led is damaged");
   }
 delay(500);
 }

Second Part: Simple Order Without Sensor Feedback

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

Simple Order Without Sensor Feedback Code:

 define TLAR 3 // RED Traffic Light for Road A
 define TLAY 4 // Yellow Traffic Light for Road A
 define TLAG 5 // Green Traffic Light for Road A
 define TLBCR 6 // RED Traffic Light for Road B & Road C
 define TLBCY 7 // Yellow Traffic Light for Road B & Road C
 define TLBCG 8 // Green Traffic Light for Road B & Road C
 define TLDR 9 // RED Traffic Light for Road D
 define TLDY 10 // Yellow Traffic Light for Road D
 define TLDG 11 // Green Traffic Light for Road D
 define TrafficLedRoadA 1 //This is the reference number for the traffic lights of road A
 define TrafficLedRoadBC 2 //This is the reference number for the traffic lights of road B and C
 define TrafficLedRoadD 3 //This is the reference number for the traffic lights of road D
 define IntinialState 4 //This is the reference number for the initial State of the system
 define red 10 //This is the reference number for the red color
 define green 20 //This is the reference number for the green color
 define yellow 30 //This is the reference number for the yellow color
 define IntinialStateColor 50 //This is the reference number of the initiale state color lighted when the system in UP for the first time
 define ERRORLIGHTS 404 //This is the reference number for the error State
 define ERRORLIGHTSTIMEREPEAT 3 //This is the number of time the yellow lights will blink when there is an error
 define ERRORLIGHTSTIME 500 //This is the delay time between each ON & OFF for the error lights
 define WaitingTime 9000 //9 Seconds This is the time of the the red light will be ON or the GREEN light will be ON.
 define GreenTime 7000 //7 Seconds is the green time (of the 9 total seconds)
 define YellowTime 2000 //2 Seconds is the yellow time (of the 9 total seconds)
 define SecurityTime 1000 //1 Second is the time to switch the green light to another traffic light. It's when all teh traffic lights are RED ON. For security purposes
 int turn=IntinialState; //This variable is used to pass between the turns of the traffic lights
 void turnTrafficLedON (int TrafficLedNumber, int colorNumber); // This function will light the specified color based on the traffic light given
 /*
  Example:
 turnTrafficLedON(1, 10);
 This will trun RED light ON in TrafficLedRoadA and turn Yellow and Green Lights OFF on the same TrafficLedRoadA
  */
 //the turn is ROAD D, ROAD A, ROAD B & C
 void setup() {
   // put your setup code here, to run once:
 //Define all the traffic lights as output - till now we don't have an input in this program
 pinMode(TLAR, OUTPUT);
   pinMode(TLAY, OUTPUT);
   pinMode(TLAG, OUTPUT); 
 pinMode(TLBCR, OUTPUT);
   pinMode(TLBCY, OUTPUT);
   pinMode(TLBCG, OUTPUT);
 pinMode(TLDR, OUTPUT);
   pinMode(TLDY, OUTPUT);
   pinMode(TLDG, OUTPUT);
 //Turn OFF all the outputs  
 digitalWrite(TLAG, LOW);
   digitalWrite(TLAY, LOW);
   digitalWrite(TLAR, LOW);
 digitalWrite(TLBCG, LOW);
   digitalWrite(TLBCY, LOW);
   digitalWrite(TLBCR, LOW);
 digitalWrite(TLDG, LOW);
   digitalWrite(TLDY, LOW);
   digitalWrite(TLDR, LOW);
 //start the serial communication for debugging purposes if needed
 //  Serial.begin(9600);
 //  Serial.println("Serial Communication started");
 }
 void loop() {
   // put your main code here, to run repeatedly:
 switch (turn)
   {
     case IntinialState:
   turnTrafficLedON(IntinialState, IntinialStateColor);   delay(WaitingTime);   turn = TrafficLedRoadD;   break; case TrafficLedRoadD:   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, green);   delay(GreenTime);   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, yellow);   delay(YellowTime);   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, red);   delay(SecurityTime);   turn = TrafficLedRoadA;   break; case TrafficLedRoadA:   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadD, red);   turnTrafficLedON(TrafficLedRoadA, green);   delay(GreenTime);   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadD, red);   turnTrafficLedON(TrafficLedRoadA, yellow);   delay(YellowTime);   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, red);   delay(SecurityTime);   turn = TrafficLedRoadBC;   break; case TrafficLedRoadBC:   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, red);   turnTrafficLedON(TrafficLedRoadBC, green);   delay(GreenTime);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, red);   turnTrafficLedON(TrafficLedRoadBC, yellow);   delay(YellowTime);   turnTrafficLedON(TrafficLedRoadBC, red);   turnTrafficLedON(TrafficLedRoadA, red);   turnTrafficLedON(TrafficLedRoadD, red);   delay(SecurityTime);   turn = TrafficLedRoadD;   break; default:   turnTrafficLedON(ERRORLIGHTS, ERRORLIGHTS);   break;
 }
 }
 //define the work of the functions declared in the beginning of the sketch
 void turnTrafficLedON (int TrafficLedNumber, int color)
 {
 //InitialSate
 if (TrafficLedNumber == IntinialState && color == IntinialStateColor) // The system is Up for the first time.
   {
     //All the Traffic Leds must be RED and the rest colors OFF
 //Turn Red Lights ON for all the traffic lights digitalWrite(TLAR, HIGH); digitalWrite(TLBCR, HIGH); digitalWrite(TLDR, HIGH); //Turn Yellow Lights OFF for all the traffic Lights digitalWrite(TLAY, LOW); digitalWrite(TLBCY, LOW); digitalWrite(TLDY, LOW); //Turn Green Lights OFF for all the traffic Lights digitalWrite(TLAG, LOW); digitalWrite(TLBCG, LOW); digitalWrite(TLDG, LOW);
 }
 //RED
 if (TrafficLedNumber == TrafficLedRoadA && color == red) // The RED traffic lights on RoadA must be turned ON
   {
     digitalWrite(TLAG, LOW);
     digitalWrite(TLAY, LOW);
     digitalWrite(TLAR, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadBC && color == red) // The RED traffic lights on RoadB & Road C must be turned ON
 {
     digitalWrite(TLBCG, LOW);
     digitalWrite(TLBCY, LOW);
     digitalWrite(TLBCR, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadD && color == red) // The RED traffic lights on RoadD must be turned ON
 {
     digitalWrite(TLDG, LOW);
     digitalWrite(TLDY, LOW);
     digitalWrite(TLDR, HIGH);
 }
 //YELLOW
 if (TrafficLedNumber == TrafficLedRoadA && color == yellow) // The YELLOW traffic lights on RoadA must be turned ON
   {
     digitalWrite(TLAG, LOW);
     digitalWrite(TLAR, LOW);
     digitalWrite(TLAY, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadBC && color == yellow) // The YELLOW traffic lights on RoadB & Road C must be turned ON
 {
     digitalWrite(TLBCG, LOW);
     digitalWrite(TLBCR, LOW);
     digitalWrite(TLBCY, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadD && color == yellow) // The YELLOW traffic lights on RoadD must be turned ON
 {
     digitalWrite(TLDG, LOW);
     digitalWrite(TLDR, LOW);
     digitalWrite(TLDY, HIGH);
 }
 //GREEN
 if (TrafficLedNumber == TrafficLedRoadA && color == green) // The GREEN traffic lights on RoadA must be turned ON
 {
     digitalWrite(TLAY, LOW);
     digitalWrite(TLAR, LOW);
     digitalWrite(TLAG, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadBC && color == green) // The GREEN traffic lights on RoadB & Road C must be turned ON
 {
     digitalWrite(TLBCY, LOW);
     digitalWrite(TLBCR, LOW);
     digitalWrite(TLBCG, HIGH);
 }
 if (TrafficLedNumber == TrafficLedRoadD && color == green) // The GREEN traffic lights on RoadD must be turned ON
 {
     digitalWrite(TLDY, LOW);
     digitalWrite(TLDR, LOW);
     digitalWrite(TLDG, HIGH);
 }
 //ERROR VALUES
 if (TrafficLedNumber == ERRORLIGHTS && color == ERRORLIGHTS) // ERROR!! The YELLOW LIGHTS will Blink for 3 times
   {
     //Make sure all the LIGHTS are OFF
   //YELLOW OFF   digitalWrite(TLAY, HIGH);   digitalWrite(TLBCY, HIGH);   digitalWrite(TLDY, HIGH);   //RED OFF   digitalWrite(TLAR, HIGH);   digitalWrite(TLBCR, HIGH);   digitalWrite(TLDR, HIGH);   //GREEN OFF   digitalWrite(TLAG, HIGH);   digitalWrite(TLBCG, HIGH);   digitalWrite(TLDG, HIGH);         for (int i=0; i<ERRORLIGHTSTIMEREPEAT; i++) //BLINK The YELLOW LIGHTS indicating an error {   digitalWrite(TLAY, HIGH);   digitalWrite(TLBCY, HIGH);   digitalWrite(TLDY, HIGH);   delay(ERRORLIGHTSTIME);   digitalWrite(TLAY, LOW);   digitalWrite(TLBCY, LOW);   digitalWrite(TLDY, LOW); }
 }
 }