Traffic Light
Traffic Light
Let's modify the materials and code for a more enhanced version of
the traffic light system. In this new version, we'll simulate a simple
intersection with two traffic lights (for north-south and east-west
directions) and add a pedestrian button that can trigger a pedestrian
signal when pressed.
Modified Materials:
6. Breadboard
7. Jumper Wires
8. Ultrasonic sensor
Circuit Setup:
3. Pedestrian Button:
o The other leg connects to GND, and you can use a 10k-ohm
pull-down resistor between the button pin and GND to
ensure the pin reads LOW when the button is not pressed.
4. Grounding:
o Connect the negative terminals (cathodes) of all LEDs to GND
through 220-ohm resistors.
o Connect the GND rail of the breadboard to the GND pin on the
Arduino.
cpp
Copy code
// Define the pins for the North-South and East-West traffic lights
void setup() {
pinMode(redNS, OUTPUT);
pinMode(yellowNS, OUTPUT);
pinMode(greenNS, OUTPUT);
pinMode(redEW, OUTPUT);
pinMode(yellowEW, OUTPUT);
pinMode(greenEW, OUTPUT);
digitalWrite(redNS, LOW);
digitalWrite(yellowNS, LOW);
digitalWrite(greenNS, LOW);
digitalWrite(redEW, LOW);
digitalWrite(yellowEW, LOW);
digitalWrite(greenEW, LOW);
void loop() {
if (digitalRead(buttonPin) == LOW) {
pedestrianRequest = true;
if (pedestrianRequest) {
pedestrianWalk();
} else {
trafficLightCycle();
}
void trafficLightCycle() {
digitalWrite(greenNS, HIGH);
digitalWrite(redNS, LOW);
digitalWrite(yellowNS, LOW);
digitalWrite(greenNS, LOW);
digitalWrite(yellowNS, HIGH);
digitalWrite(yellowNS, LOW);
digitalWrite(redNS, HIGH);
digitalWrite(greenEW, HIGH);
digitalWrite(redEW, LOW);
digitalWrite(yellowEW, LOW);
digitalWrite(greenEW, LOW);
digitalWrite(yellowEW, HIGH);
digitalWrite(yellowEW, LOW);
digitalWrite(redEW, HIGH);
void pedestrianWalk() {
digitalWrite(redNS, HIGH);
digitalWrite(redEW, HIGH);
digitalWrite(greenNS, LOW);
digitalWrite(greenEW, LOW);
delay(5000);
pedestrianRequest = false;
Code Breakdown:
2. Pedestrian Button:
5. Button Debouncing:
6. Pedestrian Request:
Testing:
Once you upload this modified code to the Arduino, the system will
behave as follows:
1. Normal Operation:
2. Pedestrian Request:
o After that, the system resumes the regular traffic light cycle.
Optional Enhancements:
This modified setup makes the traffic light system more interactive and
realistic by simulating pedestrian crossings and handling both directions of
traffic.
4o mini