Traffic Light

Traffic Lights


In this project you’re going to build a traffic lights system:
·         There are 3 LEDs with different colors (green, yellow and red) to mimic the traffic lights for the cars
·         There are 2 LEDs with different colors (green and red) to mimic the traffic lights for the pedestrians

·         There is a pushbutton to mimic the ones in the pedestrians traffic lights

Parts Required

Grab all the needed components for this project.

·         1x Breadboard
·           Arduino UNO – read Best Arduino Starter Kits
·         3x 5mm LED (1x red, 1x yellow, 1x green)
·         2x 3mm LED (1x red, 1x green)
·         5x 220Ohm Resistor
·         1x 10kOhm Resistor
·         1x pushbutton
·         Jumper Wires
I’m using LEDs of different sizes but if you don’t have LEDs of different sizes, it is ok. The project still works.


Assemble all the parts by following the schematics below.


You don’t need any library for this code. The code is very simple. Here’s some tips to better understand what’s going on.
·         The car light is always green, and so the pedestrian light is always red unless someone presses the button.
·         When someone presses the button here’s what happens:
·         The car light changes to yellow and then to red
·         The pedestrian light changes to green
·         The lights are in this state for a while  (in  the  code  this  time  is  the  variable crossTime)
·         The pedestrian green light flashes and goes to red
·         The car light changes from red to green
All these actions will be inside the function changeLights(). Everytime you want to change the lights, you just need to call the changeLights() function.
Copy the following code to your Arduino IDE, and upload it to your Arduino board. Make sure you have the right board and COM port selected.

int redCar = 13; int yellowCar = 12; int greenCar = 11; int greenPed = 2; int redPed = 3;
int button = 7;
int crossTime = 2000; unsigned long changeTime;

void setup() {
// initialize timer changeTime = millis();
// here we are initializing our pins as outputs pinMode(redCar, OUTPUT);
pinMode(yellowCar, OUTPUT); pinMode(greenCar, OUTPUT); pinMode(redPed, OUTPUT); pinMode(greenPed, OUTPUT); pinMode(button, INPUT);
//turn on the green light digitalWrite(greenCar, HIGH); digitalWrite(redPed, HIGH); digitalWrite(redCar, LOW); digitalWrite(yellowCar, LOW);
digitalWrite(greenPed, LOW);

void loop() {
// this variable will tell us if the button is pressed int state = digitalRead(button); Serial.println(state);
// if the button is pressed and if it has passed 5 seconds since last button press
if (state == HIGH && (millis() - changeTime) > 5000) {
//call the function to change the lights changeLights();

void changeLights() {
digitalWrite(greenCar, LOW);    // the green LED will turn off digitalWrite(yellowCar, HIGH); // the yellow LED will turn on for 2 second delay(2000);

digitalWrite(yellowCar, LOW); // the yellow LED will turn off digitalWrite(redCar, HIGH); // the red LED will turn on for 5 seconds

digitalWrite(redPed, LOW); digitalWrite(greenPed, HIGH); delay(crossTime);

// flash the ped green
for (int x=0; x<10; x++) { digitalWrite(greenPed, LOW); delay(100); digitalWrite(greenPed, HIGH); delay(100);
digitalWrite(greenPed, LOW); digitalWrite(redCar, LOW); digitalWrite(redPed, HIGH); digitalWrite(greenCar, HIGH);

changeTime = millis();


When you press the button, the light for the cars changes from green to red, and the pedestrian light changes from red to green.