Introduction: 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
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.
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
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)
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;
}
}
// 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.
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.