PCBX
Published

Traffic Light-Arduino UNO

A simple traffic light project to learn how to light up different LEDs in sequence according to a pattern.

BeginnerFull instructions provided78
Traffic Light-Arduino UNO

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×1
Signal Indicator Accessory, EZ-LIGHT Traffic Lights
Signal Indicator Accessory, EZ-LIGHT Traffic Lights
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1

Software apps and online services

Online Simulation
PCBX Online Simulation

Story

Read more

Schematics

traffic_light_uZtyEiTZQH.png

Code

Traffic Light - Arduino UNO

Arduino
int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
int cathodePin = 9;
int greenLED = A5, yellowLED = A4, redLED = A3;

byte numbers[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};

void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(cathodePin, OUTPUT);

pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
}

void loop() {
// Green light
digitalWrite(greenLED, HIGH);
displayTime(8); // 8 seconds
digitalWrite(greenLED, LOW);
delay(1000); // 1-second pause

// Yellow light
for (int i = 0; i < 3; i++) {
digitalWrite(yellowLED, HIGH);
delay(500); // Yellow LED on for 0.5 seconds
digitalWrite(yellowLED, LOW);
delay(500); // Yellow LED off for 0.5 seconds
}
digitalWrite(yellowLED, LOW);
delay(1000); // 1-second pause

// Red light
digitalWrite(redLED, HIGH);
displayTime(8); // 8 seconds
digitalWrite(redLED, LOW);
delay(1000); // 1-second pause
}

void displayTime(int time) {
for (int i = time; i > 0; i--) {
byte num = numbers[i];
for (int j = 0; j < 7; j++) {
digitalWrite(segmentPins[j], num & (1 << j));
}
digitalWrite(cathodePin, LOW);
delay(1000); // Display each second
digitalWrite(cathodePin, HIGH);
}
}
No preview (download only).

Credits

PCBX
33 projects • 11 followers
Customer Success: Your one-stop solution for PCB and PCBA services, plus component sourcing. Enjoy FREE online simulation and EDA.

Comments