Introduction: Traffic Lights [Beginner Arduino Project]
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
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;
}
}
Attachments
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.

Participated in the
Arduino Challenge
13 Comments
5 years ago
HI
please help me only one light is working
5 years ago
I am from Singapore. I need to seek your advice. I have hooked up the circuit and downloaded the codes into my Arduino UNO. On my breadboard, the traffic lights are running smoothly as programmed but the 2 pedestrian red lights remain on all the time. Can you please advise what has gone wrong with my piece. I have checked the wirings and gone through the source codes. Nothing seems to be amiss. Thanks.
6 years ago
Great tutorial! Worked perfectly for me once I figured out what I had wired up wrong (and after I replaced a dead LED :P )
7 years ago
Hi agnivohneb
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.
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.
traffic light with turning arrows:
https://namibsands.files.wordpress.com/2010/04/05-...
https://namibsands.files.wordpress.com/2010/04/06-...
traffic light with pedestrian:
https://namibsands.files.wordpress.com/2010/04/03-...
https://namibsands.files.wordpress.com/2010/04/02-...
if you could incorporate the turning arrows for the two sets of traffic lights (A) & (B) then you'll be completing the whole set!!!!!
please email me at cameronliamreaper@gmail.com where we can chat further
10 years ago on Introduction
sir! when we run the progran it has an error...it says like this:
Binary sketch size: 2656 bytes (of a 30720 byte maximum)
avrdude: stk500_getsync(): not in sync: resp=0x30
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
what am i going to do? its my first time taking up arduino subject...hehehe
Reply 8 years ago on Introduction
1. remove wires on pin 1
2. check com serial port is correct. check in device manager
3. check drivers are installed properly
4. check board is selected correct. Tools -> board
8 years ago on Introduction
When I tried to upload I received an error message that says this:
Arduino: 1.6.4 (Windows 7), Board: "Arduino Uno"
Build options changed, rebuilding all
Sketch uses 2,462 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 68 bytes (3%) of dynamic memory, leaving 1,980 bytes for local variables. Maximum is 2,048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x8d
Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#up... for suggestions.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x8d
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x8d
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
How do I fix this?
Reply 8 years ago on Introduction
1. remove wires on pin 1
2. check com serial port is correct. check in device manager
3. check drivers are installed properly
9 years ago on Introduction
Hi, I am using the arduino UNO and i have no problems with this project. cool project
Reply 8 years ago
Can u plz upload your code?
8 years ago on Introduction
will you be able to add a buzzer that sounds when the pedgreens turn off
9 years ago on Introduction
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!
10 years ago on Introduction
sir! when we run the program it has an error....its says like this:
Binary sketch size: 2656 bytes (of a 30720 byte maximum)
avrdude: stk500_getsync(): not in sync: resp=0x30
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
what am i going to do?