Traffic lights, also known as traffic signals, traffic lamps, signal lights, stop lights, or technically as traffic control signals are signalling device positioned at road intersection, pedestrian crossings, and other locations to control competing flows of traffic.

In this project we will create a simple “traffic light”, imitating what traffic light does and program it using Arduino.

Objective

  • Three light switch each other to perform a simple traffic light.
  • Multiple digital signal output.
  • The order of light in one cycle: red – yellow – green – yellow

Prerequisite

Requirement

Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LEDs ( red x1, yellow x1, green x1 )
  • Resistor 10k ohm x3
  • Breadboard

You can change the Arduino board to higher specification if you like.

Because we want to do this as realistic as we can, we will use three different LED: one red LED, one yellow LED, one green LED.

The resistors should be 1/4 Watt resistors with resistance value of 10k ohm. You can use resistors of any band.

You also need an USB A to B cable for upload our program to our Arduino.

Circuit

Here is the circuit image, created using Fritzing.

Traffic Lamp

We use three LED at once. The first LED is red, the second LED should be yellow, and the last one is green LED. Remember which one is the positive leg and which one is negative leg (anode and cathode) for each LED. Connect attach the long leg of an LED (positive leg or anode) with resistor and then connect it to corresponding pin. Red one will use pin 12, yellow will be in 11, and green would use pin 10. Connect the short leg (negative leg or cathode) to the ground (GND).

Code

Let’s see what code we use for this project and discuss it later.

We define three constant here: RED_PIN, YELLOW_PIN, and GREEN_PIN for red, yellow, and green LED respectively. However this time we only define it using macro not as constant. Then we create an array of LED as enumLED. This array will be our shortcut to switch between LED. For indexing we use currentLED variable.

The timeout array is an array which stored delay interval for each LED in seconds. This means our lights won’t turned on in same duration.

The alternate variable is used to increment the currentLED index. Remember that in one cycle, we define the LED switching as red – yellow – green – yellow.

Result

LED is turned on in following order:

  • red for 10 seconds
  • yellow for 1 second
  • green for 20 seconds
  • yellow for 1 second
  • red for 10 seconds at another cycle.

Why it Works

Our code is as simple as turned off current LED and turned on the next LED. The previous, current, and next LED are defined in an array and we use currentLED variable as an index. The alternate is used to increase or decrease the index and make our LED obey our pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">