Smart Traffic Light System Using Arduino

Introduction:
 
In this article we are going to build little components with LED's called a  Smart Traffic Light System 
 
Required Components: 
  • Arduino Uno
  • Led-3
  • Bread Board
  • Push Button 
  • Some Wires  
Connections:
  • Connect LED aode pin to the Arduino digital pin 10,11,12.
  • And Cathode pin to the Gnd.
  • Push button to the digital pin 7. 
Program
  1. int one = 10;  
  2. int two = 11;  
  3. int three = 12;  
  4. int btn = 7;  
  5. int state = 0;  
  6. void setup()  
  7. {  
  8.     pinMode(one, OUTPUT);  
  9.     pinMode(two, OUTPUT);  
  10.     pinMode(three, OUTPUT);  
  11. }  
  12. void loop()  
  13. {  
  14.     if (digitalRead(btn))  
  15.     {  
  16.         if (state == 0)  
  17.         {  
  18.             setLights(HIGH, LOW, LOW);  
  19.             state = 1;  
  20.         } else if (state == 1)   
  21.         {  
  22.             setLights(HIGH, HIGH, LOW);  
  23.             state = 2;  
  24.         } else if (state == 2)   
  25.         {  
  26.             setLights(LOW, LOW, HIGH);  
  27.             state = 3;  
  28.         } else if (state == 3)  
  29.         {  
  30.             setLights(LOW, HIGH, LOW);  
  31.             state = 0;  
  32.         }  
  33.         delay(1000);  
  34.     }  
  35. }  
  36. void Traffic(int red, int yellow, int green)  
  37. {  
  38.     digitalWrite(one, red);  
  39.     digitalWrite(two, yellow);  
  40.     digitalWrite(three, green);  
  41. }  
Explanation:
 
Declare the variables as one, two, three btn in the integer data type and assign the digital pins to that variables and set the state as "0." In the setup() function we want to set the pinMode for our LED's and in the loop() function we want to give the condition for our LED's, such as, first we want to read the button value from the analog pin. For that we use the built in function called digitalRead. It reads the analog value from the Arduino board
  1. if (state==0) set the led1 as HIGH   
  2. if (state==1) led1,led2 are HIGH   
  3. if (state==2) led3 is HIGH   
  4. if(state==2)led2 is HIGH  
And set the delay time as 1000 s for the 3 led's and create the function called Traffic with parameters such as int red, int yellow, int green.
     
DigitalWrite: 
 
Writes the HIGH or LOW value to the digital pins.  Inside the Traffic function we want to set the digitalWrite function like this, 
  1. void Traffic(int red, int yellow, int green)    
  2. {     
  3.    digitalWrite(one, red); //one    
  4.    digitalWrite(two, yellow);   //two 
  5.    digitalWrite(three, green);     //three
  6. }  
Output
 

Read more articles on Arduino:

Up Next
    Ebook Download
    View all
    Learn
    View all