LED traffic lights (1)

Now, we start a new circuit.

Task:
Build a traffic lights!

The circuit:

1.2-LED

For this tutorial, wee need the following components:

  • LED red (i.e. 2V, max. 20 mA)
  • LED yellow (i.e. 2V, max. 20 mA)
  • LED green (i.e. 2V, max. 20 mA)
  • 3x resistors (min. 150 Ohm)

The LED is a standard component. We use  LEDs with 2V and a maximum current of 20 mA. Please, look tutorial 1.0 or 1.1  how to calculate the resistor. It’s the seem.

In this tutorial, we use three pins – pin 9 to control the red LED, pin 10 to control the yellow LED and pin 11 to control the green LED.

Here is the wiring diagram:

1.2-LED-circuit

We start our Arduino software to create the first sketch.

/*
 LED traffic lights
 playground2014.worldpress.com
*/

int pinred = 9;           // PinPort LED red
int pinyellow = 10;       // PinPort LED yellow
int pingreen = 11;        // PinPort LED green

void setup()  { 
  pinMode(pinred, OUTPUT);  
  pinMode(pinyellow, OUTPUT);  
  pinMode(pingreen, OUTPUT);

  // to initialize the traffic ligths with red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);
} 

void loop()  { 
  // let us start: the traffic ligths is red
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, LOW);
  delay(2000);                   

  // attention, we will switch
  digitalWrite(pinred, HIGH);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000); 

  // it's green
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, LOW);
  digitalWrite(pingreen, HIGH);
  delay(2000);

  // attention, we will switch
  digitalWrite(pinred, LOW);
  digitalWrite(pinyellow, HIGH);
  digitalWrite(pingreen, LOW);
  delay(1000);

  //it's red
  // we start again (loop)
}

First, we define our variables.
void setup() { … }
We define the direction – we use all three pins as output pins. And, the first time, we set a default value.

void loop() { … }
I think, the code is easy and you will understand it without long explanations.

You can adjust the breaks between the phases.
STOPP! Don’t destroy this circuit, this is the basic for the next circuit!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s