Traffic Light using 8051.

 Code : 

#include<reg51.h>                                    // Include 8051 library.
sbit red = P1^0;                                         //  Set the port where red led is connected.
sbit yellow = P1^1;                                    //  Set the port where yellow led is connected.
sbit green = P1^2;                                     //  Set the port where green led is connected.
void delay(int t);                                        // Delay between 2 leds.
void main()                                                // Main function starts.
{
    red = 1;                                                   // Red turned OFF.
    yellow = 1;                                             // Yellow turned OFF.
    green = 1;                                              // Green turned OFF.
while(1)                                     // Infinite loop.
{
red = 0;                           // Red turned ON.
delay(2);                         // Delay function calling.
red = 1;                          // Red turned OFF.
yellow = 0;                     // Yellow turned ON.
delay(2);                         // Delay function calling.
yellow = 1;                    // Yellow turned OFF.
green = 0;                      // Green turned ON.
delay(2);                        // Delay function calling.
green = 1;                      // Green turned OFF.
              }
}

void delay(int t)                                                // Delay function
{
int i,j;                                                        // Assigning integer i & j. 
for(i=0;i<100*t;i++)                               // For loop for delay
for(j=0;j<=1275;j++);                    // Nested  For loop for delay
}


:  Circuit Diagram

  Leds are connected in sinking modes. Sinking mode is when cathode of          LED is connected to Port of 8051 and anode is connected to Vcc.

            A ballast resistor is added in series with leds to limit the flow of current       through the led and prevent excess current that can blow the leds.

          3 leds are connected to represent 3 lights of signal ( Red , Green , Yellow ). 



Traffic Light  Result   : 

   Initially we make the ports(P1.0)(P1.1)(P1.2) high for the leds to turn off.

  As soon as port(P1.0) becomes low the red led forward biases and it turns on.

A delay function is used to provide an interval between 2 leds.

Once the delay time completes the port turns high the led switches off and now the 2nd port turns low and yellow light turns on.

This keeps on going because we have used a while infinite loop.


 


Comments