loading
This is a beginners project for the Arduino. I am using the Arduino Uno but should work with little or no modification on other arduino models. 

Setting up the hardware is very easy. This project is more of a training tool for learning how to program a timer list while listening for a button press.

Parts you will need.
- Arduino
- Solder-less Breadboard
- 10 LEDs
--- 2 Red
--- 2 Yellow
--- 2 Green
--- 2 Small Red
--- 2 Small Green
- 2 1Kohm Resistors
- 2 Momentary Switches (normally open)
- Jumper Wires


This video is a demonstration of the project.

Step 1: The Circuit

Step 2: Setup LEDs

Setup the lights on the breadboard like a traffic light. Red on top, then yellow, and then green. The small red and green are the pedestrian crossing signals. Again red on top and green just below.

In my images I am using the right side positive run of the breadboard as negative because its easier to put in the LED.

Put the cathode (-) into the negative run of the breadboard and the anode (+) into a hole next to it.

Step 3: Buttons

Put the buttons in so you have at least 2 pin holes space to add the wires and resistors. Place the 1kohm resistor between one side of the button and the negative run on the breadboard.

Step 4: Wire Up LEDs

There are a lot of wire in this project so try to keep it neat so you know where everything is going.

Start by linking the two power rails of the breadboard and giving the breadboard power from the arduino.

An easy way to wire the lights is to just start from the top and work your way down.

Wire guide:
LED / Pin

Side A
Red = 4
Yellow = 3
Green = 2
Small Red = 7
Small Green = 6

Side B
Red = 10
Yellow = 9
Green = 8
Small Red = 13
Small Green = 12

Step 5: Wire Up Buttons

A wire on the same side of the resistor will go to pins 5 (Side A) and 11 (Side B)
On the opposite side a wire will go to power (+3.5v)

Step 6: Make the Program

You can download the attached file or copy and paste this code to your arduino software.


// Traffic Lights
// By Ben Hovinga http://ben.hovinga.me/

// Lights {GREEN, YELLOW, RED, PEDGREEN, PEDRED}
int light [2][5] = {{2,3,4,6,7},{8,9,10,12,13}};

// Buttons {light[0],light[1]}
int btn [] = {5,11};

// Timers
int tgreen = 30000; // 30s
int tyellow = 3000; // 3s
int tred = 3000; // 3s
int tpedwarn = 5000; // 5s (must be less than tgreen)
int tpedwarnint = 500; // 0.5s (must be less than tpedwarn)

// Other Vals. Don't Change
int btnval = 0;
boolean pedwait [] = {false,false};
boolean pedactive [] = {false,false};
int direct = 0;
int stp = 1;
boolean stpset = false;
unsigned long now = 0;
unsigned long changeat = -1;
unsigned long changeatped = -1;
boolean pedredon = true;

// SETUP
void setup() {
  // Assign all Pins
  for (int x = 0; x < 2; x++){
    for (int y = 0; y < 5; y++){
      pinMode(light[x][y], OUTPUT);
    }
    // Start everything stopped (red)
    digitalWrite(light[x][2], HIGH);
    digitalWrite(light[x][4], HIGH);
  }
 
  // Assign buttons
  for (int x = 0; x < 2; x++){
    pinMode(btn[x], INPUT);
  }
}

// RUN
void loop() {
  // Update Time
  now = millis();
 
  // Check if button pressed
  for (int x = 0; x < 2; x ++){
    btnval = digitalRead(btn[x]);
    if (btnval == HIGH){
      pedwait[x] = true; // We have someone waiting to cross
    }
  }
 
  // Get into it
  switch (stp){
    // Green
    case 1:
      // Setup
      if (stpset == false){
        changeat = now + tgreen - tpedwarn; // Set timer
        // Turn on Ped
        if (pedwait[direct] == true){
          // Lights change for ped
          digitalWrite(light[direct][3], HIGH);
          digitalWrite(light[direct][4], LOW);
          // Set some vars
          pedwait[direct] = false;
          pedactive[direct] = true;
          pedredon = false;
        }
        // Turn on Green
        digitalWrite(light[direct][0], HIGH);
        // Turn off Red
        digitalWrite(light[direct][2], LOW);
       
        stpset = true; // We are setup
      }
      // Run
      else{
        if (now > changeat){ // Times up
          if (pedactive[direct] == true){
            // Turn off Ped
            digitalWrite(light[direct][3], LOW);
            digitalWrite(light[direct][4], HIGH);
            pedredon = true;
          }
          // Next step
          stp++;
          stpset = false;
        }
      }
    break;
    // Warn ped (if possible)
    case 2:
      // Setup
      if (stpset == false){
        changeat = now + tpedwarn;
        changeatped = now + tpedwarnint;
        stpset = true;
      }
      // Run
      else{
        // Flash Ped Red
        if (pedactive[direct] == true){
          if (pedredon == true && changeatped < now){
            digitalWrite(light[direct][4], LOW);
            pedredon = false;
            changeatped = now + tpedwarnint;
          }
          if (pedredon == false && changeatped < now){
            digitalWrite(light[direct][4], HIGH);
            pedredon = true;
            changeatped = now + tpedwarnint;
          }
        }
        if (now > changeat){ // Times up
          // Turn off
          digitalWrite(light[direct][0], LOW);
          digitalWrite(light[direct][4], HIGH);
          pedredon = true;
          pedactive[direct] = false;
          // Next step
          stp++;
          stpset = false;
        }
      }
    break;
    // Yellow
    case 3:
      // Setup
      if (stpset == false){
        changeat = now + tyellow;
        digitalWrite(light[direct][1], HIGH);
        stpset = true;
      }
      // Run
      else{
        if (now > changeat){ // Times up
          // Turn off
          digitalWrite(light[direct][1], LOW);
          // Next step
          stp++;
          stpset = false;
         
        }
      }
    break;
    // Red
    case 4:
      // Setup
      if (stpset == false){
        changeat = now + tred;
        digitalWrite(light[direct][2], HIGH);
        stpset = true;
      }
      // Run
      else{
        if (now > changeat){ // Times up
          // Start over
          stp = 1;
          stpset = false;
         
          // Change Direction
          if (direct == 1){
            direct = 0;
          }
          else {
            direct = 1;
          }
        }
      }
    break;
  }
}

