Back to Resources
Beginner

LED Traffic Light System

Beginner Arduino Project
1-2 hours

Build a realistic traffic light system with timed sequences. Great introduction to Arduino programming, digital outputs, and timing functions.

LED Traffic Light System

Project Overview

Learn the fundamentals of Arduino programming by building your own traffic light system! This project introduces you to digital outputs, timing functions, and basic programming logic.

The traffic light system will cycle through realistic timing sequences just like real traffic lights. You'll learn how to control LEDs, use delay functions, and create repeating loops.

This is perfect for beginners who want hands-on experience with Arduino while building something practical and fun. The concepts you learn here will be the foundation for more advanced projects.

What You'll Learn

  • Understanding digital pin outputs
  • Using delay() functions for timing
  • Creating loops and sequences
  • Basic circuit building
  • Arduino IDE fundamentals
  • Troubleshooting common issues

Key Features

Real-time sensor data display
Data validation and error handling
Serial monitor output
Adjustable refresh rates
Realistic traffic light timing sequences
Three-color LED control
Customizable timing intervals

Step-by-Step Instructions

1

Set Up the Circuit

Connect the LEDs to digital pins 13 (Red), 12 (Yellow), and 11 (Green). Each LED needs a 100Ω resistor in series to limit current.

Set Up the Circuit
2

Write the Basic Code

Start with defining pin numbers and setting them as outputs in the setup() function.

cconst int redBulbPin = 13; 
const int yellowBulbPin = 12; 
const int blueBulbPin = 8;
int delay_time = 2000;

void setup()
{
  
  pinMode(redBulbPin, OUTPUT);
  pinMode(yellowBulbPin, OUTPUT);
  pinMode(blueBulbPin, OUTPUT);
}
3

Create the Traffic Light Sequence

In the loop() function, create a realistic traffic light sequence with proper timing.

void loop()
{
  digitalWrite(redBulbPin, HIGH);
  digitalWrite(yellowBulbPin, LOW);
  digitalWrite(blueBulbPin, LOW);
  delay(delay_time * 2); // Wait for 4000 millisecond(s)
  
  digitalWrite(redBulbPin, LOW);
  digitalWrite(yellowBulbPin, HIGH);
  digitalWrite(blueBulbPin, LOW);
  delay(delay_time); // Wait for 2000 millisecond(s)
  
  digitalWrite(redBulbPin, LOW);
  digitalWrite(yellowBulbPin, LOW);
  digitalWrite(blueBulbPin, HIGH);
  delay(delay_time * 2); // Wait for 4000 millisecond(s)
}
}
4

Upload and Test

Upload the code to your Arduino and observe the traffic light sequence. Adjust timing as needed.

Upload and Test
5

Sample Code When Using Pyfirmata

Pyfirmata is a python library that allows us to use python code to control an arduni board

# TRAFFIC LIGHT CONTROL USING PYFIRMATA
# CODE BY ANDREW BENYEOGOR
# 09/01/2024
#####################

from pyfirmata import Arduino, util
from time import sleep

# Replace 'COM3' with actual port you see on the PC 
board = Arduino('COM3')

# Define pins
red = board.get_pin('d:13:o')     # digital pin 13 as OUTPUT
yellow = board.get_pin('d:12:o')  # digital pin 12 as OUTPUT
blue = board.get_pin('d:8:o')     # digital pin 8 as OUTPUT

delay_time = 2  # seconds

def set_lights(r=False, y=False, b=False):
    red.write(1 if r else 0)
    yellow.write(1 if y else 0)
    blue.write(1 if b else 0)

while True:
    set_lights(r=True)
    sleep(delay_time * 2)

    set_lights(y=True)
    sleep(delay_time)

    set_lights(b=True)
    sleep(delay_time * 2)

Interactive Demo

Tags

#arduino#beginner#electronics

Materials Needed

  • Arduino Uno R3
  • Breadboard
  • Jumper Wires
  • USB Cable
  • Red LED (5mm)
  • Yellow LED (5mm)
  • Green LED (5mm)
  • 3x 100Ω Resistors

Project Details

Difficulty:Beginner
Estimated Time:1-2 hours
Category:arduino projects
Est. Cost:$20-45
benyeogorosenwe@gmail.com

© Osenwe Benyeogor.