Utkarsh TiwariVidit Shah
Published © GPL3+

Traffic Lights

The sequence of traffic signals is red, red and amber together, green, amber, and then back to red.

BeginnerShowcase (no instructions)1 hour16,334
Traffic Lights

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
3 mm LED: Red
3 mm LED: Red
×1
3 mm LED: Yellow
3 mm LED: Yellow
×1
3 mm LED: Green
3 mm LED: Green
×1
Resistor 221 ohm
Resistor 221 ohm
×3
Resistor 1k ohm
Resistor 1k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
MALE TO MALE
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Red colored wires show Positive terminal.
Black colored wires show Negative terminal.
Since all the LED's in schematic are RED in color, I've changed the wire colors to show various LED.

Code

Code

Arduino
//Traffic Lights


//Declaring the LED and button pins and initial state
int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int buttonPin = 5;
int state = 0;

void setup()
{
  //Declaring the functioning of LED and Button
pinMode(redPin, OUTPUT);  
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop()
{
if (digitalRead(buttonPin))
{
if (state == 0)
{
digitalWrite(redPin,HIGH);
digitalWrite(yellowPin,LOW);
digitalWrite(greenPin,LOW);
state = 1;
}
else if (state == 1)
{
digitalWrite(redPin,HIGH);
digitalWrite(yellowPin,HIGH);
digitalWrite(greenPin,LOW);
state = 2;
}
else if (state == 2)
{
digitalWrite(redPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(greenPin,HIGH);
state = 3;
}
else if (state == 3)
{
digitalWrite(redPin,LOW);
digitalWrite(yellowPin,HIGH);
digitalWrite(greenPin,LOW);
state = 0;
}
delay(1000); //the delay can be changed to 2 seconds,3 seconds,etc.
}
}
void setLights(int red, int yellow,
int green)
{
digitalWrite(redPin, red);
digitalWrite(yellowPin, yellow);
digitalWrite(greenPin, green);
}

Credits

Utkarsh Tiwari

Utkarsh Tiwari

10 projects • 40 followers
Trying to FIND myself! :P Interested in DIY's and coding!
Vidit Shah

Vidit Shah

10 projects • 39 followers
Programmer

Comments