Chapter 09 – Experiment 02 – TRAFFIC LIGHTS

In the previous experiment we had a single LED blinking, changed the fading, the brightness and the speed of blinking. With this new experiment, we will try to improve adding more LEDs and creating a more interesting application that we see every day when we drive all around, we wanted to provide you the experiment with practical application, so you can understand this kit is not only for fun but make you to understand and create also some practical applications. Now it’s time to go go ahead with this new experiment, let’s connect two groups of 3 LEDs and a group of two LEDs. The three LEDs will be our traffic lights, like in a crossing and the group of two LEDs will be the pedestrians light sign.

In addition to create a more complex program, we will learn to use arrays, a for() loop, functions and simple programming methods to a program small (this due the limited memory of the microprocessor).


Traffic lights
Turns on a series of LEDs as a traffic light.

This example code is in the public domain.

// a is the first traffic lights, b the second one. Red is on 7 pin, Yellow on 6 and Green on 5. For the second traffic light you have 4,3 and 2.

int a = 7; // first traffic light
int b = 4; // second traffic light

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as an output.
pinMode(a, OUTPUT);
pinMode(a – 1, OUTPUT);
pinMode(a – 2, OUTPUT);

pinMode(b, OUTPUT);
pinMode(b – 1, OUTPUT);
pinMode(b – 2, OUTPUT);

// the loop routine runs over and over again forever:
void loop() {
// You will notice that the multi tasking is not fully implemented. Check

void trafficlight(int a) {
led(a, HIGH, 0);
led(a – 1, LOW, 0);
led(a – 2, LOW, 0);
delay (4000);

led(a, LOW, 0);
led(a – 1, LOW, 0);
led(a – 2, HIGH, 0);
delay (4000);

led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);
led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);
led(a – 1, HIGH, 1000);
led(a – 1, LOW, 1000);


void led(int led, int value, int ledDelay) {
digitalWrite(led, value);   // turn the LED on (HIGH is the voltage level)
delay(ledDelay);               // wait for X milliseconds


Some LEDs Fail to Light
It is easy to insert an LED backwards. Check the LEDs that aren’t working and ensure they the right way around.

Operating out of sequence
With eight wires it’s easy to cross a couple. Double check that the first LED is plugged into pin 2 and each pin there after.

Starting afresh
It is easy to accidentally misplace a wire without noticing it. Pulling everything out and starting with a fresh slate is often easier than trying to track down the problem.


Making the two traffic lights working in sync

The library ( ) can help to run in background tasks, try to download and use it.

Switching to loops:
In the loop() function there are 4 lines. The last three all start with a ‘//’. This means the line is treated as a comment (not run). To switch the program to use different type of animations, remove and add the // in the code leaving only one function request uncommented.

Upload the program, you will notice that using oneAfterAnotherNoLoop();  or oneAfterAnotherLoop(); functions nothing is changed. You can take a look at the two functions, each does the same thing, but use different approaches (hint: the second one uses a for loop).

The other two functions  oneOnAtATime();  and inAndOut(); are different, the first one is doing that the other one is doing this.

Extra animations:
Tired of these animations? Try to create and invent new sample animations using your fantasy. In one of the future experiments you will see how this different type of animations will allow you to create an incredible special effect. You will be amazed, we promise!

Below: amateur video showing the experiment working



  1. [...] Chapter 07 – MICROCONTROLLER, PINS AND LAYOUT Chapter 08 – Experiment 01 – BLINKING LED Chapter 09 – Experiment 02 – TRAFFIC LIGHTS Chapter 10 – Experiment 03 – LIGHT [...]

  2. Be careful when you copy/paste the example in this page: the minus sign is not actually a minus, it must be corrected.

  3. When you copy from the page and paste the code the minus is converted with another sign. You should replace it with a minus sign (-)

    • The code dosent work at all . Tried with the to replace instead of – with ” (-) but still didnt work …
      and i am doing the experiment with 3 diodes keep it simple…
      what should i do ?

  4. [...] Traffic Lights (details for this experiment here) [...]

  5. It doesn’t work
    Is there any problem with the code ???

  6. I read a lot of interesting posts here. Probably you spend a lot of time writing,
    i know how to save you a lot of work, there is an online tool that creates
    high quality, SEO friendly posts in seconds, just search
    in google – k2seotips unlimited content

  7. I must thank you for the efforts you have put in penning this blog.
    I’m hoping to check out the same high-grade blog posts from you
    in the future as well. In fact, your creative writing
    abilities has encouraged me to get my own, personal site now ;

  8. It’s nearly impossible to find experienced people for this topic, however, you
    sound like you know what you’re talking about!

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat