Traffic Light Model

by JeanC117 in Circuits > Arduino

5824 Views, 12 Favorites, 0 Comments

Traffic Light Model

2016-07-21 15.02.09.jpg
2016-07-21 15.09.06.jpg

We are all familiar with traffic lights -- they are one of our biggest pet peeves, yet creates order in society by giving us all a common understanding needed for navigating cars. Traffic lights are one of the great advances in technologies, many take for granted. Prior to the invention of timed traffic lights, multiple people would have to manually direct the traffic, with a popular example is the London Bridge.

This project introduces the fundamental skill of driving multiple LEDs, understanding timing, and programming digital output signals using an Arduino; learning by design.

Tools and Materials

2016-07-21 14.58.10.jpg
  • Arduino Uno
  • Breadboard
  • Green LED
    Yellow LED
    Red LED
  • Jumper Wires
  • 100 Ohm Resistors

Connecting the LEDs to Arduino

2016-07-21 15.02.43.jpg
Screen Shot 2017-07-26 at 12.50.08 PM.png
2016-07-21 15.12.10.jpg
  1. Connect the ground pin, indicated as flat edges of the LED, to ground. Since the Arduino only has limited ground pins, connect them to a common ground indicated as the blue bar on the breadboard
  2. Connect any pin in this bleu bar on the breadboard to any ground pin on the Arduino. Now all the LEDs are hooked up to ground
  3. Connect the positive terminal of each of the LEDs to a 100Ω Resistor.
  4. Then, connect the Red LED's resistor to the digital pin 11 of the Arduino. This is indicated as the Orange Wire.
  5. Similarly, connect the Yellow LED's resistor to the digital pin 10 of the Arduino. This is indicated as the Yellow Wire.
  6. Finally, connect the Green LED's resistor to the digital pin 9 of the Arduino. This is indicated as the White Wire.

Make sure them like traffic lights, where red is on top, followed by yellow, and then green.

Coding

Screen Shot 2017-07-22 at 4.19.11 PM.png
// declare the pin wherein the red, yellow and green LEDs are connected, respectively
int redPin = 9;
int yellowPin = 10; 
int greenPin = 11;
void setup() { 


// declare each of the coloured pins as digital output signals
pinMode(redPin, OUTPUT); 
pinMode(yellowPin, OUTPUT); 
pinMode(greenPin, OUTPUT); 
} 

void loop() {

// green light
setLights (0, 0, 1); // stay at a green light for 5 seconds delay(5000);
// yellow light setLights (0, 1, 0); // stay at a yellow light for 1 second or 1000 milliseconds delay(1000);
// stop light 
setLights (1, 0, 0); // stay at a stop light for 5 seconds or 5000 milliseconds delay(5000);
}
// write a custom function that sets a particular coloured LED ON or off
void setLights(int red, int yellow, int green) { 

// sets each of the coloured lights according to the parameters passed by the user
// control a colour as they wish by using 1 as high and 0 as low


digitalWrite(redPin, red); 
digitalWrite(yellowPin, yellow); 
digitalWrite(greenPin, green); 

}

Traffic Model Demo

Traffic Lights Demo (Sped Ip)

The code is a demonstration of a sped up traffic light, and does not reflect the actual timing of the traffic light, since it would be quite a long and boring video demonstration. Here, it is demonstrated that the light will cycle through green, yellow, then red.

Changing the delays allows for the line of code preceding the delay to continue running until the delay count is finished.