Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
PCBX
Published

Arduino-Powered Traffic Light Countdown

Transform traffic flow with an Arduino-powered countdown timer for traffic lights, enhancing safety and efficiency.

BeginnerProtip225

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Grove - OLED Display 0.66" (SSD1306)- IIC - 3.3V/5V
Seeed Studio Grove - OLED Display 0.66" (SSD1306)- IIC - 3.3V/5V
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Online Simulation
PCBX Online Simulation

Story

Read more

Schematics

traffic_light1_3NutMlZ3af.png

Code

Untitled file

Arduino
#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>



#define SCREEN_WIDTH 128 // OLED display width (pixels)

#define SCREEN_HEIGHT 64 // OLED display height (pixel)

#define OLED_RESET-1 // Reset pin # (no pins can be used -1)



Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);



const int redPin = 3;  // Red LED pin

const int greenPin = 5;  // Green LED pin

const int bluePin = 6;  // Blue LED pin



void setup() {

// Initializes the pin mode

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);



// Initialize the OLED display

if (! display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

Serial.println(F("SSD1306 allocation failed"));

while (true);  // Stop loop on failure

}

display.clearDisplay();

display.setTextSize(2);

display.setTextColor(SSD1306_WHITE);

}



void loop() {

// The red light is on for 8 seconds

digitalWrite(redPin, HIGH);



digitalWrite(greenPin, LOW);



digitalWrite(bluePin, LOW);

// Red

countdownDisplay(8);



// Blue light for 3 seconds



digitalWrite(redPin, LOW);



digitalWrite(greenPin, LOW);



digitalWrite(bluePin, HIGH);  // Blue

countdownDisplay(3);



// The green light is on for 5 seconds

digitalWrite(redPin, LOW);



digitalWrite(greenPin, HIGH);



digitalWrite(bluePin, LOW);  // Green

countdownDisplay(5);

}







void countdownDisplay(int seconds) {

for (int i = seconds;  i >= 0;  i--) {

display.clearDisplay();

display.setCursor(35, 20);  // Center display Settings

display.print(F("Countdown:"));

display.setCursor(45, 45);  // Center countdown position



display.print(i);

display.display();

delay(1000);  // Countdown every second

}

}

Credits

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

Comments

Please log in or sign up to comment.