Project: Traffic Light

Objectives and Overview

The Arduino Traffic Light is an additional challenge that extends the previous multiple LEDs project. This project is a Going Further activity! Make sure that you’re comfortable with adding and controlling multiple LEDs before working on this project.

Lesson Objectives

  • Attach multiple LEDs to a breadboard and control them with an Arduino.
  • Solve the programming challenge to imitate the functionality of a traffic light.

Traffic Light

Now that you’ve practiced setting up (and controlling) multiple LEDs on a breadboard, it’s time to create a variation of a traffic light!

There are two versions of this project. Make sure you try both!

The template sketch that you’ll be using for this project contains some logic errors. The code works but you’ll need to follow the comments and figure out how to improve it!

Base Activity

The base activity for this project is to create an LED and Arduino powered traffic light! You’ll be using at least 3 unique LEDs that you’ll need to program to change based on what you know about traffic lights.

This experiment is designed to use the skills you’ve been practicing while also test your critical thinking and problem-solving abilities!

Supplies

  • Uno and breadboard
  • Jumper wires
  • At least 3 uniquely colored LEDs
    • Recommended: Red, Yellow, Green
  • 220 or 560 Ohm resistor for each LED

Steps

The steps for this challenge are broken into two parts: the circuit setup and the sketch. Let’s begin with the circuit setup.

Circuit

Refer to this Fritzing diagram throughout the steps:

Fritzing diagram of an Arduino and breadboard with red, yellow, and green LEDs connected

Connect the GND of the Arduino to the – rail of the breadboard.

Connect each LED to the breadboard. Align the LEDs so that the positive (long) leg is on top.

Connect the positive (long) leg of each LED to a digital pin on the Arduino. For example, connect the positive leg of each LED to a unique pin, such as pins 13, 12, and 8 in the diagram. Each LED needs its own pin, so you’ll need 3 pins.

Connect a 220 or 560 Ohm resistor between the – rail of the breadboard and the negative (short) leg of the LED.

Sketch

Now let’s take a look at the Arduino sketch. Here is the basic sketch for this project. There’s going to be some pieces that you’ll need to fix!

Gist Link: If you prefer to get the code from a GitHub Gist, go to this link. You can click the Raw button to get a format that’s easier to copy.

Suggested: Instead of copying the code, read the comments and type it yourself! This is the best way to develop an understanding of each piece.

/* Arduino Experimenter Challenge: Broken Traffic Light * This traffic light is -sooo close- to being correct, but something seems * off with the logic... * * Can you fix it? * * Hint: Think about how a functioning traffic light works and build out the logic- * what steps should the lights go in? * should two colors ever be on at the same time? * * Going Further: * after you get the light working correctly, invent your own traffic light system! * * */ // global variables go here. set and change the led pin variables to match what you need int red = 13; int yellow = 12; int green = 8; void setup() { // make sure to set the pinMode to output for EACH led variable pinMode(red, OUTPUT); pinMode(yellow, OUTPUT); pinMode(green, OUTPUT); } void loop() { changeLights(); delay(10000); //process starts over every 10 seconds } void changeLights() { //first, green is on and the other two are off: digitalWrite(red,HIGH); digitalWrite(yellow,LOW); digitalWrite(green,LOW); delay(5000); //red light for 5 seconds! //now, yellow light: digitalWrite(yellow,HIGH); digitalWrite(green,LOW); digitalWrite(red,LOW); delay(2000); //yellow light for 2 seconds! //now, green light: digitalWrite(green,HIGH); digitalWrite(red,LOW); digitalWrite(yellow,LOW); //green light! }
Code language: JavaScript (javascript)

Traffic Light: Experimenter Challenges

Once you’ve figured out how to fix the example, go ahead and make some changes and program your own light!

Experiment with different delay() values as well as different choices. Perhaps invent your own system of traffic control where you need two lights to be on in a quick pattern to signal traffic to move!