Arduino: Simple Traffic Lights [MINI PROJECT]

Description

This is a simple Arduino circuit that imitates a traffic light controller. It’s intentionally made very simple so that it’s easy to understand the code.

Once you have understood it, try to expand on the code so that it behaves more like real traffic lights. Ex: Real traffic lights never jump straight from red to green. Instead, they light up the red and yellow for a bit, before turning on the green light.

Part list:

  • Breadboard
  • Jumper wires
  • Red LED
  • Yellow LED
  • Green LED
  • 3 x resistors 220 Ohm

Connection Diagram

Connection diagram for the traffic lights

Code

// PIN Definitions
#define LED_RED    2
#define LED_YELLOW 4
#define LED_GREEN  7

// setup() is run one time at the beginning
void setup() 
{
  pinMode(LED_GREEN,OUTPUT);   // Set pin as output
  pinMode(LED_YELLOW,OUTPUT);  // Set pin as output
  pinMode(LED_RED,OUTPUT);     // Set pin as output
}

// loop() runs repeatedly after setup() finishes
void loop() 
{
 digitalWrite(LED_GREEN,HIGH);  // turn ON green LED
 delay(2000);                   // wait 2 seconds
 digitalWrite(LED_GREEN,LOW);   // turn OFF green LED
 delay(500);                    // wait half a second
 
 digitalWrite(LED_YELLOW,HIGH);   // turn ON yellow LED
 delay(2000);                     // wait 2 seconds
 digitalWrite(LED_YELLOW,LOW);    // turn OFF yellow LED
 delay(500);                      // wait half a second
 
 digitalWrite(LED_RED,HIGH);      // turn ON red LED
 delay(2000);                     // wait 2 seconds
 digitalWrite(LED_RED,LOW);       // turn OFF red LED
 delay(500);                      // wait half a second
}