Introduction: Arduino Workshop-Traffic Lights

We are now going to create a set of traffic lights based on the system that will change from green to red, via amber, and back again, after a set length of time using the four-state system

Parts Required Arduino traffic Lights

Arduino Uno

Breadboard

5mm LED

100Ω Resistor

Jumper Wires

You can read this book and it will help you to learn more About Arduino Beginning Arduino

Circuit Diagram Arduino traffic Lights

Traffic Lights

Connect your circuit as in Traffic Lights. This time, we have connected three LEDs with the anode of each one going to digital pins 8, 9 and 10, via a 150Ω current-limiting resistor (or whichever value you require) for each. Figure 2-6. The circuit for Project 3 We have taken a jumper wire from the ground of the Arduino to the ground rail at the top of the breadboard. A ground wire goes from the cathode leg of each LED to the common ground rail via a current-limiting resistor (this time, connected to the cathode). For this simple circuit, it doesn’t matter if the resistor is connected between the digital pin and the anode, or between the cathode and ground, as long as it is in series with the LED

Code
int ledDelay = 10000; // delay in between changes
int redPin = 10;
int yellowPin = 9;
int greenPin = 8;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 5 seconds
digitalWrite(yellowPin, HIGH); // turn on yellow
delay(2000); // wait 2 seconds
digitalWrite(greenPin, HIGH); // turn green on
digitalWrite(redPin, LOW); // turn red off
digitalWrite(yellowPin, LOW); // turn yellow off
delay(ledDelay); // wait ledDelay milliseconds
digitalWrite(yellowPin, HIGH); // turn yellow on
digitalWrite(greenPin, LOW); // turn green off
delay(2000); // wait 2 seconds
digitalWrite(yellowPin, LOW); // turn yellow off
// now our loop repeats
}

Previous tutorial

1. Arduino Workshop-LED Flashers