Step 7: Upload and Run

Connect your arduino to your computer and upload the program. After it has uploaded you will see it cycle through the lights.
When you press a button it will wait for the next pass for that side to be green before turning the pedestrian light on. It will then give a warning flash before the light turns yellow.

Leave a comment and maybe some pictures if your having problems, or if not tell me about your success.
<p>Great tutorial! Worked perfectly for me once I figured out what I had wired up wrong (and after I replaced a dead LED :P )</p>
<p>Hi <a href="https://www.instructables.com/member/agnivohneb/" rel="nofollow">agnivohneb</a></p><p>i need some help please. ive recreated your project as per your wiring diagram and up loaded the code you supplied on to AutoDesk 123D circuits. my problem is that when i initiate the start up and run process the Arduino does nothing. please help me out as i know that this is supposed to work as per your YouTube video.</p><p>great design and great setup though! i like your idea... would it be possible if you could possibly add turning arrows with the pedestrian lights and the main three lights.</p><p>traffic light with turning arrows:</p><p><a href="https://namibsands.files.wordpress.com/2010/04/05-the-green-arrow.jpg" rel="nofollow">https://namibsands.files.wordpress.com/2010/04/05-...</a></p><p><a href="https://namibsands.files.wordpress.com/2010/04/06-the-red-arrow.jpg" rel="nofollow">https://namibsands.files.wordpress.com/2010/04/06-...</a></p><p>traffic light with pedestrian:</p><p><a href="https://namibsands.files.wordpress.com/2010/04/03-the-green-man.jpg" rel="nofollow">https://namibsands.files.wordpress.com/2010/04/03-...</a></p><p><a href="https://namibsands.files.wordpress.com/2010/04/02-the-red-man.jpg" rel="nofollow">https://namibsands.files.wordpress.com/2010/04/02-...</a></p><p>if you could incorporate the turning arrows for the two sets of traffic lights (A) &amp; (B) then you'll be completing the whole set!!!!!</p><p>please email me at cameronliamreaper@gmail.com where we can chat further</p>
<p>Just made this, my first arduino project. Took a little tweeking with the switches to get the &quot;crosswalk&quot; lights to work properly. Awesome! Thank you very much. excellent instructions. I copied and pasted the code as is, and had no issues. Thanks again. On to the next project.</p>
sir! when we run the progran it has an error...it says like this: <br> <br> <br>Binary sketch size: 2656 bytes (of a 30720 byte maximum) <br>avrdude: stk500_getsync(): not in sync: resp=0x30 <br>avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51 <br> <br> <br> <br>what am i going to do? its my first time taking up arduino subject...hehehe
<p>1. remove wires on pin 1</p><p>2. check com serial port is correct. check in device manager</p><p>3. check drivers are installed properly</p><p>4. check board is selected correct. Tools -&gt; board</p>
<p>When I tried to upload I received an error message that says this:</p><p> Arduino: 1.6.4 (Windows 7), Board: &quot;Arduino Uno&quot;</p><p>Build options changed, rebuilding all</p><p>Sketch uses 2,462 bytes (7%) of program storage space. Maximum is 32,256 bytes.</p><p>Global variables use 68 bytes (3%) of dynamic memory, leaving 1,980 bytes for local variables. Maximum is 2,048 bytes.</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x8d</p><p>Problem uploading to board. See <a href="http://www.arduino.cc/en/Guide/Troubleshooting#upload" rel="nofollow"> http://www.arduino.cc/en/Guide/Troubleshooting#up...</a> for suggestions.</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x8d</p><p>avrdude: stk500_recv(): programmer is not responding</p><p>avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x8d</p><p> This report would have more information with</p><p> &quot;Show verbose output during compilation&quot;</p><p> enabled in File &gt; Preferences.</p><p>How do I fix this?</p>
<p>1. remove wires on pin 1</p><p>2. check com serial port is correct. check in device manager</p><p>3. check drivers are installed properly</p>
<p>Hi, I am using the arduino UNO and i have no problems with this project. cool project </p>
Can u plz upload your code?
<p>will you be able to add a buzzer that sounds when the pedgreens turn off </p>
<p>Have built this circuit using an Arduino Micro for a traffic intersection on my N scale model railway layout. Works absolutely fantastic! Thanks for this Instructable, it is also my first Arduino project!</p>
sir! when we run the program it has an error....its says like this: <br> <br> <br> <br>Binary sketch size: 2656 bytes (of a 30720 byte maximum) <br>avrdude: stk500_getsync(): not in sync: resp=0x30 <br>avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51 <br> <br>what am i going to do?

About This Instructable

83,888views

42favorites

License:

Bio: I have a hairless cat.
More by agnivohneb:Traffic Lights [Beginner Arduino Project] Dog Leash Tripod Strap BUSINESS CARD STAND FROM RECYCLED MATERIAL 
Add instructable to: