You are on page 1of 10

Food

Living

Outside

Play

Technology

Workshop

Traffic Lights [Beginner Arduino Project]


by agnivohneb on January 14, 2012 Table of Contents Traffic Lights [Beginner Arduino Project] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intro: Traffic Lights [Beginner Arduino Project] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 1: The Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 2: Setup LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 3: Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 4: Wire up LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 5: Wire up Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Step 6: Make the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 2 3 4 5 6 7 9

Step 7: Upload and Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

Intro: Traffic Lights [Beginner Arduino Project]


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

Step 1: The Circuit

Image Notes 1. Pedestrian Lights 2. Side A 3. Side B

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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.

Image Notes 1. Side A 2. Side B

Image Notes 1. Anode ( ) 2. Cathode (-)

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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.

Image Notes 1. 1Kohm Brown = 1 Black = 0 Red = 2 Gold = +- 5% tolerance Put it together like this 1 0 00 (2 zeros) = 1000 = 1k (shortened)

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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

Image Notes 1. 3.5v 2. Ground 3. Side A 4. Side B

Image Notes 1. Pedestrian Lights 2. Side A 3. Side B

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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)

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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 } }

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

// 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

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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; } }

File Downloads

traffic_lights.ino (4 KB) [NOTE: When saving, if you see .tmp as the file ext, rename it to 'traffic_lights.ino']

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

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.

Related Instructables

Arduino traffic lights by zoltzerino

Arduino Traffic Light for Beginners by Dan_the_Man

Arduino: Making a set of traffic lights by unknowndomain

Arduino Traffic Light Controller w/Remote Control by Auger Duval

Simple Arduino Traffic Lights by bjarni1995

DIY Music Synchronized LED Traffic Light (Photos) by murray484

http://www.instructables.com/id/Traffic-Lights-Beginner-Arduino-Project/

You might also like