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 systemParts Required
Arduino UnoBreadboard
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

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
}
Figure 2-7. The four states of a traf ic light system (image by Alex43223 from WikiMedia)
In the next project, we are going
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
Figure 2-7. The four states of a traf ic light system (image by Alex43223 from WikiMedia) | |
In the next project, we are going |
Previous tutorial
1. Arduino Workshop-LED Flashers
No comments